api-ix commited on
Commit
19b540a
·
verified ·
1 Parent(s): 2ea4a8e

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +51 -60
index.html CHANGED
@@ -12,8 +12,8 @@
12
  --bg-color: #212121;
13
  --text-color: #e0e0e0;
14
  --accent-color: #4caf50;
 
15
  --panel-bg: #333;
16
- --highlight: #00e5ff;
17
  }
18
 
19
  body {
@@ -25,7 +25,7 @@
25
  align-items: center;
26
  margin: 0;
27
  padding: 10px;
28
- user-select: none; /* Prevent text selection while tapping */
29
  }
30
 
31
  h1 { margin-bottom: 15px; text-align: center; font-size: 1.5rem; }
@@ -40,7 +40,7 @@
40
  width: 100%;
41
  }
42
 
43
- /* --- BOARD --- */
44
  #board {
45
  width: 100%;
46
  max-width: 380px;
@@ -54,6 +54,12 @@
54
  color: #777;
55
  }
56
 
 
 
 
 
 
 
57
  /* --- TURN SWITCHER UI --- */
58
  .turn-controls {
59
  display: flex;
@@ -82,7 +88,6 @@
82
  .turn-card img { height: 35px; }
83
  .turn-card span { font-weight: bold; }
84
 
85
- /* Active Turn Styles */
86
  .turn-card.active {
87
  opacity: 1;
88
  background: #505050;
@@ -120,21 +125,6 @@
120
  letter-spacing: 2px;
121
  }
122
 
123
- .eval-bar {
124
- height: 6px;
125
- background: #555;
126
- border-radius: 3px;
127
- overflow: hidden;
128
- margin-top: 5px;
129
- position: relative;
130
- }
131
- .eval-fill {
132
- height: 100%;
133
- width: 50%;
134
- background: var(--highlight);
135
- transition: width 0.5s ease;
136
- }
137
-
138
  .controls { display: flex; gap: 10px; margin-top: 5px; }
139
  button {
140
  flex: 1;
@@ -202,19 +192,12 @@
202
  <script src="https://cdnjs.cloudflare.com/ajax/libs/chessboard-js/1.0.0/chessboard-1.0.0.min.js"></script>
203
 
204
  <script>
205
- // --- ERROR HANDLING ---
206
- window.onerror = function(msg) {
207
- $('#debug-log').show().text("Err: " + msg);
208
- };
209
 
210
- // --- CONFIG ---
211
  const STOCKFISH_PATH = 'https://cdnjs.cloudflare.com/ajax/libs/stockfish.js/10.0.0/stockfish.js';
212
  let board, game, engine;
213
 
214
- // --- INIT ---
215
- $(document).ready(function() {
216
- initGame();
217
- });
218
 
219
  function initGame() {
220
  game = new Chess();
@@ -236,44 +219,44 @@
236
  updateUI();
237
  }
238
 
239
- // --- TURN CONTROL LOGIC ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
240
  function forceTurn(color) {
241
- // Only switch if it's not already that color's turn
242
  if (game.turn() === color) return;
243
-
244
- // 1. Get current FEN
245
  let fen = game.fen();
246
  let parts = fen.split(' ');
247
-
248
- // 2. Manipulate the active color (2nd part of FEN string)
249
  parts[1] = color;
250
-
251
- // 3. Clear En Passant target (3rd part) to avoid logic errors when swapping turns
252
  parts[3] = '-';
253
-
254
- // 4. Rebuild FEN
255
  let newFen = parts.join(' ');
256
-
257
- // 5. Load into game
258
- let result = game.load(newFen);
259
-
260
- if(result) {
261
  board.position(newFen);
262
  updateUI();
263
- analyzePosition(); // Re-analyze for the new player
264
- } else {
265
- console.log("Cannot switch turn: Illegal position");
266
  }
267
  }
268
 
269
  function updateUI() {
270
- // Update Turn Cards
271
  $('.turn-card').removeClass('active');
272
- if (game.turn() === 'w') {
273
- $('#turn-w').addClass('active');
274
- } else {
275
- $('#turn-b').addClass('active');
276
- }
277
  }
278
 
279
  // --- ENGINE ---
@@ -291,11 +274,8 @@
291
  }
292
 
293
  if (line.startsWith('info') && line.includes('score cp')) {
294
- // Parse Score
295
  const matchScore = line.match(/score cp (-?\d+)/);
296
  const matchMate = line.match(/score mate (-?\d+)/);
297
-
298
- // Parse Best Move
299
  const matchPv = line.match(/ pv (.+)/);
300
 
301
  if (matchMate) {
@@ -308,12 +288,14 @@
308
  }
309
 
310
  if (matchPv) {
311
- // SPLIT the line and take only the first move
312
  let moves = matchPv[1].split(' ');
313
  let bestMove = moves[0];
314
 
315
- // Make it look pretty (e.g., e2e4)
316
  $('#best-move').html(`➜ ${bestMove}`);
 
 
 
317
  }
318
  }
319
  };
