Modern Apps git

← Modern-Apps.git

sudoku: underline wrong and repeated digits; announce cell state (#ux-audit)

Commit
d2c0c13d83967009a0d6a5c4d245b8e8163471a0
Author
Vayun Mathur <[email protected]>
When
2026-11-13 01:15 UTC (23 days ago)
Parents
79c610b
Symptom: a wrong entry (with Highlight mistakes on) and a digit
repeated in its row/column/box were distinguished only by colour - red
text and an error-tinted background - which colour-blind players and
greyscale displays cannot see. TalkBack users got only "Row r, column
c, <digit>": no indication of the cursor, of which digits are the
puzzle's givens versus their own entries, of mistakes, or of the pencil
marks in an empty cell.

Root cause: SudokuCell expressed all of that through colour and font
weight, and its clearAndSetSemantics block set only a content
description.

Fix:
- Every digit drawn in the error colour (a conflicting entry, or a
  wrong entry while highlighting is on) is also underlined.
- The cell's semantics now carry `selected` and a stateDescription
  built from: "given"/"entered", "incorrect" (only while Highlight
  mistakes is on, mirroring the red digit so TalkBack never reveals a
  hidden mistake), "repeated in row, column or box" (always, as it is
  derivable from the board), and "notes 1 4 7" for an empty cell's
  pencil marks. The content description is unchanged, so existing
  lookups and tests still match.
- Five new English strings with translator comments.

User-visible effect: wrong and repeated digits are underlined as well
as red; TalkBack announces e.g. "Row 3, column 7, 5, selected,
entered, incorrect" or "Row 1, column 2, empty, notes 1 3".

Verification: new SudokuComponentsTest cases assert selected /
not-selected, the given, entered, entered+incorrect and notes state
descriptions, that "incorrect" disappears when highlighting is turned
off, and that a duplicated digit is announced as a conflict.
:games:sudoku:testDevUnitTest in the toolchain container.

Diff

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 897b2046c1..139c3ea069 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@@ -21,8 +21,11 @@ import androidx.compose.ui.graphics.Color import androidx.compose.ui.platform.LocalDensity import androidx.compose.ui.semantics.clearAndSetSemantics import androidx.compose.ui.semantics.contentDescription+import androidx.compose.ui.semantics.selected+import androidx.compose.ui.semantics.stateDescription import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.text.style.TextAlign+import androidx.compose.ui.text.style.TextDecoration import androidx.compose.ui.unit.Dp import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.em@@ -138,9 +141,10 @@ private fun SudokuCell(     // apply. A conflicting given keeps its own colour, because the given is never the copy     // that is misplaced. Answer mode never has wrong entries to draw, since a wrong digit is     // refused rather than written.+    val shownWrong = !given && highlightMistakes && game.isWrong(index)+    val flagged = (isConflict && !given) || shownWrong     val digitColor = when {-        isConflict && !given -> scheme.error-        !given && highlightMistakes && game.isWrong(index) -> scheme.error+        flagged -> scheme.error         given -> scheme.onSurface         else -> scheme.primary     }@@ -149,10 +153,24 @@ private fun SudokuCell(     val side = game.size.side     val description =         stringResource(R.string.cd_cell, index / side + 1, index % side + 1, spoken)+    val state = cellStateDescription(+        given = given,+        hasValue = value != 0,+        shownWrong = shownWrong,+        isConflict = isConflict,+        notes = if (value == 0) game.notes[index] else 0,+        size = game.size,+    )     Box(         modifier             .background(background)-            .clearAndSetSemantics { contentDescription = description },+            .clearAndSetSemantics {+                contentDescription = description+                // TalkBack hears what sighted players see from colour and weight alone: the+                // cursor, given vs entered, the mistake markers, and the pencil marks.+                selected = isSelected+                if (state.isNotEmpty()) stateDescription = state+            },         contentAlignment = Alignment.Center,     ) {         when {@@ -164,6 +182,9 @@ private fun SudokuCell(                 fontSize = density.fitSp((game.size.digitScale * 22).sp, cell * DIGIT_CELL_FRACTION),                 lineHeight = LINE_HEIGHT_EM.em,                 fontWeight = if (given) FontWeight.Bold else FontWeight.Medium,+                // A shape cue as well as the error colour, so a wrong or duplicated entry is+                // visible to colour-blind players and in greyscale, not by hue alone.+                textDecoration = if (flagged) TextDecoration.Underline else null,                 textAlign = TextAlign.Center,             ) @@ -172,6 +193,35 @@ private fun SudokuCell(     } } +/**+ * The spoken state of one cell, beyond its position and digit.+ *+ * "Incorrect" follows the highlight-mistakes setting, exactly like the red digit, so TalkBack never+ * reveals a wrong entry the board is hiding; a duplicate is derivable from the board, so it is always+ * announced.+ */+@Composable+private fun cellStateDescription(+    given: Boolean,+    hasValue: Boolean,+    shownWrong: Boolean,+    isConflict: Boolean,+    notes: Int,+    size: BoardSize,+): String {+    val parts = buildList {+        if (hasValue) add(stringResource(if (given) R.string.cd_given else R.string.cd_entered))+        if (shownWrong) add(stringResource(R.string.cd_wrong))+        if (isConflict) add(stringResource(R.string.cd_conflict))+        if (notes != 0) {+            val marks = (1..size.side).filter { notes and (1 shl (it - 1)) != 0 }+                .joinToString(" ") { sudokuSymbol(it) }+            add(stringResource(R.string.cd_notes, marks))+        }+    }+    return parts.joinToString(", ")+}+ /**  * Pencil marks, arranged in the board's own box shape.  *
games/sudoku/src/main/res/values/strings.xml
diff --git a/games/sudoku/src/main/res/values/strings.xml b/games/sudoku/src/main/res/values/strings.xmlindex 939b49777b..5f1a4e978f 100644--- a/games/sudoku/src/main/res/values/strings.xml+++ b/games/sudoku/src/main/res/values/strings.xml@@ -64,4 +64,14 @@     <string name="cd_note_key">Note <xliff:g id="digit" example="7">%1$s</xliff:g></string>     <!-- GameBoardScreen: Spoken in place of a digit for a cell with nothing in it. Adjective -->     <string name="cd_empty">empty</string>+    <!-- GameBoardScreen: Spoken state of a cell whose digit was printed by the puzzle and cannot be changed. Adjective -->+    <string name="cd_given">given</string>+    <!-- GameBoardScreen: Spoken state of a cell whose digit the player typed. Verb past participle -->+    <string name="cd_entered">entered</string>+    <!-- GameBoardScreen: Spoken state of an entered digit that disagrees with the solution, only while mistakes are highlighted. Adjective -->+    <string name="cd_wrong">incorrect</string>+    <!-- GameBoardScreen: Spoken state of a digit repeated in the same row, column or box. Phrase -->+    <string name="cd_conflict">repeated in row, column or box</string>+    <!-- GameBoardScreen: Spoken pencil marks in an empty cell. %1$s = the pencilled digits separated by spaces, e.g. 1 4 7 -->+    <string name="cd_notes">notes <xliff:g id="digits" example="1 4 7">%1$s</xliff:g></string> </resources>
games/sudoku/src/test/java/com/vayunmathur/games/sudoku/ui/components/SudokuComponentsTest.kt
diff --git a/games/sudoku/src/test/java/com/vayunmathur/games/sudoku/ui/components/SudokuComponentsTest.kt b/games/sudoku/src/test/java/com/vayunmathur/games/sudoku/ui/components/SudokuComponentsTest.ktindex 113445103b..b122ef1bfc 100644--- a/games/sudoku/src/test/java/com/vayunmathur/games/sudoku/ui/components/SudokuComponentsTest.kt+++ b/games/sudoku/src/test/java/com/vayunmathur/games/sudoku/ui/components/SudokuComponentsTest.kt@@ -5,7 +5,10 @@ import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.setValue import androidx.compose.ui.test.assertCountEquals+import androidx.compose.ui.semantics.SemanticsProperties import androidx.compose.ui.test.assertIsNotEnabled+import androidx.compose.ui.test.assertIsNotSelected+import androidx.compose.ui.test.assertIsSelected import androidx.compose.ui.test.junit4.createComposeRule import androidx.compose.ui.test.onAllNodesWithText import androidx.compose.ui.test.onNodeWithContentDescription@@ -178,6 +181,69 @@ class SudokuComponentsTest {         rule.onNodeWithContentDescription(cellDesc(game, blank)).assertExists()     } +    @Test+    fun cellsAnnounceSelectionGivenEnteredWrongConflictAndNotes() {+        // 9x9 so some blank surely has a wrong digit that duplicates nothing.+        val base = game(BoardSize.NINE)+        val side = base.size.side+        val blanks = base.givens.indices.filter { base.givens[it] == 0 }+        val givenCell = base.givens.indices.first { base.givens[it] != 0 }+        // A wrong entry that duplicates nothing in its row, column or box.+        val (wrongCell, wrongDigit) = blanks.firstNotNullOf { cell ->+            (1..side).firstOrNull { d ->+                d != base.solution[cell] && base.copy(+                    entries = base.entries.toMutableList().also { it[cell] = d }+                ).conflictingIndices().isEmpty()+            }?.let { cell to it }+        }+        val correctCell = blanks.first { it != wrongCell }+        val notesCell = blanks.first { it != wrongCell && it != correctCell }+        val entries = base.entries.toMutableList().also {+            it[wrongCell] = wrongDigit+            it[correctCell] = base.solution[correctCell]+        }+        val notes = base.notes.toMutableList().also { it[notesCell] = 0b101 }+        val game = base.copy(entries = entries, notes = notes, selected = correctCell)+        var highlight by mutableStateOf(true)+        rule.setContent { SudokuGrid(game = game, onSelect = {}, highlightMistakes = highlight) }++        fun state(index: Int) = rule.onNodeWithContentDescription(cellDesc(game, index))+            .fetchSemanticsNode().config.getOrElse(SemanticsProperties.StateDescription) { "" }++        assertEquals(ctx.getString(R.string.cd_given), state(givenCell))+        rule.onNodeWithContentDescription(cellDesc(game, correctCell)).assertIsSelected()+        rule.onNodeWithContentDescription(cellDesc(game, givenCell)).assertIsNotSelected()+        assertEquals(ctx.getString(R.string.cd_entered), state(correctCell))+        assertEquals(+            "${ctx.getString(R.string.cd_entered)}, ${ctx.getString(R.string.cd_wrong)}",+            state(wrongCell),+        )+        assertEquals(ctx.getString(R.string.cd_notes, "1 3"), state(notesCell))++        // With highlighting off the board hides the mistake, so TalkBack must too.+        highlight = false+        assertEquals(ctx.getString(R.string.cd_entered), state(wrongCell))+    }++    @Test+    fun duplicatedDigitIsAnnouncedAsAConflict() {+        val base = game()+        val side = base.size.side+        // Copy a given into a blank of the same row: a duplicate visible from the board alone.+        val (blank, given) = base.givens.indices.firstNotNullOf { cell ->+            if (base.givens[cell] != 0) return@firstNotNullOf null+            val row = cell / side+            (row * side until row * side + side).firstOrNull { base.givens[it] != 0 }+                ?.let { cell to it }+        }+        val entries = base.entries.toMutableList().also { it[blank] = base.givens[given] }+        val game = base.copy(entries = entries)+        rule.setContent { SudokuGrid(game = game, onSelect = {}) }+        val state = rule.onNodeWithContentDescription(cellDesc(game, blank))+            .fetchSemanticsNode().config.getOrElse(SemanticsProperties.StateDescription) { "" }+        assertTrue(ctx.getString(R.string.cd_conflict) in state, state)+    }+     @Test     fun numberPadTwelveUsesLetterKeys() {         val tapped = mutableListOf<Int>()