chess: drop saved games whose replay breaks turn or promotion rules
- Commit
de2e16af9c92c2581d71416e0477053938d6da13- Author
- Vayun Mathur <[email protected]>
- When
- 2026-12-01 20:18 UTC (4 days ago)
- Parents
80c5d67
ChessProgressCodec.decode replayed the stored UCI list with only Board.isValidMove as its legality check. That validator has no notion of whose turn it is and no notion of promotion choice, and Board.movePiece applies whatever promotion piece it is handed, so a crafted or corrupted save whose FEN matched the position it produced was restored even when no legal game could reach it: Black moving first (e7e5 as move one, restored with Black to move again), a pawn becoming a queen in mid-board (e2e4q), a promotion suffix on a non-promotion move, or a last-rank pawn step with no suffix that was not the final move. The replay now tracks the side to move from White, rejects a move by the wrong colour, and requires a promotion suffix exactly on a pawn step to the last rank (Board.isPromotionSquare). The restored turn is the tracked side to move. Genuine saves are unaffected: local games always start with White and alternate, and Move.toUci writes a suffix exactly for promoted moves. Verified: the new ChessProgressCodecTest replayRejectsWrongSideAndBogusPromotion fails before the fix (decode accepted the black-first save) and passes after; the full :games:chess:testDevUnitTest suite passes (the :library:ml cargo tasks were excluded locally because this branch predates the Cargo.lock fix 9473c16e2 on main).
Diff
games/chess/src/main/java/com/vayunmathur/games/chess/util/ChessViewModel.kt
diff --git a/games/chess/src/main/java/com/vayunmathur/games/chess/util/ChessViewModel.kt b/games/chess/src/main/java/com/vayunmathur/games/chess/util/ChessViewModel.ktindex ac8afb5911..bc67e66b48 100644--- a/games/chess/src/main/java/com/vayunmathur/games/chess/util/ChessViewModel.kt+++ b/games/chess/src/main/java/com/vayunmathur/games/chess/util/ChessViewModel.kt@@ -9,6 +9,7 @@ import com.vayunmathur.games.chess.data.PieceType import com.vayunmathur.games.chess.data.Position import com.vayunmathur.games.chess.data.opposite import com.vayunmathur.games.chess.data.isFivefoldRepetition+import com.vayunmathur.games.chess.data.isPromotionSquare import com.vayunmathur.games.chess.data.isSeventyFiveMoveDraw import com.vayunmathur.games.chess.data.parseUciMove import com.vayunmathur.games.chess.data.toFen@@ -105,10 +106,21 @@ object ChessProgressCodec { val fen = root.getValue("fen").jsonPrimitive.content var board = Board.initialState val ucis = root.getValue("moves").jsonArray.map { it.jsonPrimitive.content }+ // Board.isValidMove has no notion of whose turn it is or of promotion+ // choice, so the replay must enforce both itself: moves alternate from+ // White, and a promotion suffix is present exactly on a pawn's step to+ // the last rank. Without this a crafted or corrupted save could restore+ // a position no legal game reaches (Black moving first, a pawn becoming+ // a queen on e4) as long as its FEN matched the position it produced.+ var sideToMove = PieceColor.WHITE for (uci in ucis) { val (from, to, promotion) = parseUciMove(uci) ?: return null+ val moving = board.pieces[from.row][from.col] ?: return null+ if (moving.color != sideToMove) return null+ if ((promotion != null) != board.isPromotionSquare(moving, to)) return null if (!board.isValidMove(from, to)) return null board = board.movePiece(from, to, promotion)+ sideToMove = sideToMove.opposite } if (board.toFen() != fen || board.promotionPosition != null) return null val mode: GameMode = when (root.getValue("mode").jsonPrimitive.content) {@@ -122,7 +134,7 @@ object ChessProgressCodec { SavedChessGame( board = board, gameMode = mode,- turn = if (ucis.size % 2 == 0) PieceColor.WHITE else PieceColor.BLACK,+ turn = sideToMove, isBoardFlipped = root.getValue("flipped").jsonPrimitive.content.toBooleanStrict(), ) }.getOrNull()
games/chess/src/test/java/com/vayunmathur/games/chess/util/ChessProgressCodecTest.kt
diff --git a/games/chess/src/test/java/com/vayunmathur/games/chess/util/ChessProgressCodecTest.kt b/games/chess/src/test/java/com/vayunmathur/games/chess/util/ChessProgressCodecTest.ktindex c1113f5c30..3629539125 100644--- a/games/chess/src/test/java/com/vayunmathur/games/chess/util/ChessProgressCodecTest.kt+++ b/games/chess/src/test/java/com/vayunmathur/games/chess/util/ChessProgressCodecTest.kt@@ -60,6 +60,26 @@ class ChessProgressCodecTest { ) } + @Test+ fun replayRejectsWrongSideAndBogusPromotion() {+ // Black cannot move first: e7e5 is pseudo-legal for the pawn, and the+ // claimed FEN is exactly the position it reaches, but no legal game+ // from the initial position starts with Black. The replay must drop it+ // instead of restoring it with Black to move again.+ val blackFirst =+ """{"v":1,"fen":"rnbqkbnr/pppp1ppp/8/4p3/8/8/PPPPPPPP/RNBQKBNR w KQkq e6 0 1",""" ++ """"flipped":false,"mode":"two","moves":["e7e5"]}"""+ assertNull(ChessProgressCodec.decode(blackFirst))++ // A promotion suffix is only legal on a pawn step to the last rank:+ // e2e4q would turn the pawn into a queen on e4, which movePiece applies+ // because the replay only checks Board.isValidMove (promotion-blind).+ val bogusPromotion =+ """{"v":1,"fen":"rnbqkbnr/pppppppp/8/8/4Q3/8/PPPP1PPP/RNBQKBNR b KQkq e3 0 1",""" ++ """"flipped":false,"mode":"two","moves":["e2e4q"]}"""+ assertNull(ChessProgressCodec.decode(bogusPromotion))+ }+ @Test fun midPuzzleRoundTripsThroughFenAndSolution() { // Fool's-mate position as a puzzle: setup f2f3, then the solver line.