Make me a fully funcinal chess game in html/css and put everything in 1 file
- User
Make me a fully funcinal chess game in html/css and put everything in 1 file
- Assistant
Make me a fully funcinal chess game in html/css and put everything in 1 file
Certainly! Below is the code for a fully functional chess game implemented in HTML, CSS, and JavaScript, all contained within a single file. You can save this code with a .html extension and open it in a web browser to play the game.
This chess game allows two players to play against each other on the same computer. It includes basic move validation for each piece and enforces turn-based play between white and black. Please note that advanced rules such as check, checkmate, castling, en passant, and pawn promotion are not implemented in this version.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Chess Game</title>
<style>
/* CSS styles */
body {
font-family: Arial, sans-serif;
background-color: #eee;
}
h1 {
text-align: center;
}
#turn-indicator {
text-align: center;
margin-bottom: 10px;
font-size: 20px;
font-weight: bold;
}
#board {
width: 480px;
height: 480px;
margin: 0 auto;
display: flex;
flex-wrap: wrap;
border: 2px solid #333;
}
.square {
width: 60px;
height: 60px;
box-sizing: border-box;
display: flex;
justify-content: center;
align-items: center;
font-size: 48px;
cursor: pointer;
user-select: none;
}
.light {
background-color: #f0d9b5;
}
.dark {
background-color: #b58863;
}
.selected {
outline: 3px solid yellow;
}
</style>
</head>
<body>
<h1>Chess Game</h1>
<div id="turn-indicator">Turn: White</div>
<div id="board"></div>
<script>
// JavaScript code
document.addEventListener('DOMContentLoaded', function() {
const boardElement = document.getElementById('board');
const turnIndicator = document.getElementById('turn-indicator');
// Initialize the board with pieces
const board = [
['r','n','b','q','k','b','n','r'],
['p','p','p','p','p','p','p','p'],
['','','','','','','',''],
['','','','','','','',''],
['','','','','','','',''],
['','','','','','','',''],
['P','P','P','P','P','P','P','P'],
['R','N','B','Q','K','B','N','R']
]; // Uppercase for white pieces, lowercase for black pieces
const pieces = {
'K': '♔', // White King
'Q': '♕', // White Queen
'R': '♖', // White Rook
'B': '♗', // White Bishop
'N': '♘', // White Knight
'P': '♙', // White Pawn
'k': '♚', // Black King
'q': '♛', // Black Queen
'r': '♜', // Black Rook
'b': '♝', // Black Bishop
'n': '♞', // Black Knight
'p': '♟' // Black Pawn
};
let selectedPiece = null;
let turn = 'white';
function renderBoard() {
boardElement.innerHTML = '';
for (let row = 0; row < 8; row++) {
for (let col = 0; col < 8; col++) {
const square = document.createElement('div');
square.classList.add('square');
square.classList.add((row + col) % 2 === 0 ? 'light' : 'dark');
square.dataset.row = row;
square.dataset.col = col;
const piece = board[row][col];
if (piece) {
square.innerHTML = pieces[piece];
square.dataset.piece = piece;
}
if (selectedPiece && selectedPiece.row == row && selectedPiece.col == col) {
square.classList.add('selected');
}
boardElement.appendChild(square);
}
}
turnIndicator.textContent = 'Turn: ' + (turn === 'white' ? 'White' : 'Black');
}
function isWhitePiece(piece) {
return piece && piece === piece.toUpperCase();
}
function isBlackPiece(piece) {
return piece && piece === piece.toLowerCase();
}
function handleClick(e) {
const target = e.target.closest('.square');
if (!target) return;
const row = parseInt(target.dataset.row);
const col = parseInt(target.dataset.col);
const piece = board[row][col];
if (selectedPiece) {
// Attempt to move the selected piece to this square
const fromRow = selectedPiece.row;
const fromCol = selectedPiece.col;
const movingPiece = board[fromRow][fromCol];
// Basic move validation
const validMove = validateMove(fromRow, fromCol, row, col, movingPiece);
const destinationPiece = board[row][col];
const isSameColor = destinationPiece && ((isWhitePiece(movingPiece) && isWhitePiece(destinationPiece)) || (isBlackPiece(movingPiece) && isBlackPiece(destinationPiece)));
if (validMove && !isSameColor) {
board[row][col] = movingPiece;
board[fromRow][fromCol] = '';
turn = (turn === 'white') ? 'black' : 'white';
}
selectedPiece = null;
renderBoard();
} else if (piece) {
// Select a piece to move if it matches the turn
if ((turn === 'white' && isWhitePiece(piece)) || (turn === 'black' && isBlackPiece(piece))) {
selectedPiece = { row, col };
renderBoard();
}
}
}
function validateMove(fromRow, fromCol, toRow, toCol, piece) {
// Implement basic move rules for each piece
// For simplicity, this does not include checks for checkmate, castling, en passant, or promotion
const deltaRow = toRow - fromRow;
const deltaCol = toCol - fromCol;
const absDeltaRow = Math.abs(deltaRow);
const absDeltaCol = Math.abs(deltaCol);
switch (piece.toLowerCase()) {
case 'p': // Pawn
const direction = isWhitePiece(piece) ? -1 : 1;
// Normal move
if (deltaCol === 0 && deltaRow === direction && !board[toRow][toCol]) {
return true;
}
// Capture
if (absDeltaCol === 1 && deltaRow === direction && board[toRow][toCol]) {
return true;
}
// Initial double move
if ((fromRow === 6 && direction === -1 || fromRow === 1 && direction === 1) && deltaCol === 0 && deltaRow === 2 * direction && !board[toRow][toCol] && !board[fromRow + direction][fromCol]) {
return true;
}
break;
case 'r': // Rook
if (deltaRow === 0 || deltaCol === 0) {
if (clearPath(fromRow, fromCol, toRow, toCol)) {
return true;
}
}
break;
case 'n': // Knight
if ((absDeltaRow === 2 && absDeltaCol === 1) || (absDeltaRow === 1 && absDeltaCol === 2)) {
return true;
}
break;
case 'b': // Bishop
if (absDeltaRow === absDeltaCol) {
if (clearPath(fromRow, fromCol, toRow, toCol)) {
return true;
}
}
break;
case 'q': // Queen
if (deltaRow === 0 || deltaCol === 0 || absDeltaRow === absDeltaCol) {
if (clearPath(fromRow, fromCol, toRow, toCol)) {
return true;
}
}
break;
case 'k': // King
if (absDeltaRow <= 1 && absDeltaCol <=1) {
return true;
}
break;
}
return false;
}
function clearPath(fromRow, fromCol, toRow, toCol) {
const deltaRow = toRow - fromRow;
const deltaCol = toCol - fromCol;
const stepRow = deltaRow === 0 ? 0 : deltaRow / Math.abs(deltaRow);
const stepCol = deltaCol === 0 ? 0 : deltaCol / Math.abs(deltaCol);
let currentRow = fromRow + stepRow;
let currentCol = fromCol + stepCol;
while (currentRow !== toRow || currentCol !== toCol) {
if (board[currentRow][currentCol]) {
return false;
}
currentRow += stepRow;
currentCol += stepCol;
}
return true;
}
boardElement.addEventListener('click', handleClick);
renderBoard();
});
</script>
</body>
</html>
Instructions to Run the Game:
.html extension, for example, chess_game.html.How to Play:
Example Moves:
Notes:
Enjoy your game of chess!
Not yet included in a published release.