games-sudoku: make erasing an empty cell a no-op
- Commit
9ccdf2c633ec4eff86af93bfd0f120aa3c4984ca- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-07 20:00 UTC (29 days ago)
- Parents
f7ba9d5
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()