Modern Apps git

← Modern-Apps.git

games/sudoku: saves whose givens contradict the solution no longer restore unwinnable

Commit
a9791974717c0535071bf8bbaa6a56d787c70855
Author
Vayun Mathur <[email protected]>
When
2026-12-01 02:08 UTC (5 days ago)
Parents
8804660
SudokuProgressCodec.decode validated shape and digit ranges
but never the invariant every writer guarantees: givens are
a subgrid of the solution. A save with given[0]=2 against
solution[0]=1 decoded and restored a board that can never
be won: valueAt returns the given so isComplete can never
become true, the cell can never be edited, and hint() aimed
at that 'wrong' cell — invisible behind the given —
incrementing the hint/move counters forever without
progress.

decode now rejects any save where a non-zero given differs
from the solution, so it starts fresh like other corrupt
saves (including ones arriving via Android backup restore).
No legitimately written save changes behaviour. The new
regression test failed pre-fix. Generator uniqueness, the
answer-mode decision table, notes, win detection, undo and
timer were audited and cleared. Verified:
:games:sudoku:testDevUnitTest 173 tests and lint pass.

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 575f8d8653..8dea749904 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,7 +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) decodes to null and the game starts fresh.+ * of the wrong length, a digit out of range, a given contradicting the solution) decodes+ * to null and the game starts fresh.  */ object SudokuProgressCodec {     private const val VERSION = 1@@ -85,6 +86,11 @@ object SudokuProgressCodec {         val entries = ints(root, "entries", cells) ?: return null         val notes = ints(root, "notes", cells) ?: return null         if (givens.any { it !in 0..size.side } || solution.any { it !in 1..size.side }) return null+        // A given is a clue copied out of the solution, and the game assumes the two+        // agree: givens cannot be edited and valueAt prefers them, so a given that+        // contradicts the solution makes the board impossible to complete and turns+        // every hint into a write to an entry the board never shows.+        if (givens.indices.any { givens[it] != 0 && givens[it] != solution[it] }) return null         if (entries.any { it !in 0..size.side }) return null         val maxMask = (1 shl size.side) - 1         if (notes.any { it !in 0..maxMask }) return null
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 71286a0e71..b0635d2198 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@@ -7,6 +7,7 @@ import org.junit.runner.RunWith import org.robolectric.RobolectricTestRunner import kotlin.test.Test import kotlin.test.assertEquals+import kotlin.test.assertFalse import kotlin.test.assertNotNull import kotlin.test.assertNull import kotlin.test.assertTrue@@ -85,6 +86,34 @@ class SudokuProgressCodecTest {         assertNull(SudokuProgressCodec.decode("""{"v":2}""", config))     } +    @Test+    fun aGivenThatContradictsTheSolutionIsDropped() {+        // A given that disagrees with the solution can never be fixed in play:+        // givens cannot be edited, valueAt prefers the given, so isComplete is+        // unreachable, and wrongIndices keeps sending every hint at that cell+        // even though writing its entry changes nothing the board shows.+        val saved = midGame()+        val givenIndex = saved.game.givens.indices.first { saved.game.givens[it] != 0 }+        val wrongGiven = (1..saved.game.size.side).first { it != saved.game.solution[givenIndex] }+        val corrupted = saved.copy(+            game = saved.game.copy(+                givens = saved.game.givens.toMutableList().also { it[givenIndex] = wrongGiven },+                entries = List(saved.game.size.cellCount) {+                    if (saved.game.givens[it] != 0) 0 else saved.game.solution[it]+                },+            ),+        )+        assertTrue(corrupted.game.isWrong(givenIndex), "the trapped cell reads as a mistake")+        assertFalse(+            corrupted.game.isComplete,+            "even with every playable cell solved, the board can never complete",+        )+        assertNull(+            SudokuProgressCodec.decode(SudokuProgressCodec.encode(corrupted), config),+            "a save whose givens contradict its solution must start fresh, not restore a dead board",+        )+    }+     @Test     fun midGameRoundTripsThroughDataStore() = runBlocking {         val store = SudokuDataStore(context)