Spaces:
Running
Running
| <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> | |