sudoku: replace 4x4 with 12x12 and make the pad a real grid
- Commit
9b4c9fb43697c6d7242e3b46083ec2c0851dda71- Author
- vayun-mathur <[email protected]>
- When
- 2026-08-24 18:42 UTC (3 months ago)
- Parents
a40c6b1
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 5e744c89cf..f7efe02379 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@@ -5,16 +5,16 @@ import kotlinx.serialization.Serializable /** * The three board shapes on offer, each with the box geometry that goes with it. *- * [boxRows] x [boxCols] must equal [side], because a Sudoku box has to hold exactly as many cells- * as a row does — that is what makes "every digit once per box" the same strength of constraint as- * "every digit once per row". 6x6 is the only asymmetric one (2 rows of 3), which is why the box+ * [boxRows] x [boxCols] must equal [side], because a Sudoku box has to hold exactly as many cells as a+ * row does — that is what makes "every digit once per box" the same strength of constraint as "every+ * digit once per row". Only 9x9 has square boxes; 6x6 uses 2x3 and 12x12 uses 3x4, which is why the box * dimensions are stored rather than derived from a square root. */ @Serializable enum class BoardSize(val side: Int, val boxRows: Int, val boxCols: Int) {- FOUR(4, 2, 2), SIX(6, 2, 3),- NINE(9, 3, 3);+ NINE(9, 3, 3),+ TWELVE(12, 3, 4); val cellCount: Int get() = side * side @@ -26,14 +26,24 @@ enum class BoardSize(val side: Int, val boxRows: Int, val boxCols: Int) { } } +/**+ * The character shown for [digit], which is 1-based.+ *+ * Past nine the symbols become letters. A 12x12 cell has to fit a pencil-mark grid of twelve+ * candidates, and "10", "11", "12" at that size are illegible; one character each keeps both the+ * entered digit and the marks readable. The solver and generator only ever deal in the numbers.+ */+fun sudokuSymbol(digit: Int): String =+ if (digit <= 9) digit.toString() else ('A' + digit - 10).toString()+ /** * How many cells the generator leaves filled in. * * The clue counts are fractions of the board rather than absolute numbers so one difficulty means- * roughly the same amount of deduction on a 4x4 as on a 9x9. [clueFraction] is a target, not a- * guarantee: digging stops early if removing any remaining clue would leave the puzzle with more- * than one solution, so an EXPERT 4x4 often lands well above its target simply because tiny grids- * run out of removable cells.+ * roughly the same amount of deduction on a 6x6 as on a 12x12. [clueFraction] is a target, not a+ * guarantee: digging stops early if removing any remaining clue would leave the puzzle with more than+ * one solution, so a small board often lands well above its target simply because it runs out of+ * removable cells. */ @Serializable enum class Difficulty(val clueFraction: Double) {
games/sudoku/src/main/java/com/vayunmathur/games/sudoku/ui/DisplayNames.kt
diff --git a/games/sudoku/src/main/java/com/vayunmathur/games/sudoku/ui/DisplayNames.kt b/games/sudoku/src/main/java/com/vayunmathur/games/sudoku/ui/DisplayNames.ktindex 1f748d6f22..401fc39d81 100644--- a/games/sudoku/src/main/java/com/vayunmathur/games/sudoku/ui/DisplayNames.kt+++ b/games/sudoku/src/main/java/com/vayunmathur/games/sudoku/ui/DisplayNames.kt@@ -13,9 +13,9 @@ import com.vayunmathur.games.sudoku.data.Difficulty @Composable fun BoardSize.displayName(): String = stringResource( when (this) {- BoardSize.FOUR -> R.string.size_four BoardSize.SIX -> R.string.size_six BoardSize.NINE -> R.string.size_nine+ BoardSize.TWELVE -> R.string.size_twelve } )
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 f9891f21f2..644d32c58c 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@@ -1,27 +1,39 @@ package com.vayunmathur.games.sudoku.ui.components -import androidx.compose.foundation.layout.Arrangement+import androidx.compose.foundation.border import androidx.compose.foundation.layout.Column+import androidx.compose.foundation.layout.PaddingValues import androidx.compose.foundation.layout.Row-import androidx.compose.foundation.layout.fillMaxWidth+import androidx.compose.foundation.layout.size import androidx.compose.runtime.Composable+import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier+import androidx.compose.ui.graphics.RectangleShape+import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp import com.vayunmathur.games.sudoku.data.SudokuGameState+import com.vayunmathur.games.sudoku.data.sudokuSymbol import com.vayunmathur.library.ui.Button-import com.vayunmathur.library.ui.ButtonDefaults import com.vayunmathur.library.ui.FilledTonalButton import com.vayunmathur.library.ui.MaterialTheme-import com.vayunmathur.library.ui.Spacing import com.vayunmathur.library.ui.Text /**- * The digit keys.+ * The digit keys, laid out as a grid in the board's own box shape. *- * 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.+ * Reusing [SudokuGameState.size]'s box geometry means the pad is a miniature of a box — 2x3 for 6x6,+ * 3x3 for 9x9, 3x4 for 12x12 — so a digit sits in the same relative position on the pad as it does in+ * the pencil marks.+ *+ * Keys are fixed-size squares packed flush against each other rather than stretched across the width:+ * a full-width grid would give keys around 90dp across on a 12x12, which crowds out the board. A+ * 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. */ @Composable fun NumberPad(@@ -29,33 +41,21 @@ fun NumberPad( onDigit: (Int) -> Unit, modifier: Modifier = Modifier, ) {- val side = game.size.side- // 4 and 6 fit one row; 9 splits so the keys stay wide enough to hit.- val perRow = if (side > 6) 5 else side- Column(modifier, verticalArrangement = Arrangement.spacedBy(Spacing.sm)) {- var digit = 1- while (digit <= side) {- val rowEnd = minOf(digit + perRow - 1, side)- Row(- Modifier.fillMaxWidth(),- horizontalArrangement = Arrangement.spacedBy(Spacing.sm),- ) {- for (d in digit..rowEnd) {- val exhausted = !game.notesMode && game.placedCount(d) >= side+ val size = game.size+ Column(modifier, horizontalAlignment = Alignment.CenterHorizontally) {+ for (row in 0 until size.boxRows) {+ Row {+ for (col in 0 until size.boxCols) {+ val digit = row * size.boxCols + col + 1 DigitKey(- digit = d,- enabled = !exhausted && !game.isWon,+ digit = digit,+ enabled = !game.isWon &&+ (game.notesMode || game.placedCount(digit) < size.side), highlighted = game.notesMode,- onClick = { onDigit(d) },- modifier = Modifier.weight(1f),+ onClick = { onDigit(digit) }, ) }- // Keeps the last row's keys the same width as a full row's.- repeat(perRow - (rowEnd - digit + 1)) {- Column(Modifier.weight(1f)) {}- } }- digit = rowEnd + 1 } } }@@ -66,28 +66,34 @@ private fun DigitKey( enabled: Boolean, highlighted: Boolean, onClick: () -> Unit,- modifier: Modifier = Modifier, ) {+ val shared = Modifier+ .size(KeySize)+ // Drawn in the background colour, so it separates neighbouring keys without adding a gap.+ .border(HairlineWidth, MaterialTheme.colorScheme.background) val label = @Composable {- Text(- text = digit.toString(),- style = MaterialTheme.typography.headlineSmall,- fontSize = 22.sp,- )+ Text(text = sudokuSymbol(digit), fontSize = 19.sp) } if (highlighted) { FilledTonalButton( onClick = onClick,- modifier = modifier,+ modifier = shared, enabled = enabled,- contentPadding = ButtonDefaults.TextButtonContentPadding,+ shape = RectangleShape,+ contentPadding = PaddingValues(0.dp), ) { label() } } else { Button( onClick = onClick,- modifier = modifier,+ modifier = shared, enabled = enabled,- contentPadding = ButtonDefaults.TextButtonContentPadding,+ shape = RectangleShape,+ contentPadding = PaddingValues(0.dp), ) { label() } } }++/** Comfortably above the 48dp touch target once the hairline is accounted for, and no larger. */+private val KeySize = 50.dp++private val HairlineWidth = 1.dp
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 8943e897c3..23e73a803d 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@@ -27,6 +27,7 @@ import androidx.compose.ui.unit.sp import com.vayunmathur.games.sudoku.R import com.vayunmathur.games.sudoku.data.BoardSize import com.vayunmathur.games.sudoku.data.SudokuGameState+import com.vayunmathur.games.sudoku.data.sudokuSymbol import com.vayunmathur.library.ui.MaterialTheme import com.vayunmathur.library.ui.Text import androidx.compose.ui.res.stringResource@@ -113,7 +114,7 @@ private fun SudokuCell( // wrong-cell colour because a wrong digit is refused rather than written. val digitColor = if (given) scheme.onSurface else scheme.primary - val spoken = if (value == 0) stringResource(R.string.cd_empty) else value.toString()+ val spoken = if (value == 0) stringResource(R.string.cd_empty) else sudokuSymbol(value) val side = game.size.side val description = stringResource(R.string.cd_cell, index / side + 1, index % side + 1, spoken)@@ -125,7 +126,7 @@ private fun SudokuCell( ) { when { value != 0 -> Text(- text = value.toString(),+ text = sudokuSymbol(value), color = digitColor, // Sized from the cell so a 4x4 does not render tiny digits in large squares. fontSize = (game.size.digitScale * 22).sp,@@ -166,7 +167,7 @@ private fun NoteMarks(size: BoardSize, mask: Int) { ) { if (mask and (1 shl (digit - 1)) != 0) { Text(- text = digit.toString(),+ text = sudokuSymbol(digit), color = MaterialTheme.colorScheme.onSurfaceVariant, fontSize = (size.digitScale * 8).sp, lineHeight = (size.digitScale * 9).sp,@@ -215,10 +216,10 @@ private fun Modifier.drawGridLines( } } -/** Scales type with the board: a 4x4 cell is more than twice the width of a 9x9 cell. */+/** Scales type with the board: a 6x6 cell is twice the width of a 12x12 cell. */ private val BoardSize.digitScale: Float get() = when (this) {- BoardSize.FOUR -> 1.6f BoardSize.SIX -> 1.25f BoardSize.NINE -> 1f+ BoardSize.TWELVE -> 0.78f }
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 8f97f16af5..92f805b762 100644--- a/games/sudoku/src/main/res/values/strings.xml+++ b/games/sudoku/src/main/res/values/strings.xml@@ -16,12 +16,12 @@ <!-- GameConfigDialog / WinOverlay: Dismiss button. Verb --> <string name="back">Back</string> - <!-- GameConfigDialog: Board size option, a 4 by 4 grid of 2x2 boxes. Noun, dimensions -->- <string name="size_four" translatable="false">4x4</string> <!-- GameConfigDialog: Board size option, a 6 by 6 grid of 2x3 boxes. Noun, dimensions --> <string name="size_six" translatable="false">6x6</string> <!-- GameConfigDialog: Board size option, a 9 by 9 grid of 3x3 boxes. Noun, dimensions --> <string name="size_nine" translatable="false">9x9</string>+ <!-- GameConfigDialog: Board size option, a 12 by 12 grid of 3x4 boxes. Noun, dimensions -->+ <string name="size_twelve" translatable="false">12x12</string> <!-- GameConfigDialog: Easiest difficulty, most clues given. Adjective --> <string name="difficulty_easy">Easy</string>
games/sudoku/src/screenshotTest/kotlin/com/vayunmathur/games/sudoku/MetadataPreviews.kt
diff --git a/games/sudoku/src/screenshotTest/kotlin/com/vayunmathur/games/sudoku/MetadataPreviews.kt b/games/sudoku/src/screenshotTest/kotlin/com/vayunmathur/games/sudoku/MetadataPreviews.ktindex 54f807920b..fb61a36f8c 100644--- a/games/sudoku/src/screenshotTest/kotlin/com/vayunmathur/games/sudoku/MetadataPreviews.kt+++ b/games/sudoku/src/screenshotTest/kotlin/com/vayunmathur/games/sudoku/MetadataPreviews.kt@@ -62,9 +62,26 @@ class MetadataPreviews { } @PreviewTest- @Preview(name = "3-small", device = PHONE, showSystemUi = true)+ @Preview(name = "3-twelve", device = PHONE, showSystemUi = true) @Composable- fun Preview3Small() {+ fun Preview3Twelve() {+ // The largest board, where digits past nine are lettered and the pad becomes a 3x4 grid.+ Board(+ game(+ size = BoardSize.TWELVE,+ difficulty = Difficulty.MEDIUM,+ seed = 1212,+ filled = 18,+ selected = 40,+ elapsed = 604,+ )+ )+ }++ @PreviewTest+ @Preview(name = "4-six", device = PHONE, showSystemUi = true)+ @Composable+ fun Preview4Six() { Board( game( size = BoardSize.SIX,@@ -78,9 +95,9 @@ class MetadataPreviews { } @PreviewTest- @Preview(name = "4-solved", device = PHONE, showSystemUi = true)+ @Preview(name = "5-solved", device = PHONE, showSystemUi = true) @Composable- fun Preview4Solved() {+ fun Preview5Solved() { val base = game(BoardSize.NINE, Difficulty.EXPERT, seed = 2024, filled = 0, elapsed = 511) Board(base.copy(entries = base.blanksFilled(), isWon = true)) }
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 c4798afb60..7ac0b50243 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@@ -9,7 +9,7 @@ import kotlin.test.assertTrue class SudokuGameStateTest { - private val puzzle = SudokuGenerator.generate(BoardSize.FOUR, Difficulty.EASY, Random(1))+ private val puzzle = SudokuGenerator.generate(BoardSize.SIX, Difficulty.EASY, Random(1)) private val fresh = SudokuGameState.from(puzzle) /** A cell the player is allowed to fill in. */
games/sudoku/src/test/java/com/vayunmathur/games/sudoku/domain/SudokuGenerationCostTest.kt
diff --git a/games/sudoku/src/test/java/com/vayunmathur/games/sudoku/domain/SudokuGenerationCostTest.kt b/games/sudoku/src/test/java/com/vayunmathur/games/sudoku/domain/SudokuGenerationCostTest.ktnew file mode 100644index 0000000000..de9b145a05--- /dev/null+++ b/games/sudoku/src/test/java/com/vayunmathur/games/sudoku/domain/SudokuGenerationCostTest.kt@@ -0,0 +1,50 @@+package com.vayunmathur.games.sudoku.domain++import com.vayunmathur.games.sudoku.data.BoardSize+import com.vayunmathur.games.sudoku.data.Difficulty+import kotlin.random.Random+import kotlin.system.measureTimeMillis+import kotlin.test.Test+import kotlin.test.assertTrue++/**+ * Guards the cost of generation.+ *+ * Puzzles are dug on device, one uniqueness check per removed clue, and the board shows a spinner until+ * that finishes. 12x12 Expert is the worst case by a wide margin: 144 cells, the loosest clue target,+ * and non-square boxes. If this ever creeps into seconds the fix is to raise that difficulty's clue+ * fraction, not to let players stare at a spinner.+ */+class SudokuGenerationCostTest {++ @Test+ fun theWorstCaseBoardGeneratesQuickly() {+ val elapsed = measureTimeMillis {+ SudokuGenerator.generate(BoardSize.TWELVE, Difficulty.EXPERT, Random(1))+ }+ assertTrue(elapsed < BUDGET_MILLIS, "12x12 Expert took ${elapsed}ms, budget ${BUDGET_MILLIS}ms")+ }++ @Test+ fun everySizeAndDifficultyStaysWithinBudget() {+ for (size in BoardSize.entries) {+ for (difficulty in Difficulty.entries) {+ val elapsed = measureTimeMillis {+ SudokuGenerator.generate(size, difficulty, Random(7))+ }+ assertTrue(+ elapsed < BUDGET_MILLIS,+ "${size.name}/${difficulty.name} took ${elapsed}ms, budget ${BUDGET_MILLIS}ms",+ )+ }+ }+ }++ private companion object {+ /**+ * Generous enough not to flake on a loaded CI machine, tight enough that a real regression -+ * the kind that makes the largest board unplayable - still trips it.+ */+ const val BUDGET_MILLIS = 4_000L+ }+}
games/sudoku/src/test/java/com/vayunmathur/games/sudoku/domain/SudokuSolverTest.kt
diff --git a/games/sudoku/src/test/java/com/vayunmathur/games/sudoku/domain/SudokuSolverTest.kt b/games/sudoku/src/test/java/com/vayunmathur/games/sudoku/domain/SudokuSolverTest.ktindex 2d387620e3..dd054cdf39 100644--- a/games/sudoku/src/test/java/com/vayunmathur/games/sudoku/domain/SudokuSolverTest.kt+++ b/games/sudoku/src/test/java/com/vayunmathur/games/sudoku/domain/SudokuSolverTest.kt@@ -39,7 +39,7 @@ class SudokuSolverTest { @Test fun contradictoryGridHasNoSolution() {- val size = BoardSize.FOUR+ val size = BoardSize.SIX // Two 1s in the same row cannot both be right. val grid = blank(size).toMutableList().also { it[0] = 1@@ -51,7 +51,7 @@ class SudokuSolverTest { @Test fun emptyGridHasManySolutions() {- val size = BoardSize.FOUR+ val size = BoardSize.SIX // Capped at 2: the point is only that it is not unique. assertEquals(2, SudokuSolver(size).countSolutions(blank(size))) }@@ -65,13 +65,14 @@ class SudokuSolverTest { @Test fun countStopsAtTheGivenLimit() {- val size = BoardSize.FOUR+ val size = BoardSize.SIX assertEquals(1, SudokuSolver(size).countSolutions(blank(size), limit = 1)) // Subtree totals are added whole, so the result can overshoot the limit - it is a // "found at least this many" answer, which is all the uniqueness check needs. val capped = SudokuSolver(size).countSolutions(blank(size), limit = 5) assertTrue(capped >= 5, "expected at least 5, got $capped")- assertTrue(capped < 288, "should have stopped well short of all 288 solutions, got $capped")+ // A 6x6 has 28,800 completions, so stopping early has to bite well short of that.+ assertTrue(capped < 1_000, "should have stopped early, got $capped") } @Test
metadata_data/games-sudoku.md
diff --git a/metadata_data/games-sudoku.md b/metadata_data/games-sudoku.mdindex cc5b5604d6..8b81ae928b 100644--- a/metadata_data/games-sudoku.md+++ b/metadata_data/games-sudoku.md@@ -2,5 +2,5 @@ A classic number puzzle, generated fresh every game Pick a board size and a difficulty and play a puzzle generated on the spot. Every puzzle has exactly one solution, so it can always be finished by reasoning alone,-and a digit that does not belong simply will not go in. Includes 4x4, 6x6 and 9x9-grids, pencil marks, hints and undo.+and a digit that does not belong simply will not go in. Includes 6x6, 9x9 and+12x12 grids, pencil marks, hints and undo.
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 5e9a00ae73..69887c00ab 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/2.png
diff --git a/metadata_data/photos/games-sudoku/2.png b/metadata_data/photos/games-sudoku/2.pngindex e6dd3fb305..b402c39f57 100644Binary files a/metadata_data/photos/games-sudoku/2.png and b/metadata_data/photos/games-sudoku/2.png differ
metadata_data/photos/games-sudoku/3.png
diff --git a/metadata_data/photos/games-sudoku/3.png b/metadata_data/photos/games-sudoku/3.pngindex 6790cba554..0cd9b4c137 100644Binary files a/metadata_data/photos/games-sudoku/3.png and b/metadata_data/photos/games-sudoku/3.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 74ac9178f8..a908868177 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.pngnew file mode 100644index 0000000000..4901d4731bBinary files /dev/null and b/metadata_data/photos/games-sudoku/5.png differ