games-sudoku: highlight conflicting digits on the board
- Commit
0155e8610ea39246462287a9b72032b0496ea2f6- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-07 20:02 UTC (28 days ago)
- Parents
d052aeb
Conventional Sudoku apps mark mistakes visibly; this board marked none in classic mode -- a wrong digit went in silently and the only signal was the win never firing (answer mode, where wrong digits are refused, already flashes the pad and spends lives). The grid now marks conflicts: a digit repeated within its row, column or box. The check (SudokuGameState.conflictingIndices) is computed from the board alone, never from the solution, so it shows the player that two cells disagree without revealing which is wrong or what belongs there. Both copies are tinted with the error container; conflicting entries turn error-coloured, while a given keeps its own colour because the given is never the misplaced copy. Answer-mode and solved boards cannot contain a duplicate, so they are unchanged. Covered by SudokuGameStateTest conflict cases; the store previews place only correct digits, so they show no conflicts.
Diff
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 74b2740f8f..465ba41425 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@@ -145,9 +145,11 @@ data class SudokuGameState( /** * True when [index] holds a digit that disagrees with the solution. *- * 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.+ * The UI never draws this directly — marking it would hand out the answer. What the grid+ * marks instead is [conflictingIndices], the duplicates a player can see for themselves.+ * A wrong digit that duplicates nothing 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 isWrong(index: Int): Boolean { val value = valueAt(index)@@ -157,6 +159,34 @@ data class SudokuGameState( /** 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 whose digit is repeated by another cell in the same row, column or box.+ *+ * Unlike [isWrong] this needs no solution: a duplicate is visible from the board alone,+ * which is exactly why the grid can mark it without giving the answer away. Both copies+ * are reported — from the board alone there is no way to say which one is misplaced.+ * Solved and answer-mode boards can never contain one, because every entry on them+ * agrees with the solution.+ */+ fun conflictingIndices(): Set<Int> {+ val conflicts = mutableSetOf<Int>()+ for (index in 0 until size.cellCount) {+ val value = valueAt(index)+ if (value == 0) continue+ for (other in 0 until size.cellCount) {+ if (other == index || valueAt(other) != value) continue+ val peer = other / size.side == index / size.side ||+ other % size.side == index % size.side ||+ size.boxOf(other) == size.boxOf(index)+ if (peer) {+ conflicts.add(index)+ conflicts.add(other)+ }+ }+ }+ return conflicts+ }+ /** Cells with nothing in them yet. */ fun blankIndices(): List<Int> = (0 until size.cellCount).filter { valueAt(it) == 0 }
games/sudoku/src/main/java/com/vayunmathur/games/sudoku/ui/components/SudokuGrid.kt
diff --git a/games/sudoku/src/main/java/com/vayunmathur/games/sudoku/ui/components/SudokuGrid.kt b/games/sudoku/src/main/java/com/vayunmathur/games/sudoku/ui/components/SudokuGrid.ktindex 23e73a803d..91da64a9b1 100644--- a/games/sudoku/src/main/java/com/vayunmathur/games/sudoku/ui/components/SudokuGrid.kt+++ b/games/sudoku/src/main/java/com/vayunmathur/games/sudoku/ui/components/SudokuGrid.kt@@ -50,6 +50,9 @@ fun SudokuGrid( val scheme = MaterialTheme.colorScheme val selected = game.selected val selectedValue = if (selected >= 0) game.valueAt(selected) else 0+ // Digits repeated within a row, column or box. Computable from the board alone, so+ // marking them shows the player a mistake without revealing the solution.+ val conflicts = game.conflictingIndices() BoxWithConstraints(modifier.aspectRatio(1f)) { val cell: Dp = maxWidth / side@@ -68,6 +71,7 @@ fun SudokuGrid( isSameValue = selectedValue != 0 && index != selected && game.valueAt(index) == selectedValue,+ isConflict = index in conflicts, modifier = Modifier .size(cell) .clickable { onSelect(index) },@@ -97,6 +101,7 @@ private fun SudokuCell( isSelected: Boolean, isPeer: Boolean, isSameValue: Boolean,+ isConflict: Boolean, modifier: Modifier = Modifier, ) { val scheme = MaterialTheme.colorScheme@@ -105,14 +110,22 @@ private fun SudokuCell( val background = when { isSelected -> scheme.primary.copy(alpha = 0.40f)+ // A duplicated digit, visible from the board alone: marked in the error palette+ // without saying which copy is wrong, let alone what belongs there instead.+ isConflict -> scheme.errorContainer.copy(alpha = 0.55f) isSameValue -> scheme.tertiary.copy(alpha = 0.30f) isPeer -> scheme.primary.copy(alpha = 0.13f) else -> Color.Transparent } - // Givens are the puzzle, entries are the player's work, so they read differently. There is no- // wrong-cell colour because a wrong digit is refused rather than written.- val digitColor = if (given) scheme.onSurface else scheme.primary+ // Givens are the puzzle, entries are the player's work, so they read differently. A+ // conflicting entry turns error-coloured; a conflicting given keeps its own colour,+ // because the given is never the copy that is misplaced.+ val digitColor = when {+ isConflict && !given -> scheme.error+ given -> scheme.onSurface+ else -> scheme.primary+ } val spoken = if (value == 0) stringResource(R.string.cd_empty) else sudokuSymbol(value) val side = game.size.side
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 158849ea61..7ad158916f 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@@ -66,6 +66,39 @@ class SudokuGameStateTest { assertEquals(listOf(blank, second).sorted(), s.wrongIndices()) } + @Test+ fun duplicatedDigitsInAUnitConflict() {+ // A second blank in the same row as [blank], entered with the same digit.+ val side = puzzle.size.side+ val peer = puzzle.givens.indices.first {+ it != blank && it / side == blank / side && puzzle.givens[it] == 0+ }+ val s = fresh.withEntry(blank, 1).withEntry(peer, 1)+ val conflicts = s.conflictingIndices()+ assertTrue(blank in conflicts && peer in conflicts, "both copies are marked: $conflicts")+ assertTrue(conflicts.all { s.valueAt(it) == 1 }, "only the duplicated digit conflicts")+ }++ @Test+ fun anEntryDuplicatingAGivenConflictsWithIt() {+ val given = puzzle.givens.indices.first { puzzle.givens[it] != 0 }+ // The blank in the given's row that entering the given's digit would duplicate.+ val side = puzzle.size.side+ val cell = puzzle.givens.indices.first {+ puzzle.givens[it] == 0 && it / side == given / side+ }+ val s = fresh.withEntry(cell, puzzle.givens[given])+ val conflicts = s.conflictingIndices()+ assertTrue(cell in conflicts && given in conflicts, "entry and given are marked: $conflicts")+ assertTrue(conflicts.all { s.valueAt(it) == puzzle.givens[given] })+ }++ @Test+ fun correctEntriesAndEmptyBoardsHaveNoConflicts() {+ assertTrue(fresh.conflictingIndices().isEmpty())+ assertTrue(fresh.withEntry(blank, puzzle.solution[blank]).conflictingIndices().isEmpty())+ }+ @Test fun wrongIndicesIsEmptyOnACleanBoard() { assertTrue(fresh.wrongIndices().isEmpty())