games-sudoku: ignore note taps on filled cells
- Commit
e088c70ce3fd60f0d93f1feffc110a2524893346- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-07 19:59 UTC (28 days ago)
- Parents
cf0a1e3
In notes mode, tapping a digit on a cell that already held an entry silently erased that entry: AnswerMode.decide returned Note for any usable selection, and SudokuViewModel.enterNote writes the mark by clearing entries[index]. A player who filled a cell, switched to notes mode and tapped a candidate lost the entry without asking to. decide now ignores note taps on filled cells (entries[index] != 0; givens are already excluded by the usable check), leaving the entry and its marks untouched. Notes on empty cells are unchanged. Covered by AnswerModeTest.notesOnAFilledCellAreIgnoredRatherThanErasingTheEntry and SudokuViewModelTest.notesTapOnFilledCellKeepsTheEntry.
Diff
games/sudoku/src/main/java/com/vayunmathur/games/sudoku/domain/AnswerMode.kt
diff --git a/games/sudoku/src/main/java/com/vayunmathur/games/sudoku/domain/AnswerMode.kt b/games/sudoku/src/main/java/com/vayunmathur/games/sudoku/domain/AnswerMode.ktindex 82a5516854..08fbb8311a 100644--- a/games/sudoku/src/main/java/com/vayunmathur/games/sudoku/domain/AnswerMode.kt+++ b/games/sudoku/src/main/java/com/vayunmathur/games/sudoku/domain/AnswerMode.kt@@ -13,7 +13,9 @@ import com.vayunmathur.games.sudoku.data.SudokuGameState * The table, in order: * * - no usable selection (nothing selected, a given cell, or an ended game) is ignored;- * - notes mode always toggles a pencil mark โ possibilities are never judged and cost nothing;+ * - notes mode toggles a pencil mark on an empty cell โ possibilities are never judged and cost+ * nothing โ but a filled cell has nothing to pencil over, so the tap is ignored there instead+ * of deleting the entry to make room for marks; * - re-tapping the digit already showing erases the cell, in either mode; * - in answer mode a digit that disagrees with the solution is rejected: never placed, one life; * - anything else is placed, exactly as classic mode always has.@@ -34,7 +36,12 @@ object AnswerMode { !game.isWon && !game.isGameOver if (!usable) return DigitAttempt.Ignore- if (game.notesMode) return DigitAttempt.Note(index, digit)+ if (game.notesMode) {+ // Pencilling over an entered digit would have to delete it first (the ViewModel's+ // note write clears the entry), which no player means by tapping in notes mode.+ if (game.entries[index] != 0) return DigitAttempt.Ignore+ return DigitAttempt.Note(index, digit)+ } if (game.entries[index] == digit) return DigitAttempt.Erase(index) if (game.answerMode && digit != game.solution[index]) return DigitAttempt.Reject(digit) return DigitAttempt.Place(index, digit)
games/sudoku/src/test/java/com/vayunmathur/games/sudoku/domain/AnswerModeTest.kt
diff --git a/games/sudoku/src/test/java/com/vayunmathur/games/sudoku/domain/AnswerModeTest.kt b/games/sudoku/src/test/java/com/vayunmathur/games/sudoku/domain/AnswerModeTest.ktindex 500969787e..cd331b54e7 100644--- a/games/sudoku/src/test/java/com/vayunmathur/games/sudoku/domain/AnswerModeTest.kt+++ b/games/sudoku/src/test/java/com/vayunmathur/games/sudoku/domain/AnswerModeTest.kt@@ -53,6 +53,22 @@ class AnswerModeTest { assertIs<DigitAttempt.Note>(attempt) } + @Test+ fun notesOnAFilledCellAreIgnoredRatherThanErasingTheEntry() {+ // Pencilling must never delete work: a note tap on a cell that already holds a digit+ // is ignored, in both modes, whatever digit was tapped.+ val filled = classic.copy(+ selected = blank,+ notesMode = true,+ entries = classic.entries.toMutableList().also { it[blank] = rightDigit },+ )+ assertIs<DigitAttempt.Ignore>(AnswerMode.decide(filled, wrongDigit))+ assertIs<DigitAttempt.Ignore>(AnswerMode.decide(filled, rightDigit))+ assertIs<DigitAttempt.Ignore>(+ AnswerMode.decide(filled.copy(answerMode = true), wrongDigit),+ )+ }+ @Test fun retappingTheShownDigitErasesEvenInAnswerMode() { val game = fresh.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 535d28c0a8..530f155546 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,21 @@ class SudokuViewModelTest { assertEquals(snapshot.moveCount, vm.uiState.value.game!!.moveCount) } + @Test+ fun notesTapOnFilledCellKeepsTheEntry() {+ val vm = startedVm()+ val cell = blank(vm)+ val correct = vm.uiState.value.game!!.solution[cell]+ vm.selectCell(cell)+ vm.enterDigit(correct)+ vm.toggleNotesMode()+ val other = (1..vm.uiState.value.game!!.size.side).first { it != correct }+ vm.enterDigit(other)+ val after = vm.uiState.value.game!!+ assertEquals(correct, after.valueAt(cell), "pencilling over an entry must not erase it")+ assertEquals(0, after.notes[cell], "and no mark is written over it either")+ }+ @Test fun hintCorrectsWrongEntryFirstThenFillsSelectedBlank() { val vm = startedVm()