Modern Apps git

← Modern-Apps.git

games-sudoku: make erasing an empty cell a no-op

Commit
d052aeb766aa35e8416a1411f9d631db14808acc
Author
Vayun Mathur <[email protected]>
When
2026-11-07 20:00 UTC (28 days ago)
Parents
cdfa86d
clearCell pushed an undo snapshot and incremented moveCount even when
the selected cell held neither an entry nor pencil marks, leaving an
Undo press that visibly changed nothing.

It now returns early when there is nothing to clear. Clearing a cell
that holds an entry or marks behaves exactly as before.

Covered by SudokuViewModelTest.clearingAnAlreadyEmptyCellIsANoOp.

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 6aa4a9bb02..b28c9320a6 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@@ -221,6 +221,9 @@ class SudokuViewModel(application: Application) : AndroidViewModel(application),         val index = game.selected         if (index !in 0 until game.size.cellCount || game.isGiven(index)) return         if (game.isWon || game.isGameOver) return+        // Nothing to clear: pushing history here would leave an undo step that visibly+        // does nothing and count a move the player never made.+        if (game.entries[index] == 0 && game.notes[index] == 0) return         pushHistory(game)         mutate { state ->             state.copy(
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 606c964c05..8150e75e64 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@@ -167,6 +167,17 @@ class SudokuViewModelTest {         assertEquals(snapshot.moveCount, vm.uiState.value.game!!.moveCount)     } +    @Test+    fun clearingAnAlreadyEmptyCellIsANoOp() {+        val vm = startedVm()+        val cell = blank(vm)+        vm.selectCell(cell)+        vm.clearCell()+        val after = vm.uiState.value.game!!+        assertEquals(0, after.moveCount, "erasing nothing is not a move")+        assertFalse(vm.uiState.value.canUndo, "and leaves no phantom undo step")+    }+     @Test     fun placingADigitRemovesItFromPeerNotesOnly() {         val vm = startedVm()