sudoku: accept wrong digits silently and never disable keys
- Commit
4c81ef8cd8717af32801cffbe41ab30232cf249c- Author
- vayun-mathur <[email protected]>
- When
- 2026-08-24 18:49 UTC (3 months ago)
- Parents
9b4c9fb
Diff
games/sudoku/src/main/assets/achievements.json
diff --git a/games/sudoku/src/main/assets/achievements.json b/games/sudoku/src/main/assets/achievements.jsonindex 7462bd8444..2b8c82c8ed 100644--- a/games/sudoku/src/main/assets/achievements.json+++ b/games/sudoku/src/main/assets/achievements.json@@ -22,7 +22,7 @@ { "id": "no_mistakes", "name": "Flawless",- "description": "Complete a puzzle without a single wrong guess."+ "description": "Complete a puzzle without ever entering a wrong digit." }, { "id": "speed_nine",
games/sudoku/src/main/java/com/vayunmathur/games/sudoku/data/SudokuModels.kt
diff --git a/games/sudoku/src/main/java/com/vayunmathur/games/sudoku/data/SudokuModels.kt b/games/sudoku/src/main/java/com/vayunmathur/games/sudoku/data/SudokuModels.ktindex f7efe02379..ea478a171d 100644--- a/games/sudoku/src/main/java/com/vayunmathur/games/sudoku/data/SudokuModels.kt+++ b/games/sudoku/src/main/java/com/vayunmathur/games/sudoku/data/SudokuModels.kt@@ -71,8 +71,8 @@ data class GameConfig( * A generated puzzle: the clues the player starts with and the unique grid they lead to. * * Both lists are [BoardSize.cellCount] long and use 0 for "blank"; digits are 1-based. Keeping the- * solution alongside the clues is what lets a hint fill one correct cell without re-running the- * solver, and lets a digit that does not belong be refused as it is entered.+ * solution alongside the clues is what lets a hint fill or correct one cell without re-running the+ * solver, and what the completion check compares against. */ data class Puzzle( val size: BoardSize,@@ -112,26 +112,29 @@ data class SudokuGameState( fun isGiven(index: Int): Boolean = givens[index] != 0 /**- * Whether [digit] may be written into [index].+ * True when [index] holds a digit that disagrees with the solution. *- * A digit that disagrees with the solution is refused outright rather than written and flagged, so- * the grid only ever holds correct entries. That is why there is no "wrong cell" state to render:- * it cannot occur.- *- * Clearing a cell is not a write and does not go through here.+ * Nothing in the UI draws this. A wrong digit goes in silently and stays there; the player finds+ * out because the puzzle refuses to complete, and a hint will fix it. It exists so [wrongIndices]+ * can find those cells. */- fun accepts(index: Int, digit: Int): Boolean =- !isWon &&- index in 0 until size.cellCount &&- !isGiven(index) &&- digit == solution[index]+ fun isWrong(index: Int): Boolean {+ val value = valueAt(index)+ return value != 0 && value != solution[index]+ } - /** How many of [digit] are already placed, so the number pad can retire finished digits. */- fun placedCount(digit: Int): Int = (0 until size.cellCount).count { valueAt(it) == digit }+ /** Filled cells that disagree with the solution, which a hint fixes before filling anything new. */+ fun wrongIndices(): List<Int> = (0 until size.cellCount).filter { isWrong(it) } - /** Cells with nothing in them yet, which is what a hint picks from. */+ /** Cells with nothing in them yet. */ fun blankIndices(): List<Int> = (0 until size.cellCount).filter { valueAt(it) == 0 } + /**+ * Complete only when every cell matches the solution.+ *+ * A full grid is not enough: since wrong digits are accepted without comment, this is the only+ * thing that tells the player they have something wrong somewhere.+ */ val isComplete: Boolean get() = (0 until size.cellCount).all { valueAt(it) == solution[it] } companion object {
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 95b283e46d..358d71d449 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@@ -89,17 +89,17 @@ class SudokuViewModel(application: Application) : AndroidViewModel(application), * Re-entering the digit already showing clears the cell instead, so the number pad doubles as a * toggle and the player rarely needs the separate erase button. *- * A confirmed digit that disagrees with the solution is refused: nothing is written, no move is- * counted, and the board does not change. The attempt is still remembered for the `no_mistakes`- * achievement, which is the only trace a wrong guess leaves.+ * A digit that disagrees with the solution goes in exactly like any other, with no highlight and no+ * complaint. The player finds out only because [checkWin] never fires, which keeps the deduction+ * theirs to do; the attempt is remembered for the `no_mistakes` achievement. */ override fun enterDigit(digit: Int) { val game = _uiState.value.game ?: return val index = game.selected if (index !in 0 until game.size.cellCount || game.isGiven(index) || game.isWon) return + pushHistory(game) if (game.notesMode) {- pushHistory(game) val bit = 1 shl (digit - 1) mutate { state -> state.copy(@@ -111,21 +111,13 @@ class SudokuViewModel(application: Application) : AndroidViewModel(application), return } - // Tapping the digit already there means "take it back", which is always allowed.- if (game.entries[index] == digit) {- clearCell()- return- }- if (!game.accepts(index, digit)) {- madeMistake = true- return- }-- pushHistory(game)+ // Tapping the digit already there means "take it back".+ val next = if (game.entries[index] == digit) 0 else digit+ if (next != 0 && next != game.solution[index]) madeMistake = true mutate { state -> state.copy(- entries = state.entries.replacing(index, digit),- // A confirmed digit makes its own pencil marks meaningless.+ entries = state.entries.replacing(index, next),+ // A written digit makes its own pencil marks meaningless. notes = state.notes.replacing(index, 0), moveCount = state.moveCount + 1, )@@ -148,16 +140,18 @@ class SudokuViewModel(application: Application) : AndroidViewModel(application), } /**- * Fills one cell with its correct digit.+ * Fixes one cell, correcting a mistake before filling anything new. *- * Prefers the selected cell so a stuck player gets help where they are looking; otherwise it takes- * the first blank. Entries are always correct, so a blank cell is the only thing a hint can help- * with.+ * Errors come first wherever they are on the board: a hint spent on a fresh cell while a wrong+ * digit sits elsewhere would leave the player deducing from something false. Only once the grid is+ * consistent does it fill a blank, preferring the selected one so help lands where they are+ * looking. */ override fun hint() { val game = _uiState.value.game ?: return if (game.isWon) return- val index = game.selected.takeIf { it >= 0 && !game.isGiven(it) && game.valueAt(it) == 0 }+ val index = game.wrongIndices().firstOrNull()+ ?: game.selected.takeIf { it >= 0 && !game.isGiven(it) && game.valueAt(it) == 0 } ?: game.blankIndices().firstOrNull() ?: return
games/sudoku/src/main/java/com/vayunmathur/games/sudoku/ui/components/NumberPad.kt
diff --git a/games/sudoku/src/main/java/com/vayunmathur/games/sudoku/ui/components/NumberPad.kt b/games/sudoku/src/main/java/com/vayunmathur/games/sudoku/ui/components/NumberPad.ktindex 644d32c58c..86b6070145 100644--- a/games/sudoku/src/main/java/com/vayunmathur/games/sudoku/ui/components/NumberPad.kt+++ b/games/sudoku/src/main/java/com/vayunmathur/games/sudoku/ui/components/NumberPad.kt@@ -30,10 +30,8 @@ import com.vayunmathur.library.ui.Text * hairline in the background colour separates them, so the block reads as distinct keys without * needing gaps. *- * A digit that has been placed as many times as the board is wide is disabled rather than hidden, so- * the keys never reflow under the player's thumb mid-game. Notes mode keeps every digit enabled,- * because a pencil mark for a finished digit is still a legitimate thing to jot down while ruling it- * out.+ * No key is ever disabled. A wrong digit is accepted silently, so there is nothing to gate on, and a+ * digit that already appears the maximum number of times may still be one the player wants to move. */ @Composable fun NumberPad(@@ -49,8 +47,6 @@ fun NumberPad( val digit = row * size.boxCols + col + 1 DigitKey( digit = digit,- enabled = !game.isWon &&- (game.notesMode || game.placedCount(digit) < size.side), highlighted = game.notesMode, onClick = { onDigit(digit) }, )@@ -63,7 +59,6 @@ fun NumberPad( @Composable private fun DigitKey( digit: Int,- enabled: Boolean, highlighted: Boolean, onClick: () -> Unit, ) {@@ -78,7 +73,6 @@ private fun DigitKey( FilledTonalButton( onClick = onClick, modifier = shared,- enabled = enabled, shape = RectangleShape, contentPadding = PaddingValues(0.dp), ) { label() }@@ -86,7 +80,6 @@ private fun DigitKey( Button( onClick = onClick, modifier = shared,- enabled = enabled, shape = RectangleShape, contentPadding = PaddingValues(0.dp), ) { label() }
games/sudoku/src/test/java/com/vayunmathur/games/sudoku/data/SudokuGameStateTest.kt
diff --git a/games/sudoku/src/test/java/com/vayunmathur/games/sudoku/data/SudokuGameStateTest.kt b/games/sudoku/src/test/java/com/vayunmathur/games/sudoku/data/SudokuGameStateTest.ktindex 7ac0b50243..158849ea61 100644--- a/games/sudoku/src/test/java/com/vayunmathur/games/sudoku/data/SudokuGameStateTest.kt+++ b/games/sudoku/src/test/java/com/vayunmathur/games/sudoku/data/SudokuGameStateTest.kt@@ -16,42 +16,60 @@ class SudokuGameStateTest { private val blank = puzzle.givens.indices.first { puzzle.givens[it] == 0 } /** A digit that does not belong in [blank]. */- private val wrongDigit =- (1..puzzle.size.side).first { it != puzzle.solution[blank] }+ private val wrongDigit = (1..puzzle.size.side).first { it != puzzle.solution[blank] }++ private fun SudokuGameState.withEntry(index: Int, digit: Int) =+ copy(entries = entries.toMutableList().also { it[index] = digit })++ @Test+ fun aWrongDigitIsRecognisedButNotBlocked() {+ // Wrong digits go in silently; nothing stops them, and nothing draws attention to them.+ val s = fresh.withEntry(blank, wrongDigit)+ assertTrue(s.isWrong(blank))+ assertEquals(wrongDigit, s.valueAt(blank), "the wrong digit should still be on the board")+ } @Test- fun theCorrectDigitIsAccepted() {- assertTrue(fresh.accepts(blank, puzzle.solution[blank]))+ fun aCorrectDigitIsNotWrong() {+ assertFalse(fresh.withEntry(blank, puzzle.solution[blank]).isWrong(blank)) } @Test- fun aWrongDigitIsRefused() {- // The whole point: the grid never holds a digit that disagrees with the solution.- assertFalse(fresh.accepts(blank, wrongDigit))+ fun anEmptyCellIsNotWrong() {+ assertFalse(fresh.isWrong(blank)) } @Test- fun givenCellsRefuseEverything() {+ fun givensAreNeverWrong() { val given = puzzle.givens.indices.first { puzzle.givens[it] != 0 }- for (digit in 1..puzzle.size.side) {- assertFalse(fresh.accepts(given, digit), "digit $digit into a given")+ assertFalse(fresh.isWrong(given))+ }++ @Test+ fun aFullBoardWithAMistakeDoesNotComplete() {+ // This is the only signal the player gets that something is off.+ val entries = List(puzzle.size.cellCount) {+ if (fresh.isGiven(it)) 0 else puzzle.solution[it] }+ val solved = fresh.copy(entries = entries)+ assertTrue(solved.isComplete)++ val spoiled = solved.withEntry(blank, wrongDigit)+ assertFalse(spoiled.isComplete, "a full grid with a wrong digit must not count as complete")+ assertTrue(spoiled.blankIndices().isEmpty(), "and it really is full") } @Test- fun outOfRangeCellsAreRefused() {- assertFalse(fresh.accepts(-1, 1))- assertFalse(fresh.accepts(puzzle.size.cellCount, 1))+ fun wrongIndicesFindEveryMistake() {+ val second = puzzle.givens.indices.last { puzzle.givens[it] == 0 }+ val s = fresh.withEntry(blank, wrongDigit).withEntry(second, wrongDigitFor(second))+ assertEquals(listOf(blank, second).sorted(), s.wrongIndices()) } @Test- fun aFinishedGridRefusesFurtherWrites() {- val solved = fresh.copy(- entries = List(puzzle.size.cellCount) { if (fresh.isGiven(it)) 0 else puzzle.solution[it] },- isWon = true,- )- assertTrue(solved.isComplete)- assertFalse(solved.accepts(blank, puzzle.solution[blank]))+ fun wrongIndicesIsEmptyOnACleanBoard() {+ assertTrue(fresh.wrongIndices().isEmpty())+ assertTrue(fresh.withEntry(blank, puzzle.solution[blank]).wrongIndices().isEmpty()) } @Test@@ -60,30 +78,15 @@ class SudokuGameStateTest { puzzle.givens.indices.filter { puzzle.givens[it] == 0 }, fresh.blankIndices(), )- // Filling one removes it from the list, which is what stops a hint reusing that cell.- val filled = fresh.copy(entries = fresh.entries.toMutableList().also {- it[blank] = puzzle.solution[blank]- })- assertFalse(blank in filled.blankIndices())+ assertFalse(blank in fresh.withEntry(blank, puzzle.solution[blank]).blankIndices()) } @Test- fun placedCountOnlyCountsWhatIsOnTheBoard() {- val digit = puzzle.solution[blank]- val before = fresh.placedCount(digit)- val after = fresh.copy(entries = fresh.entries.toMutableList().also {- it[blank] = digit- }).placedCount(digit)- assertEquals(before + 1, after)- }-- @Test- fun aGridIsCompleteOnlyWhenEveryCellMatchesTheSolution() {- assertFalse(fresh.isComplete)- val solved = fresh.copy(- entries = List(puzzle.size.cellCount) { if (fresh.isGiven(it)) 0 else puzzle.solution[it] }- )- assertTrue(solved.isComplete)+ fun aWrongEntryCountsAsFilledNotBlank() {+ // A hint has to treat it as an error to fix, not as an empty cell to fill.+ val s = fresh.withEntry(blank, wrongDigit)+ assertFalse(blank in s.blankIndices())+ assertTrue(blank in s.wrongIndices()) } @Test@@ -97,12 +100,28 @@ class SudokuGameStateTest { for (size in BoardSize.entries) { val counts = IntArray(size.side) for (index in 0 until size.cellCount) counts[size.boxOf(index)]++- // Every box must hold exactly as many cells as a row does, or the box constraint would- // be weaker or stronger than the row constraint.+ // Every box must hold exactly as many cells as a row does, or the box constraint would be+ // weaker or stronger than the row constraint. assertTrue( counts.all { it == size.side }, "${size.name} box sizes were ${counts.toList()}", ) } }++ @Test+ fun symbolsAreOneCharacterOnEveryBoard() {+ // A 12x12 pencil-mark grid has twelve candidates in one cell; two-character labels are unusable.+ for (size in BoardSize.entries) {+ for (digit in 1..size.side) {+ assertEquals(1, sudokuSymbol(digit).length, "${size.name} digit $digit")+ }+ }+ assertEquals("9", sudokuSymbol(9))+ assertEquals("A", sudokuSymbol(10))+ assertEquals("C", sudokuSymbol(12))+ }++ private fun wrongDigitFor(index: Int) =+ (1..puzzle.size.side).first { it != puzzle.solution[index] } }
metadata_data/photos/games-sudoku/1.png
diff --git a/metadata_data/photos/games-sudoku/1.png b/metadata_data/photos/games-sudoku/1.pngindex 69887c00ab..a37dba7c93 100644Binary files a/metadata_data/photos/games-sudoku/1.png and b/metadata_data/photos/games-sudoku/1.png differ
metadata_data/photos/games-sudoku/4.png
diff --git a/metadata_data/photos/games-sudoku/4.png b/metadata_data/photos/games-sudoku/4.pngindex a908868177..91f87be414 100644Binary files a/metadata_data/photos/games-sudoku/4.png and b/metadata_data/photos/games-sudoku/4.png differ
metadata_data/photos/games-sudoku/5.png
diff --git a/metadata_data/photos/games-sudoku/5.png b/metadata_data/photos/games-sudoku/5.pngindex 4901d4731b..3ea5ab6977 100644Binary files a/metadata_data/photos/games-sudoku/5.png and b/metadata_data/photos/games-sudoku/5.png differ