Modern Apps git

← Modern-Apps.git

sudoku: a restored solved board is dropped instead of reopening un-won

Commit
1251920216a2e490f924256a50bdd3e65216fc3d
Author
Vayun Mathur <[email protected]>
When
2026-12-01 06:05 UTC (5 days ago)
Parents
8d02417
The progress codec stores no won flag and reconstructs
isWon = false, while isGameOver is defined !isComplete —
so a saved already-solved board restored as solved-but-not-
won: no win overlay, no stats or achievement, the clock kept
ticking on a finished board, and the next debounced save
wrote it back out (neither won nor over), perpetuating
forever; the player had to erase and re-enter a digit to
get the win recognised. The codec's existing guard tested
isWon/isGameOver, dead for exactly this case. No legitimate
writer can produce such a save: completion sets isWon
synchronously and the ViewModel clears the slot for won
games instead of saving.

decode now rejects a complete board like every other
finished or corrupt save. Restoring as a fresh win was
rejected: finished games clear their slot by design, and a
backup-restored solved grid would be farmable. Two
regression tests (solution mode and answer mode) failed
pre-fix. Solver, generator uniqueness, notes handling and
the decision tables were audited and cleared. Verified:
:games:sudoku:testDevUnitTest 175 tests pass;
:games:sudoku: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 8dea749904..0de9857088 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@@ -41,8 +41,8 @@ data class SavedSudokuGame(  * Compact JSON for [SavedSudokuGame], following the pipes/unblockjam codec approach:  * flat int arrays for every board list, a version tag, and strict validation on decode.  * A save that does not fit [GameConfig] (size, difficulty or answer mode changed, a list- * of the wrong length, a digit out of range, a given contradicting the solution) decodes- * to null and the game starts fresh.+ * of the wrong length, a digit out of range, a given contradicting the solution, a board+ * already complete) decodes to null and the game starts fresh.  */ object SudokuProgressCodec {     private const val VERSION = 1@@ -122,7 +122,15 @@ object SudokuProgressCodec {             answerMode = answerMode,             livesRemaining = root.getValue("lives").jsonPrimitive.int,         )-        if (game.isWon || game.isGameOver || game.livesRemaining !in 0..ANSWER_MODE_LIVES) return null+        // A finished board is not an in-progress game. Completion sets isWon the+        // moment the last digit lands and the writer clears the slot instead of+        // saving (see SudokuViewModel.writeInProgress), so no legitimate save is+        // complete — and the format stores no won flag, so a complete save would+        // restore as a solved grid that is not won: restore never re-runs the win+        // check, the clock keeps ticking on it, and the next save writes it back.+        if (game.isComplete || game.isGameOver ||+            game.livesRemaining !in 0..ANSWER_MODE_LIVES+        ) 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 b0635d2198..f812ce7a69 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@@ -114,6 +114,74 @@ class SudokuProgressCodecTest {         )     } +    @Test+    fun aCompletedBoardIsDropped() {+        // A legal 6x6 solution (2x3 boxes); givens on the diagonal, every other+        // cell entered correctly, so the board is already solved.+        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 completed = SavedSudokuGame(+            game = SudokuGameState(+                size = BoardSize.SIX,+                difficulty = Difficulty.EASY,+                givens = givens,+                solution = solution,+                entries = List(solution.size) { if (givens[it] == 0) solution[it] else 0 },+                notes = List(solution.size) { 0 },+            ),+            history = emptyList(),+            elapsedSeconds = 42,+        )+        assertTrue(completed.game.isComplete, "the fixture board really is solved")+        assertFalse(completed.game.isWon, "and the codec stores no won flag to restore")+        assertNull(+            SudokuProgressCodec.decode(SudokuProgressCodec.encode(completed), config),+            "a completed board is a finished game, not an in-progress save: restoring " ++                "it shows a solved grid the win check never sees",+        )+    }++    @Test+    fun aCompletedAnswerModeBoardIsDropped() {+        val solved = 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(solved.size) { if (it % 7 == 0) solved[it] else 0 }+        val completed = SavedSudokuGame(+            game = SudokuGameState(+                size = BoardSize.SIX,+                difficulty = Difficulty.EASY,+                givens = givens,+                solution = solved,+                entries = List(solved.size) { if (givens[it] == 0) solved[it] else 0 },+                notes = List(solved.size) { 0 },+                answerMode = true,+            ),+            history = emptyList(),+            elapsedSeconds = 9,+        )+        assertTrue(completed.game.isComplete)+        assertFalse(completed.game.isGameOver, "a solved board is never out-of-lives")+        assertNull(+            SudokuProgressCodec.decode(+                SudokuProgressCodec.encode(completed),+                GameConfig(BoardSize.SIX, Difficulty.EASY, answerMode = true),+            ),+        )+    }+     @Test     fun midGameRoundTripsThroughDataStore() = runBlocking {         val store = SudokuDataStore(context)