Sure! Below is a fully functional checkers game implemented using HTML, CSS, and JavaScript. This game allows two players to play checkers in a web browser. The code includes the game board setup, piece movement logic (including king pieces), capturing mechanics, turn-based gameplay, and win condition checking.
html Copy code
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Checkers Game</title>
<style>
body {
font-family: Arial, sans-serif;
text-align: center;
}
#board {
display: grid;
grid-template-columns: repeat(8, 60px);
grid-template-rows: repeat(8, 60px);
width: 480px;
height: 480px;
margin: 20px auto;
border: 2px solid #333;
}
.square {
width: 60px;
height: 60px;
box-sizing: border-box;
}
.light {
background-color: #f0d9b5;
}
.dark {
background-color: #b58863;
}
.piece {
width: 50px;
height: 50px;
border-radius: 50%;
margin: 5px auto;
position: relative;
cursor: pointer;
}
.red {
background-color: red;
}
.black {
background-color: black;
}
.king:after {
content: "K";
color: white;
font-weight: bold;
position: absolute;
top: 12px;
left: 22px;
font-size: 24px;
}
.selected {
outline: 3px solid yellow;
}
#turnIndicator {
margin-top: 20px;
font-size: 24px;
}
#winner {
margin-top: 20px;
font-size: 32px;
color: green;
}
</style>
</head>
<body>
<h1>Checkers Game</h1>
<div id="board"></div>
<div id="turnIndicator">Current Turn: <span id="currentTurn">Red</span></div>
<div id="winner"></div>
<script>
const board = document.getElementById('board');
const currentTurnDisplay = document.getElementById('currentTurn');
const winnerDisplay = document.getElementById('winner');
let selectedPiece = null;
let turn = 'red';
let pieces = [];
let forcedCaptures = [];
function createBoard() {
for (let row = 0; row < 8; row++) {
for (let col = 0; col < 8; col++) {
const square = document.createElement('div');
square.classList.add('square');
square.id = 'square-' + row + '-' + col;
if ((row + col) % 2 === 0) {
square.classList.add('light');
} else {
square.classList.add('dark');
if (row < 3) {
createPiece(square, 'black');
} else if (row > 4) {
createPiece(square, 'red');
}
}
board.appendChild(square);
}
}
}
function createPiece(square, color) {
const piece = document.createElement('div');
piece.classList.add('piece', color);
piece.dataset.color = color;
piece.dataset.king = 'false';
piece.addEventListener('click', selectPiece);
square.appendChild(piece);
pieces.push(piece);
}
function selectPiece(e) {
const piece = e.target;
if (piece.dataset.color !== turn || winnerDisplay.textContent !== '') {
return;
}
if (selectedPiece) {
selectedPiece.classList.remove('selected');
}
selectedPiece = piece;
selectedPiece.classList.add('selected');
showValidMoves();
e.stopPropagation(); // Prevent the square's click event
}
function showValidMoves() {
removeHighlights();
forcedCaptures = getForcedCaptures();
if (forcedCaptures.length > 0 && !forcedCaptures.includes(selectedPiece)) {
alert('You must capture with one of the highlighted pieces.');
selectedPiece.classList.remove('selected');
selectedPiece = null;
return;
}
const validMoves = getValidMoves(selectedPiece);
validMoves.forEach(move => {
const square = document.getElementById('square-' + move.row + '-' + move.col);
square.classList.add('highlight');
square.addEventListener('click', movePiece);
});
}
function getForcedCaptures() {
const forced = [];
pieces.forEach(piece => {
if (piece.dataset.color === turn) {
const captures = getValidMoves(piece, true);
if (captures.length > 0) {
forced.push(piece);
}
}
});
return forced;
}
function getValidMoves(piece, onlyCaptures = false) {
const moves = [];
const currentSquare = piece.parentElement;
const position = currentSquare.id.split('-');
const row = parseInt(position[1]);
const col = parseInt(position[2]);
const directions = getMoveDirections(piece);
directions.forEach(direction => {
const [dx, dy] = direction;
const newRow = row + dy;
const newCol = col + dx;
if (newRow >= 0 && newRow < 8 && newCol >= 0 && newCol < 8) {
const targetSquare = document.getElementById('square-' + newRow + '-' + newCol);
if (targetSquare.children.length === 0 && !onlyCaptures) {
moves.push({ row: newRow, col: newCol, captures: [] });
} else if (targetSquare.children.length > 0) {
const targetPiece = targetSquare.children[0];
if (targetPiece.dataset.color !== piece.dataset.color) {
const jumpRow = newRow + dy;
const jumpCol = newCol + dx;
if (jumpRow >= 0 && jumpRow < 8 && jumpCol >= 0 && jumpCol < 8) {
const jumpSquare = document.getElementById('square-' + jumpRow + '-' + jumpCol);
if (jumpSquare.children.length === 0) {
moves.push({
row: jumpRow,
col: jumpCol,
captures: [targetPiece]
});
}
}
}
}
}
});
if (onlyCaptures) {
return moves.filter(move => move.captures.length > 0);
} else {
return moves;
}
}
function getMoveDirections(piece) {
const isKing = piece.dataset.king === 'true';
const directions = [];
if (piece.dataset.color === 'red' || isKing) {
directions.push([-1, -1], [1, -1]); // Up-left, Up-right
}
if (piece.dataset.color === 'black' || isKing) {
directions.push([-1, 1], [1, 1]); // Down-left, Down-right
}
return directions;
}
function movePiece(e) {
const targetSquare = e.currentTarget;
const currentSquare = selectedPiece.parentElement;
const position = currentSquare.id.split('-');
const row = parseInt(position[1]);
const col = parseInt(position[2]);
const moves = getValidMoves(selectedPiece);
const move = moves.find(m => m.row === parseInt(targetSquare.id.split('-')[1]) &&
m.col === parseInt(targetSquare.id.split('-')[2]));
if (!move) {
alert('Invalid move');
return;
}
// Move piece
targetSquare.appendChild(selectedPiece);
// Remove captured pieces
if (move.captures.length > 0) {
move.captures.forEach(capturedPiece => {
const index = pieces.indexOf(capturedPiece);
if (index > -1) {
pieces.splice(index, 1);
}
capturedPiece.parentElement.removeChild(capturedPiece);
});
}
// King me!
const endRow = parseInt(targetSquare.id.split('-')[1]);
if ((selectedPiece.dataset.color === 'red' && endRow === 0) ||
(selectedPiece.dataset.color === 'black' && endRow === 7)) {
selectedPiece.dataset.king = 'true';
selectedPiece.classList.add('king');
}
// Check for another capture
if (move.captures.length > 0) {
const additionalCaptures = getValidMoves(selectedPiece, true);
if (additionalCaptures.length > 0) {
removeHighlights();
selectedPiece.classList.add('selected');
showValidMoves();
return;
}
}
endTurn();
}
function endTurn() {
removeHighlights();
selectedPiece.classList.remove('selected');
selectedPiece = null;
turn = (turn === 'red') ? 'black' : 'red';
currentTurnDisplay.textContent = capitalize(turn);
checkWinCondition();
}
function checkWinCondition() {
const redPieces = pieces.filter(piece => piece.dataset.color === 'red');
const blackPieces = pieces.filter(piece => piece.dataset.color === 'black');
if (redPieces.length === 0) {
winnerDisplay.textContent = 'Black Wins!';
} else if (blackPieces.length === 0) {
winnerDisplay.textContent = 'Red Wins!';
}
}
function removeHighlights() {
document.querySelectorAll('.highlight').forEach(square => {
square.classList.remove('highlight');
square.removeEventListener('click', movePiece);
});
}
function capitalize(str) {
return str.charAt(0).toUpperCase() + str.slice(1);
}
document.body.addEventListener('click', function (e) {
if (selectedPiece) {
selectedPiece.classList.remove('selected');
selectedPiece = null;
removeHighlights();
}
});
createBoard();
</script>
</body>
</html>