Modern Apps git

← Modern-Apps.git

nonogram: drop impossible restored marks instead of soft-locking

Commit
12938805c96b0244d9fc7025303a481a00946569
Author
Vayun Mathur <[email protected]>
When
2026-12-02 01:29 UTC (4 days ago)
Parents
a8b6359
Symptom: a corrupt or backup-restored board carrying a filled
cell outside the picture (or a revealed blank on a picture cell)
soft-locked the puzzle. Both marks are locked in game, the win
needs every picture cell filled, and the extra fill also breaks
the win size check, so the board could never be won and only a
restart wiping all progress recovered.

Root cause: observeMarks passed the stored filled and revealed
sets straight into NonogramGameState. Real play can never create
those marks: a guess fills only picture cells and reveals only
non-picture ones.

Fix: filter the restored sets against the puzzle solution on
load, keeping only fills on picture cells and reveals on
non-picture cells (out-of-range indices drop out with them).

Verified: new NonogramViewModelTest case restoring both
impossible marks failed before the fix and passes after, with
the remaining correct fills winning the board; full
:games:nonogram:testDevUnitTest, :games:nonogram:lint and
:games:nonogram:lintVitalRelease all pass.

Diff

games/nonogram/src/main/java/com/vayunmathur/games/nonogram/platform/NonogramViewModel.kt
diff --git a/games/nonogram/src/main/java/com/vayunmathur/games/nonogram/platform/NonogramViewModel.kt b/games/nonogram/src/main/java/com/vayunmathur/games/nonogram/platform/NonogramViewModel.ktindex fff7a008d8..2a87f1d790 100644--- a/games/nonogram/src/main/java/com/vayunmathur/games/nonogram/platform/NonogramViewModel.kt+++ b/games/nonogram/src/main/java/com/vayunmathur/games/nonogram/platform/NonogramViewModel.kt@@ -185,11 +185,18 @@ class NonogramViewModel(application: Application) : AndroidViewModel(application         }.collectLatest { (board, dailyDone) ->             // Undo history belongs to one board; a different puzzle, level or mode starts afresh.             if (_uiState.value.game?.isBoardFor(session, puzzle) != true) undoStack.clear()+            // Stored marks are trusted only as far as the game could have+            // produced them: a guess fills picture cells and reveals+            // non-picture ones, and both are locked afterwards. A corrupt+            // or backup-restored store holding the opposite (a fill on a+            // blank, a reveal on a picture cell) would soft-lock the+            // board — the win needs every picture cell filled, yet the+            // bad mark can never be removed in game — so drop them here.             val stored = NonogramGameState(                 puzzle = puzzle,-                filled = board.filled,+                filled = board.filled.filterTo(mutableSetOf()) { puzzle.solution.getOrNull(it) == true },                 crossed = board.crossed,-                revealedBlanks = board.revealed,+                revealedBlanks = board.revealed.filterTo(mutableSetOf()) { puzzle.solution.getOrNull(it) == false },                 hearts = board.hearts,                 mode = session.mode,                 level = session.level,
games/nonogram/src/test/java/com/vayunmathur/games/nonogram/platform/NonogramViewModelTest.kt
diff --git a/games/nonogram/src/test/java/com/vayunmathur/games/nonogram/platform/NonogramViewModelTest.kt b/games/nonogram/src/test/java/com/vayunmathur/games/nonogram/platform/NonogramViewModelTest.ktindex a8b86abcf1..fc566f3958 100644--- a/games/nonogram/src/test/java/com/vayunmathur/games/nonogram/platform/NonogramViewModelTest.kt+++ b/games/nonogram/src/test/java/com/vayunmathur/games/nonogram/platform/NonogramViewModelTest.kt@@ -4,6 +4,7 @@ import android.app.Application import androidx.test.core.app.ApplicationProvider import com.vayunmathur.games.nonogram.data.GameMode import com.vayunmathur.games.nonogram.data.MarkMode+import com.vayunmathur.games.nonogram.data.StoredBoard import com.vayunmathur.games.nonogram.data.STARTING_HEARTS import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.ExperimentalCoroutinesApi@@ -307,6 +308,37 @@ class NonogramViewModelTest {         awaitState("long press fills", vm) { it.game?.filled?.contains(noteCell) == true }     } +    @Test fun restoredImpossibleMarksAreDroppedInsteadOfSoftLockingTheBoard() {+        val vm = loadedVm()+        val g = vm.uiState.value.game!!+        val answer = g.puzzle.solution.indices.filter { g.puzzle.solution[it] }.toSet()+        val blank = g.puzzle.solution.indices.first { !g.puzzle.solution[it] }+        // Marks no legitimate game can produce arrive via a corrupt or+        // backup-restored store: a fill on a blank cell (real guesses only+        // fill picture cells) sitting beside the complete picture, and a+        // revealed blank on a picture cell (real mistakes only reveal+        // blanks). Both are locked, so keeping either makes the board+        // unwinnable with no in-game way to remove it.+        val store = com.vayunmathur.games.nonogram.data.NonogramDataStore(app)+        blocking {+            store.saveBoard(+                StoredBoard(+                    filled = answer + blank,+                    revealed = setOf(answer.first()),+                    hearts = STARTING_HEARTS,+                ),+                daily = false,+                level = 1,+                day = 0L,+            )+        }+        awaitState("restored board lands", vm) { it.game?.filled?.contains(answer.first()) == true }+        val restored = vm.uiState.value.game!!+        assertFalse(blank in restored.filled, "a fill outside the picture must be dropped on load")+        assertFalse(answer.first() in restored.revealedBlanks, "a revealed blank on a picture cell must be dropped on load")+        assertTrue(restored.isWon, "the five correct fills that remain must win the board")+    }+     @Test fun movesUpdateTheBoardBeforeAnyWriteLands() {         val vm = loadedVm()         val g = vm.uiState.value.game!!