Spaces:
Running
Running
Update index.html
Browse files- index.html +185 -301
index.html
CHANGED
@@ -1,327 +1,211 @@
|
|
1 |
<!DOCTYPE html>
|
2 |
<html>
|
3 |
<head>
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
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 |
-
|
106 |
-
|
107 |
-
|
|
|
|
|
|
|
108 |
|
109 |
-
|
110 |
-
|
111 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
112 |
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
});
|
117 |
-
}
|
118 |
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
|
123 |
-
|
124 |
-
|
125 |
-
|
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 |
-
|
140 |
-
|
141 |
-
|
142 |
-
moves.push(...getRookMoves(row, col));
|
143 |
-
moves.push(...getBishopMoves(row, col));
|
144 |
-
break;
|
145 |
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
|
|
|
|
|
|
151 |
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
moves.push(...getBishopMoves(row, col));
|
156 |
-
break;
|
157 |
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
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 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
180 |
}
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
-
|
229 |
-
|
230 |
-
|
231 |
-
|
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 |
-
|
252 |
-
|
253 |
-
|
254 |
-
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
|
260 |
-
|
261 |
-
|
262 |
-
|
263 |
-
|
264 |
-
|
265 |
-
|
266 |
-
|
267 |
-
|
268 |
-
|
269 |
-
|
270 |
-
|
271 |
-
|
272 |
-
|
273 |
-
|
274 |
-
|
275 |
-
|
276 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
-
|
303 |
-
|
304 |
-
|
305 |
-
|
306 |
-
|
307 |
-
|
308 |
-
|
309 |
-
|
310 |
-
|
311 |
-
|
312 |
-
|
313 |
-
|
314 |
-
|
315 |
-
|
316 |
-
|
317 |
-
|
318 |
-
|
319 |
-
|
320 |
-
|
321 |
-
|
322 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
323 |
|
324 |
-
|
325 |
-
|
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>
|