Spaces:
Running
Running
File size: 6,296 Bytes
073c4dc 7ad8ecd 073c4dc 7ad8ecd f45f133 7ad8ecd 54b2acd 7ad8ecd f45f133 4ebde63 7ad8ecd f45f133 4ebde63 1d5467d 4ebde63 1d5467d 7ad8ecd 4ebde63 7ad8ecd f45f133 7ad8ecd f45f133 073c4dc f45f133 7ad8ecd 1d5467d 54b2acd 1d5467d 54b2acd 1d5467d 4ebde63 f45f133 1d5467d 4ebde63 54b2acd 1d5467d f45f133 7ad8ecd 1d5467d 073c4dc f45f133 7ad8ecd 54b2acd 7ad8ecd 54b2acd 7ad8ecd f45f133 1d5467d 3dd7884 7ad8ecd 1d5467d 7ad8ecd 4ebde63 54b2acd 3dd7884 1d5467d 4ebde63 54b2acd 1d5467d 7ad8ecd 3dd7884 1d5467d f45f133 073c4dc 54b2acd 4ebde63 3dd7884 4ebde63 54b2acd 4ebde63 54b2acd 4ebde63 f45f133 073c4dc 4ebde63 3dd7884 54b2acd 4ebde63 54b2acd 3dd7884 54b2acd 4ebde63 1d5467d 7ad8ecd 2fcf273 54b2acd f45f133 073c4dc 7ad8ecd |
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 |
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Chess Game</title>
<style>
:root {
--board-size: min(80vh, 600px);
--square-size: calc(var(--board-size) / 8);
--primary-dark: #2c3e50;
--primary-light: #34495e;
--highlight: #f1c40f;
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: Arial, sans-serif;
background: var(--primary-dark);
color: #ecf0f1;
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
}
.container {
display: grid;
grid-template-columns: var(--board-size) 300px;
gap: 2rem;
background: var(--primary-light);
padding: 2rem;
border-radius: 1rem;
box-shadow: 0 10px 20px rgba(0,0,0,0.2);
}
.chessboard {
width: var(--board-size);
height: var(--board-size);
display: grid;
grid-template-columns: repeat(8, 1fr);
grid-template-rows: repeat(8, 1fr);
}
.square {
display: flex;
align-items: center;
justify-content: center;
font-size: calc(var(--square-size) * 0.7);
}
.white { background: #f0d9b5; }
.black { background: #b58863; }
.selected { background: var(--highlight); }
.piece {
font-size: calc(var(--square-size) * 0.7);
user-select: none;
}
.piece.white { color: #fff; }
.piece.black { color: #000; }
</style>
</head>
<body>
<div class="container">
<div class="chessboard" id="board"></div>
<div>
<h3>Position Analysis</h3>
<p id="status">White to move</p>
<button onclick="resetBoard()">Reset Board</button>
</div>
</div>
<script>
class ChessGame {
constructor() {
this.board = this.createInitialBoard();
this.currentPlayer = 'white';
this.selectedPiece = null;
this.initializeBoard();
}
createInitialBoard() {
return [
['β', 'β', 'β', 'β', 'β', 'β', 'β', 'β'],
['β', 'β', 'β', 'β', 'β', 'β', 'β', 'β'],
[null, null, null, null, null, null, null, null],
[null, null, null, null, null, null, null, null],
[null, null, null, null, null, null, null, null],
[null, null, null, null, null, null, null, null],
['β', 'β', 'β', 'β', 'β', 'β', 'β', 'β'],
['β', 'β', 'β', 'β', 'β', 'β', 'β', 'β']
];
}
initializeBoard() {
const boardElement = document.getElementById('board');
boardElement.innerHTML = '';
for (let row = 0; row < 8; row++) {
for (let col = 0; col < 8; col++) {
const square = document.createElement('div');
square.className = `square ${(row + col) % 2 === 0 ? 'white' : 'black'}`;
square.dataset.row = row;
square.dataset.col = col;
square.onclick = () => this.handleSquareClick(row, col);
const piece = this.board[row][col];
if (piece) {
const pieceElement = document.createElement('div');
pieceElement.className = `piece ${row < 2 ? 'black' : 'white'}`;
pieceElement.textContent = piece;
square.appendChild(pieceElement);
}
boardElement.appendChild(square);
}
}
}
handleSquareClick(row, col) {
const square = document.querySelector(`[data-row="${row}"][data-col="${col}"]`);
const piece = this.board[row][col];
const pieceColor = row < 2 ? 'black' : row > 5 ? 'white' : null;
if (this.selectedPiece) {
this.tryMove(this.selectedPiece.row, this.selectedPiece.col, row, col);
this.selectedPiece = null;
document.querySelectorAll('.square').forEach(sq => sq.classList.remove('selected'));
} else if (piece && pieceColor === this.currentPlayer) {
this.selectedPiece = { row, col };
document.querySelectorAll('.square').forEach(sq => sq.classList.remove('selected'));
square.classList.add('selected');
}
}
tryMove(fromRow, fromCol, toRow, toCol) {
const targetPiece = this.board[toRow][toCol];
const targetColor = toRow < 2 ? 'black' : toRow > 5 ? 'white' : null;
if (targetColor !== this.currentPlayer) {
this.movePiece(fromRow, fromCol, toRow, toCol);
}
}
movePiece(fromRow, fromCol, toRow, toCol) {
this.board[toRow][toCol] = this.board[fromRow][fromCol];
this.board[fromRow][fromCol] = null;
this.currentPlayer = this.currentPlayer === 'white' ? 'black' : 'white';
document.getElementById('status').textContent =
`${this.currentPlayer.charAt(0).toUpperCase() + this.currentPlayer.slice(1)} to move`;
this.initializeBoard();
}
}
const game = new ChessGame();
function resetBoard() {
game.board = game.createInitialBoard();
game.currentPlayer = 'white';
game.initializeBoard();
document.getElementById('status').textContent = 'White to move';
}
</script>
</body>
</html>
|