Repository navigation
Description
Summary
Any king that has not moved is offered a two-file sideways move without the castling checks. The move is then executed as castling, which can delete a friendly piece, castle out of or through check, or "castle" with no rook.
Where
js/pieces.js:87-89(isPseudoLegalMove, king case)js/game.js:346-381(generateLegalMovesForPiece, generic loop)js/game.js:397-406(thecanCastleblock, which only adds moves)js/game.js:186-197(makeMove, castling branch)
Details
isPseudoLegalMove accepts any two-file king move while hasMoved is false:
// js/pieces.js:87
if (absColDiff === 2 && absRowDiff === 0 && !piece.hasMoved) {
return true; // Potential castle
}The generic loop in generateLegalMovesForPiece pushes that square with no canCastle check. canCastle is consulted only for a second, separate push, so a false result never removes the move. makeMove treats every two-file king move as castling and overwrites the square next to the king with the rook.
Because isSquareAttacked uses the same predicate, an unmoved king is also treated as attacking the squares two files away, which removes legal king moves from the opponent in some endgames.
Impact
From ordinary positions the king can jump over its own piece (the rook then overwrites it), castle while in check or through an attacked square, castle with a missing or already-moved rook, and capture an enemy piece two files away. Castling squares also appear twice in the move list when castling is legal.
Steps to reproduce
- New game. Play 1.Nf3 a6.
- Select the white king. g1 is offered although the f1 bishop is still there.
- Play Ke1-g1. The rook lands on f1, the bishop is gone, and the move is recorded as
O-O.
Other cases: a lone king on e1 in check from a rook on e8 is offered both c1 and g1; with a black rook on f8 attacking f1, g1 is still offered.
Suggested fix
Remove the two-square branch from the king case in isPseudoLegalMove (or skip two-file king moves in the generic loop), so castling squares come only from the canCastle block.
Acceptance criteria
- Two-file king moves are offered only when
canCastlereturns true - An unmoved king does not attack squares two files away
- Castling destinations appear once in the move list
- Tests cover: piece between king and rook, king in check, transit square attacked, rook missing or moved
Getting started
- Relevant files:
js/pieces.js,js/game.js,tests/unit/game-moves.test.js - How to run tests:
npm install, thennpm testandnpm run lint - Related docs:
docs/ARCHITECTURE.md,.github/CONTRIBUTING.md
Filed by repo-audit/full-audit at 8a2c86c. Severity: high. Effort: S. audit-key: bug|js/game.js|generateLegalMovesForPiece
Activity