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

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +173 -224
index.html CHANGED
@@ -1,275 +1,224 @@
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
- position: relative;
 
29
  cursor: pointer;
 
 
 
 
 
 
 
 
 
 
30
  }
31
 
32
- .white {
33
- background-color: #EEEED2;
 
 
 
34
  }
35
 
36
- .black {
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>
54
  <body>
55
- <div id="chessboard"></div>
 
 
 
 
 
 
 
 
 
 
 
 
56
 
57
  <script>
58
- const pieces = {
59
- 'white': {
60
- 'king': '♔',
61
- 'queen': '',
62
- 'rook': '♖',
63
- 'bishop': '♗',
64
- 'knight': '♘',
65
- 'pawn': '♙'
66
- },
67
- 'black': {
68
- 'king': '♚',
69
- 'queen': '♛',
70
- 'rook': '♜',
71
- 'bishop': '♝',
72
- 'knight': '♞',
73
- 'pawn': '♟'
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);
99
- board.appendChild(square);
100
  }
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>
275
- </html>
 
1
  <!DOCTYPE html>
2
+ <html lang="en">
3
  <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
  <title>Chess Game</title>
7
  <style>
8
+ :root {
9
+ --board-size: min(80vh, 600px);
10
+ --square-size: calc(var(--board-size) / 8);
11
+ --primary-dark: #2c3e50;
12
+ --primary-light: #34495e;
13
+ --highlight: #f1c40f;
14
+ --move-highlight: rgba(46, 204, 113, 0.4);
15
+ --check-highlight: rgba(231, 76, 60, 0.4);
16
+ }
17
+
18
+ * {
19
+ margin: 0;
20
+ padding: 0;
21
+ box-sizing: border-box;
22
+ }
23
+
24
  body {
25
+ font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Arial, sans-serif;
26
+ background: var(--primary-dark);
27
+ color: #ecf0f1;
28
+ min-height: 100vh;
29
  display: flex;
 
 
 
 
 
30
  }
31
 
32
+ .container {
33
+ display: grid;
34
+ grid-template-columns: var(--board-size) 300px;
35
+ gap: 2rem;
36
+ margin: auto;
37
+ padding: 2rem;
38
+ background: var(--primary-light);
39
+ border-radius: 1rem;
40
+ box-shadow: 0 10px 20px rgba(0,0,0,0.2);
41
+ }
42
+
43
+ .board-container {
44
+ width: var(--board-size);
45
+ height: var(--board-size);
46
+ position: relative;
47
+ }
48
+
49
+ .chessboard {
50
+ width: 100%;
51
+ height: 100%;
52
  display: grid;
53
+ grid-template-columns: repeat(8, 1fr);
54
+ grid-template-rows: repeat(8, 1fr);
55
+ border: 4px solid #2c3e50;
56
+ border-radius: 4px;
57
+ overflow: hidden;
58
  }
59
 
60
  .square {
 
 
61
  display: flex;
 
62
  align-items: center;
63
+ justify-content: center;
64
+ font-size: calc(var(--square-size) * 0.7);
65
  cursor: pointer;
66
+ transition: all 0.2s;
67
+ position: relative;
68
+ user-select: none;
69
+ }
70
+
71
+ .white { background: #f0d9b5; }
72
+ .black { background: #b58863; }
73
+
74
+ .square.selected {
75
+ background: var(--highlight) !important;
76
  }
77
 
78
+ .square.valid-move::after {
79
+ content: '•';
80
+ position: absolute;
81
+ color: rgba(0,0,0,0.3);
82
+ font-size: 2em;
83
  }
84
 
85
+ .square.attack-move::after {
86
+ content: '';
87
+ position: absolute;
88
+ width: 100%;
89
+ height: 100%;
90
+ border: 2px solid rgba(231, 76, 60, 0.6);
91
+ border-radius: 50%;
92
  }
93
 
94
  .piece {
95
+ width: 100%;
96
+ height: 100%;
97
+ display: flex;
98
+ align-items: center;
99
+ justify-content: center;
100
+ font-size: calc(var(--square-size) * 0.7);
101
+ cursor: grab;
102
+ color: #000;
103
  }
104
 
105
+ .piece.white { color: #fff; }
 
 
106
 
107
+ @media (max-width: 1024px) {
108
+ .container {
109
+ grid-template-columns: 1fr;
110
+ grid-template-rows: auto auto;
111
+ }
112
+
113
+ .board-container {
114
+ margin: auto;
115
+ }
116
  }
117
  </style>
118
  </head>
119
  <body>
120
+ <div class="container">
121
+ <div class="board-container">
122
+ <div class="chessboard" id="board"></div>
123
+ </div>
124
+ <div class="controls">
125
+ <div class="status-panel">
126
+ <h3>Position Analysis</h3>
127
+ <p id="status">White to move</p>
128
+ <p id="evaluation">Evaluation: 0.0</p>
129
+ </div>
130
+ <button onclick="resetBoard()">Reset Board</button>
131
+ </div>
132
+ </div>
133
 
134
  <script>
135
+ class ChessGame {
136
+ constructor() {
137
+ this.board = this.createInitialBoard();
138
+ this.currentPlayer = 'white';
139
+ this.selectedPiece = null;
140
+ this.initializeBoard();
 
 
 
 
 
 
 
 
 
 
141
  }
 
142
 
143
+ createInitialBoard() {
144
+ return [
145
+ ['♜', '♞', '♝', '♛', '♚', '♝', '♞', '♜'],
146
+ ['♟', '♟', '♟', '♟', '♟', '♟', '♟', '♟'],
147
+ [null, null, null, null, null, null, null, null],
148
+ [null, null, null, null, null, null, null, null],
149
+ [null, null, null, null, null, null, null, null],
150
+ [null, null, null, null, null, null, null, null],
151
+ ['', '♙', '♙', '♙', '♙', '♙', '', ''],
152
+ ['♖', '♘', '♗', '♕', '♔', '♗', '♘', '♖']
153
+ ];
154
+ }
155
 
156
+ initializeBoard() {
157
+ const boardElement = document.getElementById('board');
158
+ boardElement.innerHTML = '';
159
+ for (let row = 0; row < 8; row++) {
160
+ for (let col = 0; col < 8; col++) {
161
+ const square = document.createElement('div');
162
+ square.className = `square ${(row + col) % 2 === 0 ? 'white' : 'black'}`;
163
+ square.dataset.row = row;
164
+ square.dataset.col = col;
165
+ square.onclick = (e) => this.handleSquareClick(e);
166
+ if (this.board[row][col]) {
167
+ const piece = document.createElement('div');
168
+ piece.className = 'piece';
169
+ piece.textContent = this.board[row][col];
170
+ square.appendChild(piece);
171
+ }
172
+ boardElement.appendChild(square);
173
  }
 
 
 
174
  }
175
  }
 
176
 
177
+ handleSquareClick(event) {
178
+ const square = event.target.closest('.square');
179
+ const row = parseInt(square.dataset.row);
180
+ const col = parseInt(square.dataset.col);
181
+ const piece = this.board[row][col];
182
+ const isWhitePiece = '♔♕♖♗♘♙'.includes(piece);
183
+
184
+ if (piece && ((this.currentPlayer === 'white' && isWhitePiece) ||
185
+ (this.currentPlayer === 'black' && !isWhitePiece))) {
186
+ this.selectPiece(row, col);
187
+ } else if (this.selectedPiece) {
188
+ this.tryMove(row, col);
 
 
 
 
 
 
 
 
 
 
189
  }
 
 
 
190
  }
 
191
 
192
+ selectPiece(row, col) {
193
+ document.querySelectorAll('.square').forEach(sq => sq.classList.remove('selected', 'valid-move', 'attack-move'));
194
+ document.querySelector(`[data-row="${row}"][data-col="${col}"]`).classList.add('selected');
195
+ this.selectedPiece = { row, col };
196
+ this.showValidMoves(row, col);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
197
  }
 
198
 
199
+ tryMove(toRow, toCol) {
200
+ const moves = this.getPieceMoves(this.selectedPiece.row, this.selectedPiece.col);
201
+ const isValidMove = moves.some(move => move.row === toRow && move.col === toCol);
202
 
203
+ if (isValidMove) {
204
+ this.makeMove(this.selectedPiece.row, this.selectedPiece.col, toRow, toCol);
 
 
 
 
205
  }
206
+ document.querySelectorAll('.square').forEach(sq => sq.classList.remove('selected', 'valid-move', 'attack-move'));
207
+ this.selectedPiece = null;
208
  }
209
 
210
+ makeMove(fromRow, fromCol, toRow, toCol) {
211
+ const piece = this.board[fromRow][fromCol];
212
+ this.board[fromRow][fromCol] = null;
213
+ this.board[toRow][toCol] = piece;
214
+ this.currentPlayer = this.currentPlayer === 'white' ? 'black' : 'white';
215
+ document.getElementById('status').textContent = `${this.currentPlayer} to move`;
216
+ this.initializeBoard();
217
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
218
  }
219
 
220
+ const game = new ChessGame();
221
+ function resetBoard() { new ChessGame(); }
222
  </script>
223
  </body>
224
+ </html>