diff --git a/js/chess.js b/js/chess.js index 9f9972d..3aab91e 100644 --- a/js/chess.js +++ b/js/chess.js @@ -79,6 +79,24 @@ function boardPut(board, where, side, piece){ board[side][row] = prefix + piece + suffix; } +function movedFrom(board){ + if (!board || !board.move || !board.move.from) { + return null; + } else if (board.move.from === 'phantom') { + return board.prior.phantom.from; + } else { + return board.move.from; + } +} + +function movedTo(board){ + if (!board || !board.move || !board.move.to) { + return null; + } else { + return board.move.to; + } +} + function countMoves(board) { let n = 0; while (board && board.prior) { @@ -145,6 +163,7 @@ function hasMoved(board, side, where){ function legalMoves(board, side, type, from, canCapture){ if (board.move && board.move.took === 'k') { + /* checkmate, the game is over */ return []; } @@ -236,11 +255,8 @@ function legalMoves(board, side, type, from, canCapture){ if (otherBoard && otherBoard.move) { const move = otherBoard.move; if (move.side[0] !== side[0] && move.type === 'p') { - const from = - (move.from === 'phantom') ? - otherBoard.prior.phantom.from : - move.from; - if (from[0] === there[0] && from[1] === forward2[1]) { + const from = movedFrom(otherBoard); + if (from && from[0] === there[0] && from[1] === forward2[1]) { /* en passant */ legals.push(there); } @@ -324,7 +340,7 @@ function movePiece(priorBoard, side, from, to){ if (otherBoard && otherBoard.move) { const move = otherBoard.move; if (move.type === 'p' && move.to[0] === to[0] && move.to[1] === actuallyFrom[1]) { - const moveFrom = (move.from === 'phantom') ? otherBoard.prior.phantom.from : move.from; + const moveFrom = movedFrom(otherBoard); if (move.side[0] === other[0] && moveFrom[1] != to[1]) { board.move.en_passant = true; board.move.took = 'p'; @@ -372,14 +388,21 @@ function movePiece(priorBoard, side, from, to){ return board; } -function findPieces(board, side, type){ +function findPieces(board, side, type, alongside){ let pieces = []; + const other = otherSide(side); + + if (board.phantom && board.phantom.type === type + side[0]) { + pieces.push(board.phantom.from); + } for (const row of "12345678") { for (const column of "abcdefgh") { const here = column + row; if (boardGet(board, here, side) === type) { - pieces.push(here); + if (!alongside || boardGet(board, here, other) === alongside) { + pieces.push(here); + } } } } @@ -437,8 +460,10 @@ function renderHistory(currentBoard) { piece = move.type === 'p' ? '' : move.type.toUpperCase(); } - if (move.from !== 'phantom') { - const sameKind = findPieces(board.prior, move.side, move.type); + const from = movedFrom(board); + + if (move.from !== 'phantom' || from !== movedTo(board.prior)) { + const sameKind = findPieces(board.prior, move.side, move.type, move.alongside || ' '); const legalFrom = []; let sameFile = 0; /* column / letter */ let sameRank = 0; /* row / number */ @@ -447,24 +472,24 @@ function renderHistory(currentBoard) { if (legalMoves(board.prior, move.side, move.type, where, true).includes(move.to)) { legalFrom.push(where); - if (where[0] === move.from[0]) { + if (where[0] === from[0]) { sameFile += 1; } - if (where[1] === move.from[1]) { + if (where[1] === from[1]) { sameRank += 1; } } } - if (legalFrom.length !== 1) { + if (legalFrom.length !== 1 || move.en_passant || (move.type === 'p' && move.took)) { /* append file, rank, or both to disambiguate */ if (sameFile === 1) { - piece += move.from[0]; + piece += from[0]; } else if (sameRank === 1) { - piece += move.from[1]; + piece += from[1]; } else { - piece += move.from; + piece += from; } } }