Modern Apps git

← Modern-Apps.git

games/nonogram: undo no longer eats a press on reveal-dropped notes

Commit
0ea04a16c16fb0cb5884c06a5a0b09663b3a8dfa
Author
Vayun Mathur <[email protected]>
When
2026-11-30 19:17 UTC (5 days ago)
Parents
9986184
Filling a crossed (or auto-crossed) cell reveals it, spends a heart
and drops the note โ€” and endStroke admitted that stroke to the undo
stack because the raw crossed/autoCrossed sets changed. But undo()
by design restores neither hearts nor notes on settled cells, so
the entry restored nothing: the next Undo press visibly did
nothing, and undoing the previous real stroke took two presses.

endStroke now asks whether undo(after, before) would actually
change the board before pushing; undo() skips entries a later
reveal has left with nothing to restore, and canUndo means 'some
entry would still change the board', so the button never promises
a dead press. Two regression tests cover the crossed-cell and
auto-crossed variants. Win detection, solver soundness, generation,
persistence and drag painting were audited and cleared. Verified:
:games:nonogram:testDevUnitTest (148 tests) and lint 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 62d866e017..fff7a008d8 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@@ -301,7 +301,13 @@ class NonogramViewModel(application: Application) : AndroidViewModel(application      */     private val undoStack = ArrayDeque<NonogramGameState>() -    private fun canUndo(game: NonogramGameState) = undoStack.isNotEmpty() && !game.isOver+    /**+     * Whether Undo would change [game]: an entry only counts while restoring it+     * still takes a mark back. A later reveal settles the cell an entry's note+     * sat on, which can leave the entry with nothing to restore.+     */+    private fun canUndo(game: NonogramGameState): Boolean =+        !game.isOver && undoStack.any { NonogramMoves.undo(game, it) != game }      override fun beginStroke(index: Int, alternate: Boolean) {         val state = _uiState.value@@ -330,11 +336,13 @@ class NonogramViewModel(application: Application) : AndroidViewModel(application         val after = _uiState.value.game         stroke = null         strokeBefore = null-        // Only strokes that changed a fill or a note are worth undoing; one that only revealed a-        // mistake has nothing undo could give back.+        // Only strokes undo can still take back are worth a slot. Judging by the+        // raw sets is not enough: a wrong fill on a crossed cell drops that+        // cell's note as it reveals it, so the sets change while undo โ€” which+        // restores neither the heart nor a note on a settled cell โ€” would give+        // nothing back. Asking undo itself keeps the two in agreement.         if (before == null || after == null) return-        if (before.filled == after.filled && before.crossed == after.crossed &&-            before.autoCrossed == after.autoCrossed) return+        if (NonogramMoves.undo(after, before) == after) return         undoStack.addLast(before)         while (undoStack.size > MAX_UNDO) undoStack.removeFirst()         _uiState.value = _uiState.value.copy(canUndo = canUndo(after))@@ -343,9 +351,19 @@ class NonogramViewModel(application: Application) : AndroidViewModel(application     override fun undo() {         val game = _uiState.value.game ?: return         if (stroke != null || game.isOver) return-        val before = undoStack.removeLastOrNull() ?: return-        val next = NonogramMoves.undo(game, before)-        if (next == game) {+        // Skip entries a later reveal has left with nothing to restore (their+        // only mark sat on the cell the reveal settled), so one press always+        // lands on a stroke that still takes something back.+        var next: NonogramGameState? = null+        while (true) {+            val before = undoStack.removeLastOrNull() ?: break+            val restored = NonogramMoves.undo(game, before)+            if (restored != game) {+                next = restored+                break+            }+        }+        if (next == null) {             _uiState.value = _uiState.value.copy(canUndo = canUndo(game))         } else {             commit(next)
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 826d996760..a8b86abcf1 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@@ -403,6 +403,51 @@ awaitStore("undo persisted") { vm.dataStore.filledCells.first { it.isEmpty() } }         assertFalse(vm.uiState.value.canUndo)     } +    @Test fun undoAfterRevealOnCrossedCellRevertsPreviousFillInOnePress() {+        val vm = loadedVm()+        val g = vm.uiState.value.game!!+        val correct = g.puzzle.solution.indices.first { g.puzzle.solution[it] }+        val wrong = g.puzzle.solution.indices.first { !g.puzzle.solution[it] }+        vm.tapCell(correct)+        awaitState("fill lands", vm) { it.game?.filled?.contains(correct) == true }+        // Note the blank, then fill-tap it: the guess reveals it, spends a+        // heart and drops the note as it settles the cell.+        vm.crossCell(wrong)+        awaitState("note placed", vm) { it.game?.crossed?.contains(wrong) == true }+        vm.tapCell(wrong)+        awaitState("reveal lands", vm) { it.game?.hearts == STARTING_HEARTS - 1 }+        assertTrue(vm.uiState.value.game!!.revealedBlanks.contains(wrong))+        assertFalse(vm.uiState.value.game!!.crossed.contains(wrong))+        // One press must reach the fill: the reveal stroke restores nothing,+        // and the note it destroyed cannot be restored either, so neither may+        // eat the press.+        vm.undo()+        val s = vm.uiState.value.game!!+        assertFalse(correct in s.filled, "one Undo press must revert the fill")+        assertTrue(wrong in s.revealedBlanks, "the revealed mistake stays")+        assertEquals(STARTING_HEARTS - 1, s.hearts, "undo never refunds a heart")+        assertFalse(vm.uiState.value.canUndo)+    }++    @Test fun undoIsNotOfferedWhenOnlyARevealedNoteCouldBeTakenBack() {+        val vm = loadedVm()+        val g = vm.uiState.value.game!!+        val wrong = g.puzzle.solution.indices.first { !g.puzzle.solution[it] }+        vm.crossCell(wrong)+        assertTrue(vm.uiState.value.canUndo, "the note can still be taken back here")+        // Revealing the noted cell settles it: the note is gone for good, so+        // there is nothing left for Undo to offer.+        vm.tapCell(wrong)+        assertTrue(vm.uiState.value.game!!.revealedBlanks.contains(wrong))+        assertFalse(+            vm.uiState.value.canUndo,+            "a stroke whose only mark was destroyed by a reveal offers no undo",+        )+        vm.undo() // no-op: the board is exactly as the reveal left it+        assertEquals(STARTING_HEARTS - 1, vm.uiState.value.game!!.hearts)+        assertTrue(vm.uiState.value.game!!.revealedBlanks.contains(wrong))+    }+     @Test fun longPressInFillModeNotesAndRestartClears() {         val vm = loadedVm()         val g = vm.uiState.value.game!!