games: an answer-mode save holding a wrong digit is rejected as corrupt
- Commit
7fc0df34932d6b2278ae5056add85ec3bb65ca85- Author
- Vayun Mathur <[email protected]>
- When
- 2026-12-01 12:38 UTC (5 days ago)
- Parents
e1c01b0
Answer mode never places a wrong digit (wrong guesses cost a life and stay off the board; the code documents 'in answer mode there are never wrong digits'), but the progress codec only range-checked restored entries. A corrupt or backup-restored save with answerMode=true and a wrong entry restored into a state no writer can produce โ and with madeMistake=false persisted, a flawless win stayed creditable off a digit no life was spent on. Classic-mode wrong entries are legal play and still restore. decode now rejects the save (fresh game, like every other corrupt save) when any playable cell's entry disagrees with the solution in answer mode, including undo snapshots, since undo would resurrect the digit. No legitimate writer can produce such a save. Two regression tests failed pre-fix. Generator/solver, notes and timing were audited and cleared. Verified: :games:sudoku:testDevUnitTest 177 tests and lint BUILD SUCCESSFUL.
Diff
games/sudoku/src/main/java/com/vayunmathur/games/sudoku/data/SudokuDataStore.kt
diff --git a/games/sudoku/src/main/java/com/vayunmathur/games/sudoku/data/SudokuDataStore.kt b/games/sudoku/src/main/java/com/vayunmathur/games/sudoku/data/SudokuDataStore.ktindex 0de9857088..6c37b04043 100644--- a/games/sudoku/src/main/java/com/vayunmathur/games/sudoku/data/SudokuDataStore.kt+++ b/games/sudoku/src/main/java/com/vayunmathur/games/sudoku/data/SudokuDataStore.kt@@ -131,6 +131,19 @@ object SudokuProgressCodec { if (game.isComplete || game.isGameOver || game.livesRemaining !in 0..ANSWER_MODE_LIVES ) return null+ // In answer mode a wrong digit is rejected and never placed, so every+ // entry a legitimate writer saves โ on the board or in an undo snapshot โ+ // agrees with the solution. An entry that disagrees could only arrive+ // corrupt or backup-restored, and restoring it would show a wrong digit+ // no life was ever spent on, which a hint would then spend itself+ // correcting. Classic entries may be wrong and restore exactly as before.+ if (answerMode) {+ fun wrongEntry(board: List<Int>): Boolean =+ board.indices.any {+ givens[it] == 0 && board[it] != 0 && board[it] != solution[it]+ }+ if (wrongEntry(entries) || history.any { wrongEntry(it.entries) }) return null+ } // Saves written before the flag existed carry no trace of an erased mistake; // the best they can offer is a wrong entry still on the board. val madeMistake = root["madeMistake"]?.jsonPrimitive?.content?.toBooleanStrict()
games/sudoku/src/test/java/com/vayunmathur/games/sudoku/data/SudokuProgressCodecTest.kt
diff --git a/games/sudoku/src/test/java/com/vayunmathur/games/sudoku/data/SudokuProgressCodecTest.kt b/games/sudoku/src/test/java/com/vayunmathur/games/sudoku/data/SudokuProgressCodecTest.ktindex f812ce7a69..72821d3f85 100644--- a/games/sudoku/src/test/java/com/vayunmathur/games/sudoku/data/SudokuProgressCodecTest.kt+++ b/games/sudoku/src/test/java/com/vayunmathur/games/sudoku/data/SudokuProgressCodecTest.kt@@ -182,6 +182,92 @@ class SudokuProgressCodecTest { ) } + @Test+ fun anAnswerModeSaveWithAWrongEntryIsDropped() {+ // A legal 6x6 solution (2x3 boxes), givens every seventh cell. In answer+ // mode a wrong digit is rejected and never placed, so every entry on a+ // legitimately written board agrees with the solution: an entry that+ // disagrees could only arrive corrupt or backup-restored, and restoring+ // it would show a wrong digit no life was ever spent on.+ val solution = listOf(+ 1, 2, 3, 4, 5, 6,+ 4, 5, 6, 1, 2, 3,+ 2, 3, 1, 5, 6, 4,+ 5, 6, 4, 2, 3, 1,+ 3, 1, 2, 6, 4, 5,+ 6, 4, 5, 3, 1, 2,+ )+ val givens = List(solution.size) { if (it % 7 == 0) solution[it] else 0 }+ val blank = givens.indices.first { givens[it] == 0 }+ val wrong = (1..BoardSize.SIX.side).first { it != solution[blank] }+ val corrupted = SavedSudokuGame(+ game = SudokuGameState(+ size = BoardSize.SIX,+ difficulty = Difficulty.EASY,+ givens = givens,+ solution = solution,+ entries = List(solution.size) { if (it == blank) wrong else 0 },+ notes = List(solution.size) { 0 },+ answerMode = true,+ ),+ history = emptyList(),+ elapsedSeconds = 9,+ madeMistake = false,+ )+ assertTrue(corrupted.game.isWrong(blank), "the restored cell really is a wrong entry")+ assertNull(+ SudokuProgressCodec.decode(+ SudokuProgressCodec.encode(corrupted),+ GameConfig(BoardSize.SIX, Difficulty.EASY, answerMode = true),+ ),+ "an answer-mode save with a wrong entry must start fresh, not restore " ++ "a board answer mode can never produce",+ )+ }++ @Test+ fun anAnswerModeSaveWhoseHistoryHoldsAWrongEntryIsDropped() {+ // Same invariant, one undo deeper: the board itself is clean, but an undo+ // snapshot still holds a wrong entry, so the first undo after restore+ // would put a digit on the board that answer mode never placed.+ val solution = listOf(+ 1, 2, 3, 4, 5, 6,+ 4, 5, 6, 1, 2, 3,+ 2, 3, 1, 5, 6, 4,+ 5, 6, 4, 2, 3, 1,+ 3, 1, 2, 6, 4, 5,+ 6, 4, 5, 3, 1, 2,+ )+ val givens = List(solution.size) { if (it % 7 == 0) solution[it] else 0 }+ val blank = givens.indices.first { givens[it] == 0 }+ val wrong = (1..BoardSize.SIX.side).first { it != solution[blank] }+ val corrupted = SavedSudokuGame(+ game = SudokuGameState(+ size = BoardSize.SIX,+ difficulty = Difficulty.EASY,+ givens = givens,+ solution = solution,+ entries = List(solution.size) { 0 },+ notes = List(solution.size) { 0 },+ answerMode = true,+ ),+ history = listOf(+ SudokuSnapshot(+ List(solution.size) { if (it == blank) wrong else 0 },+ List(solution.size) { 0 },+ ),+ ),+ elapsedSeconds = 9,+ )+ assertNull(+ SudokuProgressCodec.decode(+ SudokuProgressCodec.encode(corrupted),+ GameConfig(BoardSize.SIX, Difficulty.EASY, answerMode = true),+ ),+ "an answer-mode save whose undo history holds a wrong entry must start fresh",+ )+ }+ @Test fun midGameRoundTripsThroughDataStore() = runBlocking { val store = SudokuDataStore(context)