<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Chess Game with Drawings</title> <style> /* Styles remain the same */ </style> </head> <body> <div id="chessboard"></div> <script> let selectedPiece = null; let isPlayerTurn = true; const board = [ [1, 2, 3, 4, 5, 3, 2, 1], [6, 6, 6, 6, 6, 6, 6, 6], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [12, 11, 10, 9, 8, 10, 11, 12], [7, 7, 7, 7, 7, 7, 7, 7], ]; const chessboardElement = document.getElementById('chessboard'); updateChessboard(); function updateChessboard() { chessboardElement.innerHTML = ''; for (let i = 0; i < 8; i++) { for (let j = 0; j < 8; j++) { const square = document.createElement('div'); square.className = 'square'; square.dataset.row = i; square.dataset.col = j; if (board[i][j] !== 0) { const piece = document.createElement('div'); piece.className = 'piece'; piece.draggable = isPlayerTurn; // Only allow dragging for player's turn piece.dataset.pieceType = board[i][j]; piece.dataset.row = i; piece.dataset.col = j; piece.textContent = getPieceSymbol(board[i][j]); if (isPlayerTurn) { piece.addEventListener('dragstart', handleDragStart); piece.addEventListener('dragover', handleDragOver); piece.addEventListener('drop', handleDrop); } square.appendChild(piece); } square.addEventListener('click', handleSquareClick); chessboardElement.appendChild(square); } } if (!isPlayerTurn) { setTimeout(makeComputerMove, 500); // Delay for a better user experience } } function handleSquareClick(event) { if (isPlayerTurn) { const clickedSquare = event.target; const row = parseInt(clickedSquare.dataset.row); const col = parseInt(clickedSquare.dataset.col); if (selectedPiece) { const fromRow = parseInt(selectedPiece.dataset.row); const fromCol = parseInt(selectedPiece.dataset.col); if (isMoveValid(fromRow, fromCol, row, col)) { board[row][col] = board[fromRow][fromCol]; board[fromRow][fromCol] = 0; selectedPiece.dataset.row = row; selectedPiece.dataset.col = col; isPlayerTurn = !isPlayerTurn; // Switch turns updateChessboard(); } else { alert('Illegal move! Try again.'); } selectedPiece = null; } else { if (board[row][col] !== 0) { selectedPiece = event.target; } } } } function handleDragStart(event) { if (isPlayerTurn) { event.dataTransfer.setData('text/plain', event.target.dataset.pieceType); } } function handleDragOver(event) { event.preventDefault(); } function handleDrop(event) { if (isPlayerTurn) { const targetType = event.dataTransfer.getData('text/plain'); const targetSquare = event.target; const fromRow = parseInt(selectedPiece.dataset.row); const fromCol = parseInt(selectedPiece.dataset.col); const toRow = parseInt(targetSquare.dataset.row); const toCol = parseInt(targetSquare.dataset.col); if (isMoveValid(fromRow, fromCol, toRow, toCol)) { board[toRow][toCol] = board[fromRow][fromCol]; board[fromRow][fromCol] = 0; selectedPiece.dataset.row = toRow; selectedPiece.dataset.col = toCol; isPlayerTurn = !isPlayerTurn; // Switch turns updateChessboard(); } else { alert('Illegal move! Try again.'); } selectedPiece = null; } } function isMoveValid(fromRow, fromCol, toRow, toCol) { // Implement your move validation logic here // For simplicity, you may start with basic checks and gradually enhance it return true; } function makeComputerMove() { const bestMove = getBestMove(); const [fromRow, fromCol, toRow, toCol] = bestMove; board[toRow][toCol] = board[fromRow][fromCol]; board[fromRow][fromCol] = 0; updateChessboard(); isPlayerTurn = !isPlayerTurn; // Switch turns } function getBestMove() { const depth = 3; // Adjust the depth as needed const isMaximizingPlayer = false; // Computer player is the minimizing player return minimax(board, depth, -Infinity, Infinity, isMaximizingPlayer).bestMove; } function minimax(board, depth, alpha, beta, maximizingPlayer) { if (depth === 0 || isGameOver(board)) { return { score: evaluateBoard(board), bestMove: null }; } const validMoves = generateValidMoves(board, maximizingPlayer); let bestMove = null; if (maximizingPlayer) { let maxScore = -Infinity; for (const move of validMoves) { const [fromRow, fromCol, toRow, toCol] = move; const newBoard = makeMove(board, fromRow, fromCol, toRow, toCol); const result = minimax(newBoard, depth - 1, alpha, beta, false); const score = result.score; if (score > maxScore) { maxScore = score; bestMove = move; } alpha = Math.max(alpha, score); if (beta <= alpha) { break; // Beta cut-off } } return { score: maxScore, bestMove }; } else { let minScore = Infinity; for (const move of validMoves) { const [fromRow, fromCol, toRow, toCol] = move; const newBoard = makeMove(board, fromRow, fromCol, toRow, toCol); const result = minimax(newBoard, depth - 1, alpha, beta, true); const score = result.score; if (score < minScore) { minScore = score; bestMove = move; } beta = Math.min(beta, score); if (beta <= alpha) { break; // Alpha cut-off } } return { score: minScore, bestMove }; } } function generateValidMoves(board, maximizingPlayer) { const validMoves = []; for (let i = 0; i < 8; i++) { for (let j = 0; j < 8; j++) { if (board[i][j] !== 0 && (maximizingPlayer ? board[i][j] < 7 : board[i][j] > 6)) { for (let k = 0; k < 8; k++) { for (let l = 0; l < 8; l++) { if (isMoveValid(i, j, k, l, board, maximizingPlayer)) { validMoves.push([i, j, k, l]); } } } } } } return validMoves; } function isGameOver(board) { // Implement your game over logic here // For simplicity, you may start with a basic implementation return false; } function evaluateBoard(board) { // Implement your board evaluation logic here // For simplicity, you may start with a basic implementation return 0; } function makeMove(board, fromRow, fromCol, toRow, toCol) { // Create a new board with the specified move const newBoard = board.map(row => row.slice()); newBoard[toRow][toCol] = newBoard[fromRow][fromCol]; newBoard[fromRow][fromCol] = 0; return newBoard; } function getPieceSymbol(pieceType) { // Return a text representation for each chess piece switch (pieceType) { case 1: return '♔'; // White King case 2: return '♕'; // White Queen case 3: return '♖'; // White Rook case 4: return '♗'; // White Bishop case 5: return '♘'; // White Knight case 6: return '♙'; // White Pawn case 7: return '♚'; // Black King case 8: return '♛'; // Black Queen case 9: return '♜'; // Black Rook case 10: return '♝'; // Black Bishop case 11: return '♞'; // Black Knight case 12: return '♟'; // Black Pawn default: return ''; } } </script> </body> </html>
Write, Run & Share HTML code online using OneCompiler's HTML online Code editor for free. It's one of the robust, feature-rich online Code editor for HTML language, running on the latest version HTML5. Getting started with the OneCompiler's HTML compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as HTML
. You can also specify the stylesheet information in styles.css
tab and scripts information in scripts.js
tab and start coding.
HTML(Hyper Text Markup language) is the standard markup language for Web pages, was created by Berners-Lee in the year 1991. Almost every web page over internet might be using HTML.
<!DOCTYPE html>
<html>
and ends with </html>
<h1>
to <h6>
where <h1>
is the highest important heading and <h6>
is the least important sub-heading.<p>..</p>
tag.<a>
tag.
<a href="https://onecompiler.com/html">HTML online compiler</a>
<img>
tag, where src
attribute consists of image name.<button>..</button>
tag<ul>
for unordered/bullet list and <ol>
for ordered/number list, and the list items are defined in <li>
.<a href="https://onecompiler.com/html">HTML online compiler</a>
CSS(cascading style sheets) describes how HTML elements will look on the web page like color, font-style, font-size, background color etc.
Below is a sample style sheet which displays heading in green and in Candara font with padding space of 25px.
body{
padding: 25px;
}
.title {
color: #228B22;
font-family: Candara;
}
<table>
tag.<tr>
tag<th>
tag<td>
tag<caption>
tag<script>
is the tag used to write scripts in HTML<script src="script.js"></script>