const socket = io(); const chess = new Chess(); const boardElement = document.querySelector(".chessboard"); let draggedPiece = null; let sourceSquare = null; let playerRole = null; let moveHistory = []; // Track all moves const renderBoard = () => { const board = chess.board(); boardElement.innerHTML = ""; board.forEach((row, rowIndex) => { row.forEach((square, colIndex) => { const squareElement = document.createElement("div"); squareElement.classList.add( "square", (rowIndex + colIndex) % 2 === 0 ? "light" : "dark" ); squareElement.dataset.row = rowIndex; squareElement.dataset.col = colIndex; if (square) { const pieceElement = document.createElement("div"); pieceElement.classList.add( "piece", square.color === "w" ? "white" : "black" ); pieceElement.innerText = getPieceUnicode(square); pieceElement.draggable = playerRole === square.color; pieceElement.addEventListener("dragstart", (e) => { if (pieceElement.draggable) { draggedPiece = pieceElement; sourceSquare = { row: rowIndex, col: colIndex }; e.dataTransfer.setData("text/plain", ""); } }); pieceElement.addEventListener("dragend", () => { draggedPiece = null; sourceSquare = null; }); squareElement.appendChild(pieceElement); } squareElement.addEventListener("dragover", (e) => e.preventDefault()); squareElement.addEventListener("drop", (e) => { e.preventDefault(); if (draggedPiece) { const targetSquare = { row: parseInt(squareElement.dataset.row), col: parseInt(squareElement.dataset.col), }; handleMove(sourceSquare, targetSquare); } }); boardElement.appendChild(squareElement); }); }); boardElement.classList.toggle("flipped", playerRole === "b"); }; const handleMove = (source, target) => { const move = { from: `${String.fromCharCode(97 + source.col)}${8 - source.row}`, to: `${String.fromCharCode(97 + target.col)}${8 - target.row}`, promotion: "q", }; socket.emit("move", move); }; const getPieceUnicode = (piece) => { const unicodePieces = { p: "♟", r: "♜", n: "♞", b: "♝", q: "♛", k: "♚", P: "♙", R: "♖", N: "♘", B: "♗", Q: "♕", K: "♔", }; return unicodePieces[piece.type] || ""; }; // New function to add move to history and update table const addMoveToHistory = (move) => { const moveObj = chess.get(move.to); const isPawnMove = moveObj && moveObj.type === 'p'; moveHistory.push({ moveNumber: Math.ceil(moveHistory.length / 2) + 1, player: chess.turn() === 'w' ? 'Black' : 'White', // Previous player who made the move piece: moveObj ? getPieceUnicode(moveObj) : '', from: move.from, to: move.to, notation: move.san || `${move.from}-${move.to}`, isPawn: isPawnMove, timestamp: new Date().toLocaleTimeString() }); updateMovesTable(); }; // New function to update the moves table const updateMovesTable = () => { const tableBody = document.querySelector("#moves-table tbody"); if (!tableBody) return; tableBody.innerHTML = ""; moveHistory.forEach((move, index) => { const row = document.createElement("tr"); if (move.isPawn) { row.classList.add("pawn-move"); } row.innerHTML = `
| Move# | Player | Piece | From | To | Notation | Time |
|---|