games/chess: check detection no longer recurses forever through castling
- Commit
cea5da84a670ab383507b1d3ecfaf2d069463469- Author
- Vayun Mathur <[email protected]>
- When
- 2026-12-01 02:38 UTC (5 days ago)
- Parents
22091f3
isValidKingMove treated any two-file king jump by an unmoved king as a castling attempt, from any square, and canCastle itself calls isKingInCheck. With kings on e1 and g1 โ a perfectly legal position, and one FEN can construct โ two unmoved kings asking 'could the enemy king capture mine' entered canCastle for each other and recursed without bound: any check/legality query crashed with StackOverflowError. The same hole let a displaced unmoved king castle from off its home square (Kd1 + Rh1 -> f1). Castling is now only offered from the king's home square, and canCastle additionally requires the corner rook to be the king's own colour, so an off-home enemy king can never enter castling during an attack test and the recursion is provably bounded. Learn and puzzle FENs keep kings on e1/e8 when rights exist, so no reachable flow changes. Four regression tests cover both king pairs, the FEN crash form, and off-home castling rejection (all failing pre-fix). Verified: :games:chess:testDevUnitTest 167 tests and lint pass.
Diff
games/chess/src/main/java/com/vayunmathur/games/chess/data/ChessMoveRules.kt
diff --git a/games/chess/src/main/java/com/vayunmathur/games/chess/data/ChessMoveRules.kt b/games/chess/src/main/java/com/vayunmathur/games/chess/data/ChessMoveRules.ktindex c2ebcfc9a9..b62041bf2b 100644--- a/games/chess/src/main/java/com/vayunmathur/games/chess/data/ChessMoveRules.kt+++ b/games/chess/src/main/java/com/vayunmathur/games/chess/data/ChessMoveRules.kt@@ -132,17 +132,29 @@ private fun Board.isValidKingMove(start: Position, end: Position, piece: Piece): val rowDiff = abs(start.row - end.row) val colDiff = abs(start.col - end.col) - if (!piece.hasMoved && rowDiff == 0 && colDiff == CASTLE_FILE_DISTANCE) {+ // Castling is only ever offered from the king's home square. That restriction also keeps+ // the attack test inside `isKingInCheck` sound: with the king off its home square, a+ // two-file king jump toward the enemy king was previously read through `canCastle`,+ // whose own `isKingInCheck` call re-entered the same test for the other king โ with+ // both kings unmoved on one rank, two files apart, the two calls recursed without+ // bound. `canCastle` is still only reached for the side actually moving its king home.+ if (!piece.hasMoved && start == homeSquare(piece.color) &&+ rowDiff == 0 && colDiff == CASTLE_FILE_DISTANCE+ ) { if (canCastle(start, end, piece)) return true } return rowDiff <= 1 && colDiff <= 1 } +private fun homeSquare(color: PieceColor): Position =+ Position(if (color == PieceColor.WHITE) WHITE_HOME_ROW else BLACK_HOME_ROW, KING_HOME_FILE)+ private fun Board.canCastle(start: Position, end: Position, piece: Piece): Boolean { if (isKingInCheck(piece.color)) return false val rookCol = if (end.col > start.col) ROOK_KINGSIDE_FILE else ROOK_QUEENSIDE_FILE val rook = pieces[start.row][rookCol] if (rook == null || rook.hasMoved || rook.type != PieceType.ROOK) return false+ if (rook.color != piece.color) return false return castlePathClear(start, rookCol, piece.color) }
games/chess/src/test/java/com/vayunmathur/games/chess/data/ChessLogicCoverageTest.kt
diff --git a/games/chess/src/test/java/com/vayunmathur/games/chess/data/ChessLogicCoverageTest.kt b/games/chess/src/test/java/com/vayunmathur/games/chess/data/ChessLogicCoverageTest.ktindex 465e9b18e7..fca261fa80 100644--- a/games/chess/src/test/java/com/vayunmathur/games/chess/data/ChessLogicCoverageTest.kt+++ b/games/chess/src/test/java/com/vayunmathur/games/chess/data/ChessLogicCoverageTest.kt@@ -94,6 +94,45 @@ class ChessLogicCoverageTest { assertFalse(rookMoved.isValidMove(sq('e', 1), sq('g', 1))) } + @Test fun `kings two files apart on one rank are not in check and can move`() {+ val kingside = board(+ sq('e', 1) to piece(PieceType.KING, PieceColor.WHITE),+ sq('g', 1) to piece(PieceType.KING, PieceColor.BLACK),+ )+ assertFalse(kingside.isKingInCheck(PieceColor.WHITE))+ assertFalse(kingside.isKingInCheck(PieceColor.BLACK))+ assertTrue(kingside.hasLegalMoves(PieceColor.WHITE))+ assertTrue(kingside.hasLegalMoves(PieceColor.BLACK))+ assertFalse(kingside.isKingInCheck(PieceColor.WHITE))+ }++ @Test fun `black kings two files apart on rank 8 are not in check`() {+ val b = board(+ sq('c', 8) to piece(PieceType.KING, PieceColor.WHITE),+ sq('e', 8) to piece(PieceType.KING, PieceColor.BLACK),+ )+ assertFalse(b.isKingInCheck(PieceColor.BLACK))+ assertFalse(b.isKingInCheck(PieceColor.WHITE))+ assertTrue(b.hasLegalMoves(PieceColor.BLACK))+ }++ @Test fun `fen board with kings on e1 and g1 answers check and move queries`() {+ val b = Board.fromFen("8/8/8/8/8/8/8/4K1k1 w Kk - 0 1")+ assertFalse(b.isKingInCheck(PieceColor.WHITE))+ assertFalse(b.isKingInCheck(PieceColor.BLACK))+ assertTrue(b.hasLegalMoves(PieceColor.WHITE))+ }++ @Test fun `king cannot castle from off its home square`() {+ val b = board(+ sq('d', 1) to piece(PieceType.KING, PieceColor.WHITE),+ sq('h', 1) to piece(PieceType.ROOK, PieceColor.WHITE),+ sq('e', 8) to piece(PieceType.KING, PieceColor.BLACK),+ )+ assertFalse(b.isValidMove(sq('d', 1), sq('f', 1)), "castling starts only on e1/e8")+ assertTrue(b.isValidMove(sq('d', 1), sq('e', 1)), "ordinary king step still legal")+ }+ @Test fun `en passant capture for white removes the double-stepped pawn`() { var b = Board.initialState b = b.movePiece(sq('e', 2), sq('e', 4))