Chess Game
Chess Game
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>
* {
margin: 0;
padding: 0;
}
body {
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
background-color: #f0f0f0;
}
.chess-board {
display: grid;
grid-template-columns: repeat(8, 80px);
grid-template-rows: repeat(8, 80px);
border: 2px solid black;
}
.cells {
width: 80px;
height: 80px;
display: flex;
justify-content: center;
align-items: center;
font-size: 30px;
cursor: pointer;
}
.black {
background-color: black;
color: white;
}
.white {
background-color: white;
}
.selected {
border: 2px solid red;
}
</style>
</head>
<body>
<div class="chess-board" id="chess-board"></div>
<script>
const pieces = {
'black': {
'king': '♚',
'queen': '♛',
'rook': '♜',
'bishop': '♝',
'knight': '♞',
'pawn': '♟︎'
},
'white': {
'king': '♔',
'queen': '♕',
'rook': '♖',
'bishop': '♗',
'knight': '♘',
'pawn': '♙'
}
};
function drawBoard() {
chessboard.innerHTML = ''; // Clear the board before redrawing
boardState.forEach((row, x) => {
row.forEach((piece, y) => {
const square = document.createElement('div');
square.classList.add('cells', (x + y) % 2 === 0 ? 'white' :
'black');
square.dataset.pos = `${x},${y}`;
square.textContent = getPieceSymbol(piece);
chessboard.appendChild(square);
});
});
}
function clearSelection() {
if (selectedSquare) {
selectedSquare.classList.remove('selected');
selectedSquare = null;
}
}
if (selectedSquare) {
// If a square is already selected, attempt to move the piece
const selectedPos =
selectedSquare.dataset.pos.split(',').map(Number);
const selectedX = selectedPos[0];
const selectedY = selectedPos[1];
const selectedPiece = boardState[selectedX][selectedY];
chessboard.addEventListener('click', handleSquareClick);
</html>