games: a check escapable only by en passant is no longer flagged as mate
- Commit
8f8486a789bde5510e07686003c311bc7bfaebfd- Author
- Vayun Mathur <[email protected]>
- When
- 2026-12-01 12:43 UTC (5 days ago)
- Parents
73f7c88
Board.movePiece computed the check/mate verdict on a bare Board(newPieces) with lastMove=null — but en-passant legality reads lastMove, so after a pawn double-step the evaluator could not see the en-passant capture answering the check and reported mate where only check exists (FEN 8/8/2nbb3/2bkn3/2bn1p2/3P4/4P3/K7 w, e2-e4: the board itself said not-mate with f4xe3 e.p. legal, yet the move flagged isCheckmate=true and SAN rendered e4# instead of e4+). The verdict is now evaluated on a board carrying the provisional move as lastMove, then stamped onto the final move; the ViewModel game state already re-tested correctly, so only the flag and SAN suffix change. Regression test asserts e.p. legality, check-not-mate and e4+. Perft suites (start, Kiwipete, EP/pin, promotion/castling, depths 1-3) and the draw-rule accounting were verified and cleared. Verified: :games:chess:testDevUnitTest 267 tests and lint BUILD SUCCESSFUL.
Diff
games/chess/src/main/java/com/vayunmathur/games/chess/data/ChessModel.kt
diff --git a/games/chess/src/main/java/com/vayunmathur/games/chess/data/ChessModel.kt b/games/chess/src/main/java/com/vayunmathur/games/chess/data/ChessModel.ktindex 854335d70a..629a641e68 100644--- a/games/chess/src/main/java/com/vayunmathur/games/chess/data/ChessModel.kt+++ b/games/chess/src/main/java/com/vayunmathur/games/chess/data/ChessModel.kt@@ -118,18 +118,22 @@ data class Board( val newCapturedBlack = capturedByBlack + listOfNotNull(capturedPiece?.takeIf { it.color == PieceColor.WHITE }) - val afterBoard = Board(newPieces.map { it.toList() })- val opponentColor = movingPiece.color.opposite- val isCheck = afterBoard.isKingInCheck(opponentColor)- val isCheckmate = afterBoard.isCheckmate(opponentColor)-- val fullMove = Move(+ // The check/mate verdict must be computed on a board that knows this was+ // the last move: en-passant legality reads `lastMove`, so a bare board+ // would miss an en-passant capture that is the defender's only escape+ // from check and report a false checkmate (and a spurious "#" in SAN).+ val provisionalMove = Move( start = start, end = end, piece = movingPiece, capturedPiece = capturedPiece, promotedTo = promoteTo,- isCheck = isCheck, isCheckmate = isCheckmate, isCastling = isCastleMove(movingPiece, start, end), ambiguity = ambiguity )+ val afterBoard = Board(newPieces.map { it.toList() }, lastMove = provisionalMove)+ val opponentColor = movingPiece.color.opposite+ val isCheck = afterBoard.isKingInCheck(opponentColor)+ val isCheckmate = afterBoard.isCheckmate(opponentColor)++ val fullMove = provisionalMove.copy(isCheck = isCheck, isCheckmate = isCheckmate) return Board( pieces = newPieces.map { it.toList() },
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 d5d5573c06..c7443fb886 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@@ -170,6 +170,22 @@ class ChessLogicCoverageTest { assertTrue(after.capturedByBlack.any { it.type == PieceType.PAWN }) } + @Test fun `double step giving check is not mate when en passant capture is the only escape`() {+ // FEN: 8/8/2nbb3/2bkn3/2bn1p2/3P4/4P3/K7 w - - 0 1+ // White e2-e4+ checks the d5 king (the pawn attacks d5). Every king+ // flight is occupied by a black minor that cannot lift the check, and+ // Kxe4 runs into the d3 pawn defending e4, so Black's only legal reply+ // is f4xe3 en passant, removing the checking pawn. The move therefore+ // is check, not checkmate, and its SAN takes "+" not "#".+ val before = Board.fromFen("8/8/2nbb3/2bkn3/2bn1p2/3P4/4P3/K7 w - - 0 1")+ val after = before.movePiece(sq('e', 2), sq('e', 4))+ assertTrue(after.isValidMove(sq('f', 4), sq('e', 3)), "en-passant capture is legal")+ assertFalse(after.isCheckmate(PieceColor.BLACK))+ assertTrue(after.lastMove?.isCheck == true)+ assertFalse(after.lastMove?.isCheckmate == true)+ assertEquals("e4+", after.lastMove.toString())+ }+ @Test fun `pawn double step needs a clear path and captures need a victim`() { val blocked = board( sq('e', 1) to piece(PieceType.KING, PieceColor.WHITE),