Raven7 commited on
Commit
f45f133
Β·
verified Β·
1 Parent(s): 073c4dc

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +185 -301
index.html CHANGED
@@ -1,327 +1,211 @@
1
  <!DOCTYPE html>
2
  <html>
3
  <head>
4
- <style>
5
- .board {
6
- width: 480px;
7
- height: 480px;
8
- border: 2px solid #333;
9
- display: grid;
10
- grid-template-columns: repeat(8, 1fr);
11
- }
12
-
13
- .square {
14
- width: 60px;
15
- height: 60px;
16
- display: flex;
17
- justify-content: center;
18
- align-items: center;
19
- font-size: 40px;
20
- cursor: pointer;
21
- position: relative;
22
- }
23
-
24
- .white {
25
- background: #fff;
26
- }
27
-
28
- .black {
29
- background: #999;
30
- }
31
-
32
- .highlight {
33
- background: rgba(255, 255, 0, 0.5);
34
- }
35
-
36
- .possible-move::after {
37
- content: '';
38
- position: absolute;
39
- width: 20px;
40
- height: 20px;
41
- background: rgba(0, 255, 0, 0.3);
42
- border-radius: 50%;
43
- }
44
-
45
- .captured {
46
- margin: 20px;
47
- padding: 10px;
48
- border: 1px solid #ccc;
49
- }
50
-
51
- .captured-piece {
52
- font-size: 24px;
53
- margin: 0 5px;
54
- }
55
- </style>
56
- </head>
57
- <body>
58
- <div class="board" id="board"></div>
59
- <div class="captured">
60
- <div>
61
- White captured: <span id="whiteCaptured"></span>
62
- </div>
63
- <div>
64
- Black captured: <span id="blackCaptured"></span>
65
- </div>
66
- </div>
67
-
68
- <script>
69
- const board = document.getElementById('board');
70
- const whiteCaptured = document.getElementById('whiteCaptured');
71
- const blackCaptured = document.getElementById('blackCaptured');
72
- let selectedPiece = null;
73
- let currentTurn = 'white';
74
- let whiteCapturedPieces = [];
75
- let blackCapturedPieces = [];
76
-
77
- const initialBoard = [
78
- ['β™œ', 'β™ž', '♝', 'β™›', 'β™š', '♝', 'β™ž', 'β™œ'],
79
- ['β™Ÿ', 'β™Ÿ', 'β™Ÿ', 'β™Ÿ', 'β™Ÿ', 'β™Ÿ', 'β™Ÿ', 'β™Ÿ'],
80
- ['', '', '', '', '', '', '', ''],
81
- ['', '', '', '', '', '', '', ''],
82
- ['', '', '', '', '', '', '', ''],
83
- ['', '', '', '', '', '', '', ''],
84
- ['β™™', 'β™™', 'β™™', 'β™™', 'β™™', 'β™™', 'β™™', 'β™™'],
85
- ['β™–', 'β™˜', 'β™—', 'β™•', 'β™”', 'β™—', 'β™˜', 'β™–']
86
- ];
87
-
88
- let gameBoard = JSON.parse(JSON.stringify(initialBoard));
89
-
90
- function createBoard() {
91
- board.innerHTML = '';
92
- for (let i = 0; i < 8; i++) {
93
- for (let j = 0; j < 8; j++) {
94
- const square = document.createElement('div');
95
- square.className = `square ${(i + j) % 2 === 0 ? 'white' : 'black'}`;
96
- square.dataset.row = i;
97
- square.dataset.col = j;
98
- square.textContent = gameBoard[i][j];
99
- square.addEventListener('click', handleClick);
100
- board.appendChild(square);
101
  }
102
- }
103
- }
104
 
105
- function isWhitePiece(piece) {
106
- return 'β™”β™•β™–β™—β™˜β™™'.includes(piece);
107
- }
 
 
 
108
 
109
- function isBlackPiece(piece) {
110
- return 'β™šβ™›β™œβ™β™žβ™Ÿ'.includes(piece);
111
- }
 
 
 
 
 
 
 
112
 
113
- function clearHighlights() {
114
- document.querySelectorAll('.square').forEach(square => {
115
- square.classList.remove('highlight', 'possible-move');
116
- });
117
- }
118
 
