chess: castling moves now carry their check and mate suffixes
- Commit
69825df509109058ceaa4e3e32ec58756f695f64- Author
- Vayun Mathur <[email protected]>
- When
- 2026-12-01 06:09 UTC (5 days ago)
- Parents
1251920
Move.toString had an early return for castling that bypassed the isCheckmate -> '#' / isCheck -> '+' suffix logic every other move goes through, though the flags were always computed correctly. From 5k2/8/8/8/8/8/8/4K2R w K -, O-O checks the f8 king yet history recorded 'O-O' instead of 'O-O+'; from the Qe3/Bb2/Nh6 net position O-O is mate yet recorded 'O-O' while the status line correctly ended the game as mate — list and status contradicting each other. The castling branch now appends the suffix from the same flags; move generation is untouched (this is deliberately not a re-fix of the recent castling-recursion commit). One new test covers O-O+, O-O# and O-O-O+. The generator was audited against standard perft suites (start, Kiwipete, the en- passant-pin and promotion positions — all exact) plus draw rules, codecs and clock handling, all cleared. Verified: :games:chess:testDevUnitTest 168 tests pass; :games:chess: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 13b1cd2090..854335d70a 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@@ -65,7 +65,15 @@ data class Move( val ambiguity: String = "" ) { override fun toString(): String {- if (isCastling) return if (end.col > start.col) "O-O" else "O-O-O"+ if (isCastling) {+ return buildString {+ append(if (end.col > start.col) "O-O" else "O-O-O")+ when {+ isCheckmate -> append("#")+ isCheck -> append("+")+ }+ }+ } return buildString { if (piece.type != PieceType.PAWN) {
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 fca261fa80..d5d5573c06 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@@ -257,6 +257,22 @@ class ChessLogicCoverageTest { assertEquals("e4", plain.toString()) } + @Test fun `castling notation marks check and checkmate`() {+ // Castling kingside lands the rook on the black king's file: check, not mate.+ val check = Board.fromFen("5k2/8/8/8/8/8/8/4K2R w K - 0 1")+ val checkMove = check.movePiece(sq('e', 1), sq('g', 1))+ assertTrue(checkMove.lastMove?.isCheck == true)+ assertEquals("O-O+", checkMove.lastMove.toString())+ // Same castle with the king's escapes covered by queen, bishop and knight: mate.+ val mate = Board.fromFen("5k2/8/7N/8/8/4Q3/1B6/4K2R w K - 0 1")+ val mateMove = mate.movePiece(sq('e', 1), sq('g', 1))+ assertTrue(mateMove.isCheckmate(PieceColor.BLACK))+ assertEquals("O-O#", mateMove.lastMove.toString())+ // Queenside castling that gives check takes the suffix too.+ val queenCheck = Board.fromFen("3k4/8/8/8/8/8/8/R3K3 w Q - 0 1")+ assertEquals("O-O-O+", queenCheck.movePiece(sq('e', 1), sq('c', 1)).lastMove.toString())+ }+ @Test fun `notation marks check and checkmate`() { val check = board( sq('e', 1) to piece(PieceType.KING, PieceColor.WHITE),