Changed around line 1
+ class ChessGame {
+ constructor() {
+ this.board = this.createBoard();
+ this.currentPlayer = 'white';
+ this.selectedPiece = null;
+ this.moves = [];
+ this.initializeBoard();
+ this.setupEventListeners();
+ }
+
+ createBoard() {
+ const board = document.getElementById('chessboard');
+ const squares = [];
+ for (let row = 0; row < 8; row++) {
+ squares[row] = [];
+ for (let col = 0; col < 8; col++) {
+ const square = document.createElement('div');
+ square.className = `square ${(row + col) % 2 ? 'dark' : 'light'}`;
+ square.dataset.row = row;
+ square.dataset.col = col;
+ board.appendChild(square);
+ squares[row][col] = square;
+ }
+ }
+ return squares;
+ }
+
+ initializeBoard() {
+ const pieces = {
+ white: ['♜', '♞', '♝', '♛', '♚', '♝', '♞', '♜'],
+ black: ['♖', '♘', '♗', '♕', '♔', '♗', '♘', '♖']
+ };
+
+ // Place pieces
+ for (let col = 0; col < 8; col++) {
+ this.placePiece(0, col, pieces.black[col], 'black');
+ this.placePiece(1, col, '♙', 'black');
+ this.placePiece(6, col, '♟', 'white');
+ this.placePiece(7, col, pieces.white[col], 'white');
+ }
+ }
+
+ placePiece(row, col, symbol, color) {
+ const piece = document.createElement('div');
+ piece.className = `piece ${color}`;
+ piece.textContent = symbol;
+ piece.dataset.color = color;
+ this.board[row][col].appendChild(piece);
+ }
+
+ setupEventListeners() {
+ document.getElementById('chessboard').addEventListener('click', (e) => {
+ const square = e.target.closest('.square');
+ if (!square) return;
+
+ const piece = square.querySelector('.piece');
+ if (this.selectedPiece) {
+ if (piece && piece.dataset.color === this.currentPlayer) {
+ this.selectPiece(piece);
+ } else {
+ this.movePiece(square);
+ }
+ } else if (piece && piece.dataset.color === this.currentPlayer) {
+ this.selectPiece(piece);
+ }
+ });
+
+ document.getElementById('newGameBtn').addEventListener('click', () => {
+ this.resetGame();
+ });
+
+ document.getElementById('undoBtn').addEventListener('click', () => {
+ this.undoMove();
+ });
+ }
+
+ selectPiece(piece) {
+ if (this.selectedPiece) {
+ this.selectedPiece.parentElement.classList.remove('highlight');
+ }
+ this.selectedPiece = piece;
+ piece.parentElement.classList.add('highlight');
+ }
+
+ movePiece(targetSquare) {
+ const sourceSquare = this.selectedPiece.parentElement;
+ if (sourceSquare === targetSquare) return;
+
+ this.moves.push({
+ from: sourceSquare.cloneNode(true),
+ to: targetSquare.cloneNode(true)
+ });
+
+ targetSquare.innerHTML = '';
+ targetSquare.appendChild(this.selectedPiece);
+ sourceSquare.classList.remove('highlight');
+ this.selectedPiece = null;
+ this.currentPlayer = this.currentPlayer === 'white' ? 'black' : 'white';
+ this.updateGameStatus();
+ }
+
+ undoMove() {
+ if (this.moves.length === 0) return;
+ const lastMove = this.moves.pop();
+ const fromSquare = this.board[lastMove.from.dataset.row][lastMove.from.dataset.col];
+ const toSquare = this.board[lastMove.to.dataset.row][lastMove.to.dataset.col];
+
+ fromSquare.innerHTML = lastMove.from.innerHTML;
+ toSquare.innerHTML = lastMove.to.innerHTML;
+ this.currentPlayer = this.currentPlayer === 'white' ? 'black' : 'white';
+ this.updateGameStatus();
+ }
+
+ resetGame() {
+ for (let row = 0; row < 8; row++) {
+ for (let col = 0; col < 8; col++) {
+ this.board[row][col].innerHTML = '';
+ }
+ }
+ this.currentPlayer = 'white';
+ this.selectedPiece = null;
+ this.moves = [];
+ this.initializeBoard();
+ this.updateGameStatus();
+ }
+
+ updateGameStatus() {
+ document.getElementById('gameStatus').textContent =
+ `${this.currentPlayer.charAt(0).toUpperCase() + this.currentPlayer.slice(1)}'s turn`;
+ }
+ }
+
+ document.addEventListener('DOMContentLoaded', () => {
+ new ChessGame();
+ });