Raven7 commited on
Commit
2fcf273
·
verified ·
1 Parent(s): f45f133

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +170 -106
index.html CHANGED
@@ -25,9 +25,8 @@
25
  display: flex;
26
  justify-content: center;
27
  align-items: center;
28
- font-size: 40px;
29
- cursor: pointer;
30
  position: relative;
 
31
  }
32
 
33
  .white {
@@ -38,34 +37,17 @@
38
  background-color: #769656;
39
  }
40
 
41
- .highlight {
42
- background-color: rgba(255, 255, 0, 0.4) !important;
43
- }
44
-
45
- .possible-move {
46
- position: relative;
47
- }
48
-
49
- .possible-move::before {
50
- content: '';
51
- position: absolute;
52
- width: 20px;
53
- height: 20px;
54
- background-color: rgba(0,0,0,0.2);
55
- border-radius: 50%;
56
- }
57
-
58
  .piece {
 
59
  user-select: none;
60
  }
61
 
62
- .white-piece {
63
- color: white;
64
- text-shadow: 1px 1px 2px black;
65
  }
66
 
67
- .black-piece {
68
- color: black;
69
  }
70
  </style>
71
  </head>
@@ -73,9 +55,6 @@
73
  <div id="chessboard"></div>
74
 
75
  <script>
76
- let selectedPiece = null;
77
- let currentTurn = 'white';
78
-
79
  const pieces = {
80
  'white': {
81
  'king': '♔',
@@ -95,35 +74,25 @@
95
  }
96
  };
97
 
 
 
 
98
  function createBoard() {
99
  const board = document.getElementById('chessboard');
100
  for (let row = 0; row < 8; row++) {
101
  for (let col = 0; col < 8; col++) {
102
  const square = document.createElement('div');
103
- square.classList.add('square');
104
- square.classList.add((row + col) % 2 ? 'black' : 'white');
105
  square.dataset.row = row;
106
  square.dataset.col = col;
107
 
108
- // Add pieces
109
- if (row < 2 || row > 5) {
110
- const color = row < 2 ? 'black' : 'white';
111
- let piece;
112
- if (row === 1 || row === 6) {
113
- piece = 'pawn';
114
- } else {
115
- if (col === 0 || col === 7) piece = 'rook';
116
- if (col === 1 || col === 6) piece = 'knight';
117
- if (col === 2 || col === 5) piece = 'bishop';
118
- if (col === 3) piece = 'queen';
119
- if (col === 4) piece = 'king';
120
- }
121
- if (piece) {
122
- square.innerHTML = pieces[color][piece];
123
- square.dataset.piece = piece;
124
- square.dataset.color = color;
125
- square.classList.add('piece', `${color}-piece`);
126
- }
127
  }
128
 
129
  square.addEventListener('click', handleClick);
@@ -132,79 +101,174 @@
132
  }
133
  }
134
 
135
- function clearHighlights() {
136
- document.querySelectorAll('.square').forEach(square => {
137
- square.classList.remove('highlight', 'possible-move');
138
- });
139
- }
140
-
141
- function showPossibleMoves(square) {
142
- const piece = square.dataset.piece;
143
- const row = parseInt(square.dataset.row);
144
- const col = parseInt(square.dataset.col);
145
- const color = square.dataset.color;
146
-
147
- if (piece === 'pawn') {
148
- const direction = color === 'white' ? -1 : 1;
149
- const startRow = color === 'white' ? 6 : 1;
150
-
151
- // Forward moves
152
- let nextRow = row + direction;
153
- if (nextRow >= 0 && nextRow < 8) {
154
- const forwardSquare = document.querySelector(`[data-row="${nextRow}"][data-col="${col}"]`);
155
- if (!forwardSquare.dataset.piece) {
156
- forwardSquare.classList.add('possible-move');
157
-
158
- // Initial two-square move
159
- if (row === startRow) {
160
- const twoAheadSquare = document.querySelector(`[data-row="${row + 2 * direction}"][data-col="${col}"]`);
161
- if (!twoAheadSquare.dataset.piece) {
162
- twoAheadSquare.classList.add('possible-move');
163
- }
164
- }
165
- }
166
- }
167
-
168
- // Captures
169
- [-1, 1].forEach(offset => {
170
- const captureSquare = document.querySelector(`[data-row="${nextRow}"][data-col="${col + offset}"]`);
171
- if (captureSquare && captureSquare.dataset.piece && captureSquare.dataset.color !== color) {
172
- captureSquare.classList.add('possible-move');
173
- }
174
- });
175
- }
176
  }
177
 
178
  function handleClick(event) {
179
- const square = event.target;
180
- const piece = square.dataset.piece;
181
- const color = square.dataset.color;
182
 
183
  clearHighlights();
184
 
185
  if (selectedPiece) {
186
  if (square.classList.contains('possible-move')) {
187
- // Move piece
188
- square.innerHTML = selectedPiece.innerHTML;
189
- square.dataset.piece = selectedPiece.dataset.piece;
190
- square.dataset.color = selectedPiece.dataset.color;
191
- square.classList.add('piece', `${selectedPiece.dataset.color}-piece`);
192
-
193
- selectedPiece.innerHTML = '';
194
- selectedPiece.removeAttribute('data-piece');
195
- selectedPiece.removeAttribute('data-color');
196
- selectedPiece.classList.remove('piece', 'white-piece', 'black-piece');
197
-
198
  currentTurn = currentTurn === 'white' ? 'black' : 'white';
 
 
199
  }
200
- selectedPiece = null;
201
- } else if (piece && color === currentTurn) {
202
  selectedPiece = square;
203
- square.classList.add('highlight');
204
  showPossibleMoves(square);
205
  }
206
  }
207
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
208
  createBoard();
209
  </script>
210
  </body>
 
25
  display: flex;
26
  justify-content: center;
27
  align-items: center;
 
 
28
  position: relative;
29
+ cursor: pointer;
30
  }