119
- function getPossibleMoves(row, col) {
120
- const piece = gameBoard[row][col];
121
- const moves = [];
122
 
123
- switch (piece) {
124
- case 'β™”':
125
- case 'β™š':
126
- // King moves
127
- for (let i = -1; i <= 1; i++) {
128
- for (let j = -1; j <= 1; j++) {
129
- if (i === 0 && j === 0) continue;
130
- const newRow = row + i;
131
- const newCol = col + j;
132
- if (isValidMove(newRow, newCol, piece)) {
133
- moves.push([newRow, newCol]);
134
- }
135
- }
136
- }
137
- break;
138
 
139
- case 'β™•':
140
- case 'β™›':
141
- // Queen moves (combination of rook and bishop)
142
- moves.push(...getRookMoves(row, col));
143
- moves.push(...getBishopMoves(row, col));
144
- break;
145
 
146
- case 'β™–':
147
- case 'β™œ':
148
- // Rook moves
149
- moves.push(...getRookMoves(row, col));
150
- break;
 
 
 
151
 
152
- case 'β™—':
153
- case '♝':
154
- // Bishop moves
155
- moves.push(...getBishopMoves(row, col));
156
- break;
157
 
158
- case 'β™˜':
159
- case 'β™ž':
160
- // Knight moves
161
- const knightMoves = [
162
- [-2, -1], [-2, 1], [-1, -2], [-1, 2],
163
- [1, -2], [1, 2], [2, -1], [2, 1]
164
- ];
165
- for (const [dRow, dCol] of knightMoves) {
166
- const newRow = row + dRow;
167
- const newCol = col + dCol;
168
- if (isValidMove(newRow, newCol, piece)) {
169
- moves.push([newRow, newCol]);
170
- }
171
- }
172
- break;
173
 
174
- case 'β™™':
175
- // White pawn moves
176
- if (row > 0 && !gameBoard[row - 1][col]) {
177
- moves.push([row - 1, col]);
178
- if (row === 6 && !gameBoard[row - 2][col]) {
179
- moves.push([row - 2, col]);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
180
  }
181
- }
182
- // Capture diagonally
183
- if (row > 0 && col > 0 && isBlackPiece(gameBoard[row - 1][col - 1])) {
184
- moves.push([row - 1, col - 1]);
185
- }
186
- if (row > 0 && col < 7 && isBlackPiece(gameBoard[row - 1][col + 1])) {
187
- moves.push([row - 1, col + 1]);
188
- }
189
- break;
190
-
191
- case 'β™Ÿ':
192
- // Black pawn moves
193
- if (row < 7 && !gameBoard[row + 1][col]) {
194
- moves.push([row + 1, col]);
195
- if (row === 1 && !gameBoard[row + 2][col]) {
196
- moves.push([row + 2, col]);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
197
  }
198
- }
199
- // Capture diagonally
200
- if (row < 7 && col > 0 && isWhitePiece(gameBoard[row + 1][col - 1])) {
201
- moves.push([row + 1, col - 1]);
202
- }
203
- if (row < 7 && col < 7 && isWhitePiece(gameBoard[row + 1][col + 1])) {
204
- moves.push([row + 1, col + 1]);
205
- }
206
- break;
207
- }
208
-
209
- return moves;
210
- }
211
-
212
- function getRookMoves(row, col) {
213
- const moves = [];
214
- const directions = [[0, 1], [0, -1], [1, 0], [-1, 0]];
215
- for (const [dRow, dCol] of directions) {
216
- let newRow = row + dRow;
217
- let newCol = col + dCol;
218
- while (isValidMove(newRow, newCol, gameBoard[row][col])) {
219
- moves.push([newRow, newCol]);
220
- if (gameBoard[newRow][newCol]) break;
221
- newRow += dRow;
222
- newCol += dCol;
223
  }
224
- }
225
- return moves;
226
- }
227
 
228
- function getBishopMoves(row, col) {
229
- const moves = [];
230
- const directions = [[1, 1], [1, -1], [-1, 1], [-1, -1]];
231
- for (const [dRow, dCol] of directions) {
232
- let newRow = row + dRow;
233
- let newCol = col + dCol;
234
- while (isValidMove(newRow, newCol, gameBoard[row][col])) {
235
- moves.push([newRow, newCol]);
236
- if (gameBoard[newRow][newCol]) break;
237
- newRow += dRow;
238
- newCol += dCol;
239
  }
240
- }
241
- return moves;
242
- }
243
-
244
- function isValidMove(row, col, piece) {
245
- if (row < 0 || row > 7 || col < 0 || col > 7) return false;
246
- const targetPiece = gameBoard[row][col];
247
- if (!targetPiece) return true;
248
- return isWhitePiece(piece) ? isBlackPiece(targetPiece) : isWhitePiece(targetPiece);
249
- }
250
 