@@ -326,6 +308,9 @@
326
 
327
  function analyzePosition() {
328
  $('#best-move').text('...');
 
 
 
329
  $('#engineStatus').text('Thinking...');
330
  if(engine) {
331
  engine.postMessage(`position fen ${game.fen()}`);
@@ -333,10 +318,9 @@
333
  }
334
  }
335
 
336
- // --- CHESS LOGIC ---
337
  function onDragStart(source, piece) {
338
  if (game.game_over()) return false;
339
- // Strict movement: only allow moving the piece whose turn it is
340
  if ((game.turn() === 'w' && piece.search(/^b/) !== -1) ||
341
  (game.turn() === 'b' && piece.search(/^w/) !== -1)) {
342
  return false;
@@ -360,16 +344,23 @@
360
  board.position(game.fen());
361
  }
362
 
363
- // --- BUTTONS ---
364
  $('#resetBtn').click(function() {
365
  game.reset();
366
  board.start();
367
  $('#best-move').text('...');
 
 
368
  updateUI();
369
  });
370
 
371
  $('#flipBtn').click(function() {
372
  board.flip();
 
 
 
 
 
 
373
  });
374
 
375
  </script>
 
12
  --bg-color: #212121;
13
  --text-color: #e0e0e0;
14
  --accent-color: #4caf50;
15
+ --highlight: #00e5ff; /* Cyan for Best Move */
16
  --panel-bg: #333;
 
17
  }
18
 
19
  body {
 
25
  align-items: center;
26
  margin: 0;
27
  padding: 10px;
28
+ user-select: none;
29
  }
30
 
31
  h1 { margin-bottom: 15px; text-align: center; font-size: 1.5rem; }
 
40
  width: 100%;
41
  }
42
 
43
+ /* --- BOARD STYLES --- */
44
  #board {
45
  width: 100%;
46
  max-width: 380px;
 
54
  color: #777;
55
  }
56
 
57
+ /* --- HIGHLIGHT CLASS (Applied by JS) --- */
58
+ .highlight-best {
59
+ box-shadow: inset 0 0 3px 3px var(--highlight) !important;
60
+ background-color: rgba(0, 229, 255, 0.3) !important;
61
+ }
62
+
63
  /* --- TURN SWITCHER UI --- */