31
 
32
  .white {
 
37
  background-color: #769656;
38
  }
39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
  .piece {
41
+ font-size: 45px;
42
  user-select: none;
43
  }
44
 
45
+ .highlight {
46
+ background-color: yellow !important;
 
47
  }
48
 
49
+ .possible-move {
50
+ background-color: rgba(144, 238, 144, 0.5) !important;
51
  }
52
  </style>
53
  </head>
 
55
  <div id="chessboard"></div>
56
 
57
  <script>
 
 
 
58
  const pieces = {
59
  'white': {
60
  'king': '♔',
 
74
  }
75
  };
76
 
77
+ let selectedPiece = null;
78
+ let currentTurn = 'white';
79
+
80
  function createBoard() {
81
  const board = document.getElementById('chessboard');
82
  for (let row = 0; row < 8; row++) {
83
  for (let col = 0; col < 8; col++) {
84
  const square = document.createElement('div');
85
+ square.classList.add('square', (row + col) % 2 === 0 ? 'white' : 'black');
 
86
  square.dataset.row = row;
87
  square.dataset.col = col;
88
 
89
+ if (row === 1 || row === 6) {
90
+ const color = row === 1 ? 'black' : 'white';
91
+ addPiece(square, color, 'pawn');
92
+ } else if (row === 0 || row === 7) {
93
+ const color = row === 0 ? 'black' : 'white';
94
+ const pieceOrder = ['rook', 'knight', 'bishop', 'queen', 'king', 'bishop', 'knight', 'rook'];
95
+ addPiece(square, color, pieceOrder[col]);
 
 
 
 
 
 
 
 
 
 
 
 
96
  }
97
 
98
  square.addEventListener('click', handleClick);
 
101
  }
102
  }
103
 
104
+ function addPiece(square, color, pieceType) {
105
+ const piece = document.createElement('span');
106
+ piece.classList.add('piece');
107
+ piece.dataset.piece = pieceType;
108
+ piece.dataset.color = color;
109
+ piece.textContent = pieces[color][pieceType];
110
+ square.appendChild(piece);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
111
  }
112
 
113
  function handleClick(event) {
114
+ const square = event.currentTarget;
115
+ const piece = square.querySelector('.piece');
 
116
 
117
  clearHighlights();
118
 
119
  if (selectedPiece) {
120
  if (square.classList.contains('possible-move')) {
121
+ movePiece(selectedPiece, square);
122
+ selectedPiece = null;
 
 
 
 
 
 
 
 
 
123
  currentTurn = currentTurn === 'white' ? 'black' : 'white';
124
+ } else {
125
+ selectedPiece = null;
126
  }
127
+ } else if (piece && piece.dataset.color === currentTurn) {
 
128
  selectedPiece = square;
 
129
  showPossibleMoves(square);
130
  }
131
  }
132
 
133
+ function movePiece(fromSquare, toSquare) {
134
+ const piece = fromSquare.querySelector('.piece');
135
+ toSquare.innerHTML = '';
136
+ toSquare.appendChild(piece);
137
+ }
138
+
139
+ function showPossibleMoves(square) {
140
+ const piece = square.querySelector('.piece');
141
+ const pieceType = piece.dataset.piece;
142
+ const color = piece.dataset.color;
143
+ const row = parseInt(square.dataset.row);
144
+ const col = parseInt(square.dataset.col);
145
+
146
+ switch (pieceType) {
147
+ case 'pawn':
148
+ showPawnMoves(row, col, color);
149
+ break;
150
+ case 'knight':
151
+ showKnightMoves(row, col, color);
152
+ break;
153
+ case 'bishop':
154
+ showBishopMoves(row, col, color);
155
+ break;
156
+ case 'rook':
157
+ showRookMoves(row, col, color);
158
+ break;
159
+ case 'queen':
160
+ showQueenMoves(row, col, color);
161
+ break;
162
+ case 'king':
163
+ showKingMoves(row, col, color);
164
+ break;
165
+ }
166
+ }
167
+
168
+ function showPawnMoves(row, col, color) {
169
+ const direction = color === 'white' ? -1 : 1;
170
+ const startRow = color === 'white' ? 6 : 1;
171
+
172
+ // Forward move
173
+ let nextRow = row + direction;
174
+ if (isEmpty(nextRow, col)) {
175
+ highlightSquare(nextRow, col);
176
+ if (row === startRow && isEmpty(nextRow + direction, col)) {
177
+ highlightSquare(nextRow + direction, col);
178
+ }
179
+ }
180
+
181
+ // Captures
182
+ [-1, 1].forEach(offset => {
183
+ const captureRow = row + direction;
184
+ const captureCol = col + offset;
185
+ if (isOpponent(captureRow, captureCol, color)) {
186
+ highlightSquare(captureRow, captureCol);
187
+ }
188
+ });
189
+ }
190
+
191
+ function showKnightMoves(row, col, color) {
192
+ const moves = [[-2, -1], [-2, 1], [-1, -2], [-1, 2], [1, -2], [1, 2], [2, -1], [2, 1]];
193
+ moves.forEach(([dr, dc]) => {
194
+ const r = row + dr;
195
+ const c = col + dc;
196
+ if (isValidMove(r, c, color)) {
197
+ highlightSquare(r, c);
198
+ }
199
+ });
200
+ }
201
+
202
+ function showBishopMoves(row, col, color) {
203
+ showLineMoves(row, col, color, [[-1, -1], [-1, 1], [1, -1], [1, 1]]);
204
+ }
205
+
206
+ function showRookMoves(row, col, color) {
207
+ showLineMoves(row, col, color, [[-1, 0], [1, 0], [0, -1], [0, 1]]);
208
+ }
209
+
210
+ function showQueenMoves(row, col, color) {
211
+ showBishopMoves(row, col, color);
212
+ showRookMoves(row, col, color);
213
+ }
214
+
215
+ function showKingMoves(row, col, color) {
216
+ const moves = [[-1, -1], [-1, 0], [-1, 1], [0, -1], [0, 1], [1, -1], [1, 0], [1, 1]];
217
+ moves.forEach(([dr, dc]) => {
218
+ const r = row + dr;
219
+ const c = col + dc;
220
+ if (isValidMove(r, c, color)) {
221
+ highlightSquare(r, c);
222
+ }
223
+ });
224
+ }
225
+
226
+ function showLineMoves(row, col, color, directions) {
227
+ directions.forEach(([dr, dc]) => {
228
+ let r = row + dr;
229
+ let c = col + dc;
230
+ while (isValidMove(r, c, color)) {
231
+ highlightSquare(r, c);
232
+ if (isOpponent(r, c, color)) break;
233
+ r += dr;
234
+ c += dc;
235
+ }
236
+ });
237
+ }
238
+
239
+ function isValidMove(row, col, color) {
240
+ return isOnBoard(row, col) && !isAlly(row, col, color);
241
+ }
242
+
243
+ function isEmpty(row, col) {
244
+ return isOnBoard(row, col) && !document.querySelector(`[data-row="${row}"][data-col="${col}"] .piece`);
245
+ }
246
+
247
+ function isOpponent(row, col, color) {
248
+ const piece = document.querySelector(`[data-row="${row}"][data-col="${col}"] .piece`);
249
+ return piece && piece.dataset.color !== color;
250
+ }
251
+
252
+ function isAlly(row, col, color) {
253
+ const piece = document.querySelector(`[data-row="${row}"][data-col="${col}"] .piece`);
254
+ return piece && piece.dataset.color === color;
255
+ }
256
+
257
+ function isOnBoard(row, col) {
258
+ return row >= 0 && row < 8 && col >= 0 && col < 8;
259
+ }
260
+
261
+ function highlightSquare(row, col) {
262
+ const square = document.querySelector(`[data-row="${row}"][data-col="${col}"]`);
263
+ square.classList.add('possible-move');
264
+ }
265
+
266
+ function clearHighlights() {
267
+ document.querySelectorAll('.square').forEach(square => {
268
+ square.classList.remove('highlight', 'possible-move');
269
+ });
270
+ }
271
+
272
  createBoard();
273
  </script>
274
  </body>