Spaces:
Running
Running
File size: 9,415 Bytes
073c4dc 52b4b86 073c4dc |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 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 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 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 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 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 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 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 327 |
<!DOCTYPE html>
<html>
<head>
<style>
.board {
width: 480px;
height: 480px;
border: 2px solid #333;
display: grid;
grid-template-columns: repeat(8, 1fr);
}
.square {
width: 60px;
height: 60px;
display: flex;
justify-content: center;
align-items: center;
font-size: 40px;
cursor: pointer;
position: relative;
}
.white {
background: #fff;
}
.black {
background: #999;
}
.highlight {
background: rgba(255, 255, 0, 0.5);
}
.possible-move::after {
content: '';
position: absolute;
width: 20px;
height: 20px;
background: rgba(0, 255, 0, 0.3);
border-radius: 50%;
}
.captured {
margin: 20px;
padding: 10px;
border: 1px solid #ccc;
}
.captured-piece {
font-size: 24px;
margin: 0 5px;
}
</style>
</head>
<body>
<div class="board" id="board"></div>
<div class="captured">
<div>
White captured: <span id="whiteCaptured"></span>
</div>
<div>
Black captured: <span id="blackCaptured"></span>
</div>
</div>
<script>
const board = document.getElementById('board');
const whiteCaptured = document.getElementById('whiteCaptured');
const blackCaptured = document.getElementById('blackCaptured');
let selectedPiece = null;
let currentTurn = 'white';
let whiteCapturedPieces = [];
let blackCapturedPieces = [];
const initialBoard = [
['β', 'β', 'β', 'β', 'β', 'β', 'β', 'β'],
['β', 'β', 'β', 'β', 'β', 'β', 'β', 'β'],
['', '', '', '', '', '', '', ''],
['', '', '', '', '', '', '', ''],
['', '', '', '', '', '', '', ''],
['', '', '', '', '', '', '', ''],
['β', 'β', 'β', 'β', 'β', 'β', 'β', 'β'],
['β', 'β', 'β', 'β', 'β', 'β', 'β', 'β']
];
let gameBoard = JSON.parse(JSON.stringify(initialBoard));
function createBoard() {
board.innerHTML = '';
for (let i = 0; i < 8; i++) {
for (let j = 0; j < 8; j++) {
const square = document.createElement('div');
square.className = `square ${(i + j) % 2 === 0 ? 'white' : 'black'}`;
square.dataset.row = i;
square.dataset.col = j;
square.textContent = gameBoard[i][j];
square.addEventListener('click', handleClick);
board.appendChild(square);
}
}
}
function isWhitePiece(piece) {
return 'ββββββ'.includes(piece);
}
function isBlackPiece(piece) {
return 'ββββββ'.includes(piece);
}
function clearHighlights() {
document.querySelectorAll('.square').forEach(square => {
square.classList.remove('highlight', 'possible-move');
});
}
function getPossibleMoves(row, col) {
const piece = gameBoard[row][col];
const moves = [];
switch (piece) {
case 'β':
case 'β':
// King moves
for (let i = -1; i <= 1; i++) {
for (let j = -1; j <= 1; j++) {
if (i === 0 && j === 0) continue;
const newRow = row + i;
const newCol = col + j;
if (isValidMove(newRow, newCol, piece)) {
moves.push([newRow, newCol]);
}
}
}
break;
case 'β':
case 'β':
// Queen moves (combination of rook and bishop)
moves.push(...getRookMoves(row, col));
moves.push(...getBishopMoves(row, col));
break;
case 'β':
case 'β':
// Rook moves
moves.push(...getRookMoves(row, col));
break;
case 'β':
case 'β':
// Bishop moves
moves.push(...getBishopMoves(row, col));
break;
case 'β':
case 'β':
// Knight moves
const knightMoves = [
[-2, -1], [-2, 1], [-1, -2], [-1, 2],
[1, -2], [1, 2], [2, -1], [2, 1]
];
for (const [dRow, dCol] of knightMoves) {
const newRow = row + dRow;
const newCol = col + dCol;
if (isValidMove(newRow, newCol, piece)) {
moves.push([newRow, newCol]);
}
}
break;
case 'β':
// White pawn moves
if (row > 0 && !gameBoard[row - 1][col]) {
moves.push([row - 1, col]);
if (row === 6 && !gameBoard[row - 2][col]) {
moves.push([row - 2, col]);
}
}
// Capture diagonally
if (row > 0 && col > 0 && isBlackPiece(gameBoard[row - 1][col - 1])) {
moves.push([row - 1, col - 1]);
}
if (row > 0 && col < 7 && isBlackPiece(gameBoard[row - 1][col + 1])) {
moves.push([row - 1, col + 1]);
}
break;
case 'β':
// Black pawn moves
if (row < 7 && !gameBoard[row + 1][col]) {
moves.push([row + 1, col]);
if (row === 1 && !gameBoard[row + 2][col]) {
moves.push([row + 2, col]);
}
}
// Capture diagonally
if (row < 7 && col > 0 && isWhitePiece(gameBoard[row + 1][col - 1])) {
moves.push([row + 1, col - 1]);
}
if (row < 7 && col < 7 && isWhitePiece(gameBoard[row + 1][col + 1])) {
moves.push([row + 1, col + 1]);
}
break;
}
return moves;
}
function getRookMoves(row, col) {
const moves = [];
const directions = [[0, 1], [0, -1], [1, 0], [-1, 0]];
for (const [dRow, dCol] of directions) {
let newRow = row + dRow;
let newCol = col + dCol;
while (isValidMove(newRow, newCol, gameBoard[row][col])) {
moves.push([newRow, newCol]);
if (gameBoard[newRow][newCol]) break;
newRow += dRow;
newCol += dCol;
}
}
return moves;
}
function getBishopMoves(row, col) {
const moves = [];
const directions = [[1, 1], [1, -1], [-1, 1], [-1, -1]];
for (const [dRow, dCol] of directions) {
let newRow = row + dRow;
let newCol = col + dCol;
while (isValidMove(newRow, newCol, gameBoard[row][col])) {
moves.push([newRow, newCol]);
if (gameBoard[newRow][newCol]) break;
newRow += dRow;
newCol += dCol;
}
}
return moves;
}
function isValidMove(row, col, piece) {
if (row < 0 || row > 7 || col < 0 || col > 7) return false;
const targetPiece = gameBoard[row][col];
if (!targetPiece) return true;
return isWhitePiece(piece) ? isBlackPiece(targetPiece) : isWhitePiece(targetPiece);
}
function handleClick(event) {
const row = parseInt(event.target.dataset.row);
const col = parseInt(event.target.dataset.col);
const piece = gameBoard[row][col];
if (selectedPiece) {
const selectedRow = parseInt(selectedPiece.dataset.row);
const selectedCol = parseInt(selectedPiece.dataset.col);
const possibleMoves = getPossibleMoves(selectedRow, selectedCol);
const isValidMove = possibleMoves.some(([r, c]) => r === row && c === col);
if (isValidMove) {
const capturedPiece = gameBoard[row][col];
if (capturedPiece) {
if (isWhitePiece(gameBoard[selectedRow][selectedCol])) {
whiteCapturedPieces.push(capturedPiece);
} else {
blackCapturedPieces.push(capturedPiece);
}
updateCapturedPieces();
if (capturedPiece === 'β' || capturedPiece === 'β') {
alert('Game Over! ' + (isWhitePiece(gameBoard[selectedRow][selectedCol]) ? 'White' : 'Black') + ' wins!');
resetGame();
return;
}
}
gameBoard[row][col] = gameBoard[selectedRow][selectedCol];
gameBoard[selectedRow][selectedCol] = '';
currentTurn = currentTurn === 'white' ? 'black' : 'white';
}
selectedPiece = null;
clearHighlights();
createBoard();
return;
}
if (!piece) return;
const isWhiteTurn = currentTurn === 'white';
if ((isWhiteTurn && !isWhitePiece(piece)) || (!isWhiteTurn && !isBlackPiece(piece))) {
return;
}
selectedPiece = event.target;
clearHighlights();
event.target.classList.add('highlight');
const possibleMoves = getPossibleMoves(row, col);
possibleMoves.forEach(([r, c]) => {
const square = document.querySelector(`[data-row="${r}"][data-col="${c}"]`);
square.classList.add('possible-move');
});
}
function updateCapturedPieces() {
whiteCaptured.textContent = whiteCapturedPieces.join(' ');
blackCaptured.textContent = blackCapturedPieces.join(' ');
}
function resetGame() {
gameBoard = JSON.parse(JSON.stringify(initialBoard));
currentTurn = 'white';
whiteCapturedPieces = [];
blackCapturedPieces = [];
selectedPiece = null;
updateCapturedPieces();
createBoard();
}
createBoard();
</script>
</body>
</html> |