games/sudoku: mistake flag survives save/restore
- Commit
d28ea6bcafd8377c77bdcad2ebc7c21630791804- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-30 19:54 UTC (5 days ago)
- Parents
996836c
The no_mistakes achievement could be farmed through a restart: madeMistake was never persisted, and restore re-derived it from the board (entries != solution). Answer-mode rejections never land in the entries, and erased/undone wrong digits leave no trace, so after save/restore the flag reset to false — despite persisted livesRemaining = 2 proving a rejection happened — and a clean solve then unlocked the achievement. madeMistake is now stored in the saved game (codec field, v1 backward-compatible: legacy saves fall back to the board-derived check, the best possible for them) and restored directly instead of re-derived. Regression tests: codec round-trip on a clean board, legacy fallback, and an end-to-end rejection + restore + solve scenario (failed pre-fix). Win detection, generator uniqueness, notes sweeping, undo caps and timers were audited and cleared. Verified: :games:sudoku:testDevUnitTest 172 tests pass, lint green.
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 9f1256366f..575f8d8653 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@@ -26,6 +26,15 @@ data class SavedSudokuGame( val game: SudokuGameState, val history: List<SudokuSnapshot>, val elapsedSeconds: Int,+ /**+ * Whether the player has ever entered or been refused a wrong digit in this game.+ *+ * Saved explicitly rather than re-derived from the board on restore: an erased,+ * undone or hint-corrected wrong entry leaves no trace in [SudokuGameState.entries],+ * and an answer-mode rejection never lands there at all, so the board alone cannot+ * say whether the `no_mistakes` achievement is still earnable.+ */+ val madeMistake: Boolean = false, ) /**@@ -54,6 +63,7 @@ object SudokuProgressCodec { put("moveCount", game.moveCount) put("hintsUsed", game.hintsUsed) put("usedUndo", game.usedUndo)+ put("madeMistake", saved.madeMistake) put("lives", game.livesRemaining) put("h", JsonArray(saved.history.map { snap -> buildJsonObject { put("e", ints(snap.entries)); put("n", ints(snap.notes)) }@@ -107,7 +117,11 @@ object SudokuProgressCodec { livesRemaining = root.getValue("lives").jsonPrimitive.int, ) if (game.isWon || game.isGameOver || game.livesRemaining !in 0..ANSWER_MODE_LIVES) return null- SavedSudokuGame(game, history, elapsed)+ // 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()+ ?: entries.indices.any { entries[it] != 0 && entries[it] != solution[it] }+ SavedSudokuGame(game, history, elapsed, madeMistake) }.getOrNull() private fun ints(values: List<Int>): JsonArray = buildJsonArray { for (v in values) add(v) }
games/sudoku/src/main/java/com/vayunmathur/games/sudoku/platform/SudokuViewModel.kt
diff --git a/games/sudoku/src/main/java/com/vayunmathur/games/sudoku/platform/SudokuViewModel.kt b/games/sudoku/src/main/java/com/vayunmathur/games/sudoku/platform/SudokuViewModel.ktindex 400d3aa72e..ed666c3d21 100644--- a/games/sudoku/src/main/java/com/vayunmathur/games/sudoku/platform/SudokuViewModel.kt+++ b/games/sudoku/src/main/java/com/vayunmathur/games/sudoku/platform/SudokuViewModel.kt@@ -136,7 +136,7 @@ class SudokuViewModel(application: Application) : AndroidViewModel(application), val variant = game.variant withContext(Dispatchers.IO) { settingsStore.clearInProgress(variant) } } else {- val saved = SavedSudokuGame(game, history.toList(), _elapsedSeconds.value)+ val saved = SavedSudokuGame(game, history.toList(), _elapsedSeconds.value, madeMistake) withContext(Dispatchers.IO) { settingsStore.saveInProgress(saved) } } }@@ -161,9 +161,11 @@ class SudokuViewModel(application: Application) : AndroidViewModel(application), } else { history.clear() history.addAll(saved.history)- madeMistake = saved.game.entries.indices.any { i ->- saved.game.entries[i] != 0 && saved.game.entries[i] != saved.game.solution[i]- }+ // The saved flag, not the board: an erased wrong entry and an+ // answer-mode rejection both leave the entries clean (see+ // SavedSudokuGame.madeMistake), so re-deriving it here would+ // hand back a no-mistakes game the player did not play.+ madeMistake = saved.madeMistake winRecorded = false rejectionAttempt = 0 _elapsedSeconds.value = saved.elapsedSeconds
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 88cec7b79b..71286a0e71 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@@ -9,6 +9,7 @@ import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertNotNull import kotlin.test.assertNull+import kotlin.test.assertTrue @RunWith(RobolectricTestRunner::class) class SudokuProgressCodecTest {@@ -38,7 +39,7 @@ class SudokuProgressCodecTest { val history = listOf( SudokuSnapshot(List(size.cellCount) { 0 }, List(size.cellCount) { 0 }), )- return SavedSudokuGame(game, history, elapsedSeconds = 87)+ return SavedSudokuGame(game, history, elapsedSeconds = 87, madeMistake = true) } @Test@@ -47,6 +48,28 @@ class SudokuProgressCodecTest { assertEquals(saved, SudokuProgressCodec.decode(SudokuProgressCodec.encode(saved), config)) } + @Test+ fun madeMistakeRoundTripsEvenWhenNoWrongEntryRemains() {+ // The wrong digit was erased before the save: the board is clean, so only+ // the persisted flag can still say a mistake happened in this game.+ val saved = midGame().let {+ it.copy(game = it.game.copy(entries = List(it.game.size.cellCount) { 0 }))+ }+ val decoded = SudokuProgressCodec.decode(SudokuProgressCodec.encode(saved), config)+ assertEquals(saved, decoded)+ assertTrue(decoded!!.madeMistake)+ }++ @Test+ fun legacySaveWithoutMadeMistakeFallsBackToWrongEntriesOnBoard() {+ // Saves written before the flag existed: a wrong entry still showing is+ // the only mistake evidence they carry, and it must still be honoured.+ val encoded = SudokuProgressCodec.encode(midGame())+ val legacy = encoded.replace("\"madeMistake\":true,", "")+ assertTrue(legacy != encoded, "the field must really be stripped: $encoded")+ assertTrue(SudokuProgressCodec.decode(legacy, config)!!.madeMistake)+ }+ @Test fun mismatchedConfigAndCorruptJsonAreDropped() { val encoded = SudokuProgressCodec.encode(midGame())
games/sudoku/src/test/java/com/vayunmathur/games/sudoku/platform/SudokuViewModelTest.kt
diff --git a/games/sudoku/src/test/java/com/vayunmathur/games/sudoku/platform/SudokuViewModelTest.kt b/games/sudoku/src/test/java/com/vayunmathur/games/sudoku/platform/SudokuViewModelTest.ktindex d725ac604c..543c72eebc 100644--- a/games/sudoku/src/test/java/com/vayunmathur/games/sudoku/platform/SudokuViewModelTest.kt+++ b/games/sudoku/src/test/java/com/vayunmathur/games/sudoku/platform/SudokuViewModelTest.kt@@ -480,6 +480,44 @@ class SudokuViewModelTest { assertFalse(restarted.isGameOver) } + @Test+ fun restoredAnswerModeRejectionStillCountsAgainstNoMistakes() {+ val config = GameConfig(BoardSize.SIX, Difficulty.EASY, answerMode = true)+ val vm = startedVm(config)+ val cell = blank(vm)+ val game = vm.uiState.value.game!!+ val wrong = (1..game.size.side).first { it != game.solution[cell] }+ vm.selectCell(cell)+ vm.enterDigit(wrong)+ assertEquals(2, vm.uiState.value.game!!.livesRemaining, "rejection costs a life")++ vm.saveInProgress()+ await("rejected game saved") {+ runBlocking { SudokuDataStore(app).loadInProgress(config) }?.game?.livesRemaining == 2+ }++ // A fresh ViewModel restores the saved game, as after process death.+ val restored = SudokuViewModel(app)+ restored.ensureGame(config)+ await("game restored") { restored.uiState.value.game != null }+ assertEquals(2, restored.uiState.value.game!!.livesRemaining)++ var guard = 0+ while (guard++ < 100) {+ val current = restored.uiState.value.game!!+ val next = current.blankIndices().firstOrNull() ?: break+ restored.selectCell(next)+ restored.enterDigit(current.solution[next])+ }+ assertTrue(restored.uiState.value.game!!.isWon)+ val noMistakes = runBlocking { restored.achievementsManager.getAchievementStatuses().first() }+ .first { it.achievement.id == "no_mistakes" }+ assertFalse(+ noMistakes.isUnlocked,+ "a rejection before save/restore must still deny the no-mistakes achievement",+ )+ }+ @Test fun answerModeCorrectDigitPlacesWithoutCostingLife() { val vm = startedVm(GameConfig(BoardSize.SIX, Difficulty.EASY, answerMode = true))