64
  .turn-controls {
65
  display: flex;
 
88
  .turn-card img { height: 35px; }
89
  .turn-card span { font-weight: bold; }
90
 
 
91
  .turn-card.active {
92
  opacity: 1;
93
  background: #505050;
 
125
  letter-spacing: 2px;
126
  }
127
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
128
  .controls { display: flex; gap: 10px; margin-top: 5px; }
129
  button {
130
  flex: 1;
 
192
  <script src="https://cdnjs.cloudflare.com/ajax/libs/chessboard-js/1.0.0/chessboard-1.0.0.min.js"></script>
193
 
194
  <script>
195
+ window.onerror = function(msg) { $('#debug-log').show().text("Err: " + msg); };
 
 
 
196
 
 
197
  const STOCKFISH_PATH = 'https://cdnjs.cloudflare.com/ajax/libs/stockfish.js/10.0.0/stockfish.js';
198
  let board, game, engine;
199
 
200
+ $(document).ready(function() { initGame(); });
 
 
 
201
 
202
  function initGame() {
203
  game = new Chess();
 
219
  updateUI();
220
  }
221
 
222
+ // --- VISUAL HIGHLIGHT LOGIC ---
223
+ function highlightSquares(move) {
224
+ // 1. Remove old highlights (Note: 'square-55d63' is the class chessboard.js uses for all squares)
225
+ $('#board .square-55d63').removeClass('highlight-best');
226
+
227
+ // 2. Parse the move (e.g., "e2e4")
228
+ let source = move.substring(0, 2);
229
+ let target = move.substring(2, 4);
230
+
231
+ // 3. Add highlight class to source and target squares
232
+ // Chessboard.js adds classes like .square-e2, .square-a4, etc.
233
+ $('#board .square-' + source).addClass('highlight-best');
234
+ $('#board .square-' + target).addClass('highlight-best');
235
+ }
236
+
237
+ function removeHighlights() {
238
+ $('#board .square-55d63').removeClass('highlight-best');
239
+ }
240
+
241
+ // --- TURN LOGIC ---
242
  function forceTurn(color) {
 
243
  if (game.turn() === color) return;
 
 
244
  let fen = game.fen();
245
  let parts = fen.split(' ');
 
 
246
  parts[1] = color;
 
 
247
  parts[3] = '-';
 
 
248
  let newFen = parts.join(' ');
249
+ if(game.load(newFen)) {
 
 
 
 
250
  board.position(newFen);
251
  updateUI();
252
+ analyzePosition();
 
 
253
  }
254
  }
255
 
256
  function updateUI() {
 
257
  $('.turn-card').removeClass('active');
258
+ if (game.turn() === 'w') $('#turn-w').addClass('active');
259
+ else $('#turn-b').addClass('active');
 
 
 
260
  }
261
 
262
  // --- ENGINE ---
 
274
  }
275
 
276
  if (line.startsWith('info') && line.includes('score cp')) {
 
277
  const matchScore = line.match(/score cp (-?\d+)/);
278
  const matchMate = line.match(/score mate (-?\d+)/);
 
 
279
  const matchPv = line.match(/ pv (.+)/);
280
 
281
  if (matchMate) {
 
288
  }
289
 
290
  if (matchPv) {
 
291
  let moves = matchPv[1].split(' ');
292
  let bestMove = moves[0];
293
 
294
+ // UPDATE TEXT
295
  $('#best-move').html(`➜ ${bestMove}`);
296
+
297
+ // UPDATE BOARD HIGHLIGHTS
298
+ highlightSquares(bestMove);
299
  }
300
  }
301
  };
 
308
 
309
  function analyzePosition() {
310
  $('#best-move').text('...');
311
+ // Clear highlights while thinking so we don't show old info
312
+ removeHighlights();
313
+
314
  $('#engineStatus').text('Thinking...');
315
  if(engine) {
316
  engine.postMessage(`position fen ${game.fen()}`);
 
318
  }
319
  }
320
 
321
+ // --- GAME MOVEMENT ---
322
  function onDragStart(source, piece) {
323
  if (game.game_over()) return false;
 
324
  if ((game.turn() === 'w' && piece.search(/^b/) !== -1) ||
325
  (game.turn() === 'b' && piece.search(/^w/) !== -1)) {
326
  return false;
 
344
  board.position(game.fen());
345
  }
346
 
 
347
  $('#resetBtn').click(function() {
348
  game.reset();
349
  board.start();
350
  $('#best-move').text('...');
351
+ $('#eval-score').text('0.00');
352
+ removeHighlights();
353
  updateUI();
354
  });
355
 
356
  $('#flipBtn').click(function() {
357
  board.flip();
358
+ // Re-apply highlights after flip because DOM is redrawn
359
+ let currentText = $('#best-move').text();
360
+ if(currentText.includes('➜')) {
361
+ let move = currentText.replace('➜ ', '');
362
+ highlightSquares(move);
363
+ }
364
  });
365
 
366
  </script>