251
- function handleClick(event) {
252
- const row = parseInt(event.target.dataset.row);
253
- const col = parseInt(event.target.dataset.col);
254
- const piece = gameBoard[row][col];
255
-
256
- if (selectedPiece) {
257
- const selectedRow = parseInt(selectedPiece.dataset.row);
258
- const selectedCol = parseInt(selectedPiece.dataset.col);
259
- const possibleMoves = getPossibleMoves(selectedRow, selectedCol);
260
-
261
- const isValidMove = possibleMoves.some(([r, c]) => r === row && c === col);
262
-
263
- if (isValidMove) {
264
- const capturedPiece = gameBoard[row][col];
265
- if (capturedPiece) {
266
- if (isWhitePiece(gameBoard[selectedRow][selectedCol])) {
267
- whiteCapturedPieces.push(capturedPiece);
268
- } else {
269
- blackCapturedPieces.push(capturedPiece);
270
- }
271
- updateCapturedPieces();
272
-
273
- if (capturedPiece === 'β™š' || capturedPiece === 'β™”') {
274
- alert('Game Over! ' + (isWhitePiece(gameBoard[selectedRow][selectedCol]) ? 'White' : 'Black') + ' wins!');
275
- resetGame();
276
- return;
 
 
 
 
 
 
 
 
277
  }
278
- }
279
-
280
- gameBoard[row][col] = gameBoard[selectedRow][selectedCol];
281
- gameBoard[selectedRow][selectedCol] = '';
282
- currentTurn = currentTurn === 'white' ? 'black' : 'white';
283
  }
284
-
285
- selectedPiece = null;
286
- clearHighlights();
287
- createBoard();
288
- return;
289
- }
290
-
291
- if (!piece) return;
292
-
293
- const isWhiteTurn = currentTurn === 'white';
294
- if ((isWhiteTurn && !isWhitePiece(piece)) || (!isWhiteTurn && !isBlackPiece(piece))) {
295
- return;
296
- }
297
-
298
- selectedPiece = event.target;
299
- clearHighlights();
300
- event.target.classList.add('highlight');
301
 
302
- const possibleMoves = getPossibleMoves(row, col);
303
- possibleMoves.forEach(([r, c]) => {
304
- const square = document.querySelector(`[data-row="${r}"][data-col="${c}"]`);
305
- square.classList.add('possible-move');
306
- });
307
- }
308
-
309
- function updateCapturedPieces() {
310
- whiteCaptured.textContent = whiteCapturedPieces.join(' ');
311
- blackCaptured.textContent = blackCapturedPieces.join(' ');
312
- }
313
-
314
- function resetGame() {
315
- gameBoard = JSON.parse(JSON.stringify(initialBoard));
316
- currentTurn = 'white';
317
- whiteCapturedPieces = [];
318
- blackCapturedPieces = [];
319
- selectedPiece = null;
320
- updateCapturedPieces();
321
- createBoard();
322
- }
 
 
 
 
 
 
 
 
323
 
324
- createBoard();
325
- </script>
326
  </body>
327
  </html>
 
1
  <!DOCTYPE html>
2
  <html>
3
  <head>
4
+ <title>Chess Game</title>
5
+ <style>
6
+ body {
7
+ display: flex;
8
+ justify-content: center;
9
+ align-items: center;
10
+ height: 100vh;
11
+ margin: 0;
12
+ background: #f0f0f0;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  }
 
 
14
 
15
+ #chessboard {
16
+ display: grid;
17
+ grid-template-columns: repeat(8, 60px);
18
+ grid-template-rows: repeat(8, 60px);
19
+ border: 2px solid #333;
20
+ }
21
 
22
+ .square {
23
+ width: 60px;
24
+ height: 60px;
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 {
34
+ background-color: #EEEED2;
35
+ }
 
 
36
 
37
+ .black {
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>
72
+ <body>
73
+ <div id="chessboard"></div>
74
+
75
+ <script>
76
+ let selectedPiece = null;
77
+ let currentTurn = 'white';
78
+
79
+ const pieces = {
80
+ 'white': {
81
+ 'king': 'β™”',
82
+ 'queen': 'β™•',
83
+ 'rook': 'β™–',
84
+ 'bishop': 'β™—',
85
+ 'knight': 'β™˜',
86
+ 'pawn': 'β™™'
87
+ },
88
+ 'black': {
89
+ 'king': 'β™š',
90
+ 'queen': 'β™›',
91
+ 'rook': 'β™œ',
92
+ 'bishop': '♝',
93
+ 'knight': 'β™ž',
94
+ 'pawn': 'β™Ÿ'
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);
130
+ board.appendChild(square);
131
+ }
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>
211
  </html>