Modern Apps git

← Modern-Apps.git

sudoku: make undo a no-op once the game is won or lost (#ux-audit)

Commit
10e3b7b7bd64d92e9bf152a599085430e249cc0b
Author
Vayun Mathur <[email protected]>
When
2026-11-13 01:09 UTC (23 days ago)
Parents
e2c65cf
Symptom: after completing the grid, Undo stayed enabled. Pressing it
restored the pre-win entries while the game stayed marked as won, so
a "solved" board reopened with a blank cell behind the win overlay; on
an answer-mode loss it likewise let the player keep editing a board the
overlay said was over.

Root cause: SudokuViewModel.undo() popped history unconditionally,
unlike enterDigit/clearCell/hint, which all refuse input on a finished
game. The Undo button was enabled from canUndo alone.

Fix: undo() returns early when the game is won or out of lives, and
the action bar disables Undo alongside Erase and Hint once the game has
ended (state.canUndo && !ended). History is left intact; it is cleared
by the next deal or restart as before.

User-visible effect: a solved or lost puzzle can no longer be
reopened through Undo; the button greys out with the other inputs.

Verification: new SudokuViewModelTest cases
undoIsANoOpOnceTheGameIsWon and undoIsANoOpOnceAnswerModeRunsOutOfLives
assert entries are unchanged and usedUndo stays false;
:games:sudoku:testDevUnitTest in the toolchain container.

Diff

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 953c559ede..2bc847404b 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@@ -286,7 +286,16 @@ class SudokuViewModel(application: Application) : AndroidViewModel(application),         checkWin()     } +    /**+     * Steps the board back one move.+     *+     * A no-op once the game has ended. Undoing on a won board used to restore the pre-win entries+     * while `isWon` stayed true, reopening a "solved" grid the player could then edit; and on an+     * answer-mode loss it let the player keep working a board the overlay says is over.+     */     override fun undo() {+        val game = _uiState.value.game ?: return+        if (game.isWon || game.isGameOver) return         val snapshot = history.removeLastOrNull() ?: return         mutate { it.copy(entries = snapshot.entries, notes = snapshot.notes, usedUndo = true) }         _uiState.update { it.copy(canUndo = history.isNotEmpty()) }
games/sudoku/src/main/java/com/vayunmathur/games/sudoku/ui/GameBoardScreen.kt
diff --git a/games/sudoku/src/main/java/com/vayunmathur/games/sudoku/ui/GameBoardScreen.kt b/games/sudoku/src/main/java/com/vayunmathur/games/sudoku/ui/GameBoardScreen.ktindex 3e5b2f2516..44a26c160f 100644--- a/games/sudoku/src/main/java/com/vayunmathur/games/sudoku/ui/GameBoardScreen.kt+++ b/games/sudoku/src/main/java/com/vayunmathur/games/sudoku/ui/GameBoardScreen.kt@@ -261,8 +261,8 @@ private fun ActionBar( ) {     val game = state.game ?: return     var confirmGiveUp by remember { mutableStateOf(false) }-    // Won or out of lives: the board takes no more input, so erase and hint go quiet too. Undo-    // stays as it is — it only restores entries, which cannot revive a finished game either way.+    // Won or out of lives: the board takes no more input, so erase, undo and hint go quiet too.+    // Undo in particular would otherwise restore pre-win entries under a board still marked won.     val ended = game.isWon || game.isGameOver     Column(verticalArrangement = Arrangement.spacedBy(Spacing.sm)) {         Row(@@ -294,7 +294,7 @@ private fun ActionBar(             OutlinedButton(                 onClick = { actions.undo() },                 modifier = Modifier.weight(1f),-                enabled = state.canUndo,+                enabled = state.canUndo && !ended,                 contentPadding = ActionPadding,             ) {                 ActionLabel(
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 bc12b6fb36..d96efc6959 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@@ -336,6 +336,47 @@ class SudokuViewModelTest {         assertEquals(entries, vm.uiState.value.game!!.entries)     } +    @Test+    fun undoIsANoOpOnceTheGameIsWon() {+        val vm = startedVm()+        var guard = 0+        while (guard++ < 100) {+            val game = vm.uiState.value.game!!+            val next = game.blankIndices().firstOrNull() ?: break+            vm.selectCell(next)+            vm.enterDigit(game.solution[next])+        }+        val won = vm.uiState.value.game!!+        assertTrue(won.isWon)+        assertTrue(vm.uiState.value.canUndo, "history is still there; undo must refuse it")++        vm.undo()+        val after = vm.uiState.value.game!!+        assertEquals(won.entries, after.entries, "undo cannot reopen a solved grid")+        assertTrue(after.isComplete)+        assertFalse(after.usedUndo)+    }++    @Test+    fun undoIsANoOpOnceAnswerModeRunsOutOfLives() {+        val vm = startedVm(GameConfig(BoardSize.SIX, Difficulty.EASY, answerMode = true))+        val first = blank(vm)+        vm.selectCell(first)+        vm.enterDigit(vm.uiState.value.game!!.solution[first])+        // A correct placement first, so there is history that undo would otherwise pop.+        assertTrue(vm.uiState.value.canUndo)+        val game = vm.uiState.value.game!!+        val second = game.blankIndices().first()+        val wrong = (1..game.size.side).first { it != game.solution[second] }+        vm.selectCell(second)+        repeat(3) { vm.enterDigit(wrong) }+        val over = vm.uiState.value.game!!+        assertTrue(over.isGameOver)++        vm.undo()+        assertEquals(over.entries, vm.uiState.value.game!!.entries, "undo cannot revive a lost board")+    }+     @Test     fun replayingASolvedPuzzleDoesNotRecordASecondWin() {         val vm = startedVm()