games-sudoku: clear peer notes when a digit is placed
- Commit
8f1e73e2426c62e0b48d8c1318510fcc96f9cc41- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-07 20:00 UTC (29 days ago)
- Parents
17ccb31
Writing a digit only cleared the pencil marks in its own cell, so the digit stayed pencilled into every cell in its row, column and box -- marks claiming a candidate the board had just ruled out, which the player then had to erase by hand after every placement. placeDigit and hint now write notes through notesAfterPlacement, which also clears the digit from all peer cells. Undo is unaffected: snapshots hold the pre-placement masks, so undoing restores the swept marks (asserted in the new test). Covered by SudokuViewModelTest.placingADigitRemovesItFromPeerNotesOnly.
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 90195555e8..11cbf10d73 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@@ -160,8 +160,9 @@ class SudokuViewModel(application: Application) : AndroidViewModel(application), mutate { state -> state.copy( entries = state.entries.replacing(index, digit),- // A written digit makes its own pencil marks meaningless.- notes = state.notes.replacing(index, 0),+ // A written digit makes its own pencil marks meaningless, and it is no+ // longer a candidate anywhere it can be seen from.+ notes = state.notesAfterPlacement(index, digit), moveCount = state.moveCount + 1, ) }@@ -242,7 +243,7 @@ class SudokuViewModel(application: Application) : AndroidViewModel(application), mutate { state -> state.copy( entries = state.entries.replacing(index, state.solution[index]),- notes = state.notes.replacing(index, 0),+ notes = state.notesAfterPlacement(index, state.solution[index]), selected = index, hintsUsed = state.hintsUsed + 1, moveCount = state.moveCount + 1,@@ -326,6 +327,29 @@ class SudokuViewModel(application: Application) : AndroidViewModel(application), manager.onProgressUpdated("win_streak_5", statsRepository.getBestWinStreak()) } + /**+ * The note masks after [digit] is written at [index]: the cell's own marks go, and the+ * digit stops being a candidate in every cell that shares its row, column or box.+ *+ * Without the peer sweep a placed digit stayed pencilled into its neighbours, so the+ * board kept claiming a digit was possible where it had just been ruled out — and the+ * player had to erase those marks by hand after every placement.+ */+ private fun SudokuGameState.notesAfterPlacement(index: Int, digit: Int): List<Int> {+ val clear = (1 shl (digit - 1)).inv()+ val row = index / size.side+ val col = index % size.side+ val box = size.boxOf(index)+ return notes.mapIndexed { other, mask ->+ when {+ other == index -> 0+ other / size.side == row || other % size.side == col ||+ size.boxOf(other) == box -> mask and clear+ else -> mask+ }+ }+ }+ private fun pushHistory(game: SudokuGameState) { history.addLast(SudokuSnapshot(game.entries, game.notes)) if (history.size > MAX_HISTORY) history.removeFirst()
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 530f155546..ac4cdf394d 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,45 @@ class SudokuViewModelTest { assertEquals(snapshot.moveCount, vm.uiState.value.game!!.moveCount) } + @Test+ fun placingADigitRemovesItFromPeerNotesOnly() {+ val vm = startedVm()+ val game = vm.uiState.value.game!!+ val side = game.size.side+ // Three blanks: one to fill, one sharing its row, one sharing neither row,+ // column nor box with it.+ val blanks = game.blankIndices()+ val target = blanks.first()+ val row = target / side+ val col = target % side+ val peer = blanks.first {+ it != target && it / side == row+ }+ val stranger = blanks.first {+ it / side != row && it % side != col && game.size.boxOf(it) != game.size.boxOf(target)+ }+ val digit = game.solution[target]+ val bit = 1 shl (digit - 1)++ vm.toggleNotesMode()+ vm.selectCell(peer); vm.enterDigit(digit)+ vm.selectCell(stranger); vm.enterDigit(digit)+ assertTrue(vm.uiState.value.game!!.notes[peer] and bit != 0)+ assertTrue(vm.uiState.value.game!!.notes[stranger] and bit != 0)++ vm.toggleNotesMode()+ vm.selectCell(target)+ vm.enterDigit(digit)+ val after = vm.uiState.value.game!!+ assertEquals(digit, after.valueAt(target))+ assertEquals(0, after.notes[peer] and bit, "a placed digit is no longer a peer candidate")+ assertTrue(after.notes[stranger] and bit != 0, "unrelated cells keep their marks")++ // Undo restores the marks the placement swept away.+ vm.undo()+ assertTrue(vm.uiState.value.game!!.notes[peer] and bit != 0)+ }+ @Test fun notesTapOnFilledCellKeepsTheEntry() { val vm = startedVm()