sudoku: new-game options inline on the home screen
- Commit
587a0afdcc39e6db5a27058155a03ec6f24175bb- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-01 18:26 UTC (1 month ago)
- Parents
5370f0f
Board size, difficulty and answer mode now sit above the New Game button instead of in a popup, giving the difficulty segments the full width so they no longer truncate. Answer mode travels in GameConfig, so a game re-dealt after process death keeps its ruleset; the stored preference only seeds the switch.
Diff
games/sudoku/AGENTS.md
diff --git a/games/sudoku/AGENTS.md b/games/sudoku/AGENTS.mdindex 27f3ecd761..f2df2d1013 100644--- a/games/sudoku/AGENTS.md+++ b/games/sudoku/AGENTS.md@@ -13,7 +13,7 @@ _Install: ./install games/sudoku (dev by default)._ ## Overview **Purpose:** Classic sudoku — 6×6/9×9/12×12 generated boards, exactly one solution each; pencil marks, hints, undo; illegal digits rejected. 100% offline.-**Game shape:** `ui/` board + home + config → `platform/SudokuViewModel` → `domain/` generator + solver → `data/` models + stats; achievements → `:sdk:games` → hub.+**Game shape:** `ui/` board + home → `platform/SudokuViewModel` → `domain/` generator + solver → `data/` models + stats; achievements → `:sdk:games` → hub. **Entry points:** `Route` → `MainActivity.kt` → `Navigation.kt` → `ui/GameBoardScreen.kt` → `platform/SudokuViewModel.kt` → `domain/SudokuGenerator.kt`. **Gotchas:** Unique-solution guarantee is the product — generator changes must pass the solver's uniqueness check. @@ -32,14 +32,13 @@ _Install: ./install games/sudoku (dev by default)._ - com/vayunmathur/games/sudoku/platform/SudokuUiContract.kt - com/vayunmathur/games/sudoku/platform/SudokuAchievementsManager.kt - com/vayunmathur/games/sudoku/platform/AppBackupAgent.kt-### ui/ — board + home + config+### ui/ — board + home - com/vayunmathur/games/sudoku/ui/GameBoardScreen.kt - com/vayunmathur/games/sudoku/ui/GameScreen.kt - com/vayunmathur/games/sudoku/ui/HomeScreen.kt - com/vayunmathur/games/sudoku/ui/DisplayNames.kt - com/vayunmathur/games/sudoku/ui/components/NumberPad.kt - com/vayunmathur/games/sudoku/ui/components/SudokuGrid.kt-- com/vayunmathur/games/sudoku/ui/dialogs/GameConfigDialog.kt ## Verify (this module only) ```
games/sudoku/src/main/java/com/vayunmathur/games/sudoku/data/SudokuDataStore.kt
diff --git a/games/sudoku/src/main/java/com/vayunmathur/games/sudoku/data/SudokuDataStore.kt b/games/sudoku/src/main/java/com/vayunmathur/games/sudoku/data/SudokuDataStore.ktindex 61f088a470..b89a1e8fdf 100644--- a/games/sudoku/src/main/java/com/vayunmathur/games/sudoku/data/SudokuDataStore.kt+++ b/games/sudoku/src/main/java/com/vayunmathur/games/sudoku/data/SudokuDataStore.kt@@ -9,17 +9,10 @@ import kotlinx.coroutines.flow.map private val Context.dataStore by preferencesDataStore(name = "settings") -/**- * The answer-mode switch on the home screen.- *- * Persisted on its own rather than folded into the stats record because it is a preference, not a- * result: stats are keyed per variant and rewritten on every game, while this is one global default- * read when a puzzle is dealt. The game itself carries a snapshot — see- * [SudokuGameState.answerMode] — so toggling mid-game cannot rewrite the rules under the player.- */+/** Remembers the last answer-mode choice from the new-game dialog so it can be pre-filled. */ class SudokuDataStore(private val context: Context) { - /** Whether new puzzles reject wrong digits and cost lives. Off until the player opts in. */+ /** Off until the player opts in. */ val answerMode: Flow<Boolean> = context.dataStore.data.map { it[ANSWER_MODE_KEY] ?: false } suspend fun setAnswerMode(enabled: Boolean) {
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 c4780f0cf9..74b2740f8f 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@@ -74,6 +74,7 @@ enum class Difficulty(val clueFraction: Double) { data class GameConfig( val size: BoardSize = BoardSize.NINE, val difficulty: Difficulty = Difficulty.MEDIUM,+ val answerMode: Boolean = false, ) { /** Stats key component, so records are tracked per board shape and difficulty. */ val variant: String get() = "${size.name}_${difficulty.name}"
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 8c040e1b5c..90195555e8 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@@ -84,21 +84,9 @@ class SudokuViewModel(application: Application) : AndroidViewModel(application), * Deals a new puzzle for [config], replacing anything in progress. * * Generation is dispatched to [Dispatchers.Default] and the board is blanked first, so the- * screen shows a spinner instead of freezing while a 9x9 Expert is dug. The game snapshots the- * current answer-mode preference rather than observing it, so the rules it is dealt with are- * the rules it keeps even if the switch moves mid-game.+ * screen shows a spinner instead of freezing while a 9x9 Expert is dug. */ fun newGame(config: GameConfig) {- newGame(config, answerMode.value)- }-- /**- * Deals a new puzzle with an explicit ruleset, for previews and tests.- *- * The no-flag overload above is the one the screens call; this one lets a caller deal an answer- * game without touching the persisted preference.- */- fun newGame(config: GameConfig, answerMode: Boolean) { history.clear() madeMistake = false rejectionAttempt = 0@@ -108,7 +96,7 @@ class SudokuViewModel(application: Application) : AndroidViewModel(application), SudokuGenerator.generate(config.size, config.difficulty, Random.Default) } statsRepository.recordGamePlayed(config.size, config.difficulty)- _uiState.value = SudokuUiState(game = SudokuGameState.from(puzzle, answerMode))+ _uiState.value = SudokuUiState(game = SudokuGameState.from(puzzle, config.answerMode)) } }
games/sudoku/src/main/java/com/vayunmathur/games/sudoku/ui/HomeScreen.kt
diff --git a/games/sudoku/src/main/java/com/vayunmathur/games/sudoku/ui/HomeScreen.kt b/games/sudoku/src/main/java/com/vayunmathur/games/sudoku/ui/HomeScreen.ktindex e7bbda3911..be1216f4a0 100644--- a/games/sudoku/src/main/java/com/vayunmathur/games/sudoku/ui/HomeScreen.kt+++ b/games/sudoku/src/main/java/com/vayunmathur/games/sudoku/ui/HomeScreen.kt@@ -6,29 +6,36 @@ import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.padding+import androidx.compose.foundation.rememberScrollState+import androidx.compose.foundation.verticalScroll import androidx.compose.runtime.Composable import androidx.compose.runtime.collectAsState import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf-import androidx.compose.runtime.remember+import androidx.compose.runtime.saveable.rememberSaveable import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.res.stringResource import androidx.compose.ui.text.style.TextAlign+import androidx.compose.ui.text.style.TextOverflow import com.vayunmathur.games.sudoku.R import com.vayunmathur.games.sudoku.Route import com.vayunmathur.games.sudoku.data.BoardSize+import com.vayunmathur.games.sudoku.data.Difficulty import com.vayunmathur.games.sudoku.data.GameConfig import com.vayunmathur.games.sudoku.platform.SudokuViewModel-import com.vayunmathur.games.sudoku.ui.dialogs.GameConfigDialog import com.vayunmathur.library.ui.AppScaffold import com.vayunmathur.library.ui.Button import com.vayunmathur.library.ui.Card import com.vayunmathur.library.ui.DesktopMaxWidthContainer+import com.vayunmathur.library.ui.ExperimentalMaterial3Api import com.vayunmathur.library.ui.MaterialTheme import com.vayunmathur.library.ui.R as UiR+import com.vayunmathur.library.ui.SegmentedButton+import com.vayunmathur.library.ui.SegmentedButtonDefaults import com.vayunmathur.library.ui.SettingsSwitchRow+import com.vayunmathur.library.ui.SingleChoiceSegmentedButtonRow import com.vayunmathur.library.ui.Spacing import com.vayunmathur.library.ui.Text import com.vayunmathur.library.ui.appBarScrollBehavior@@ -36,6 +43,7 @@ import com.vayunmathur.library.ui.game.GameTopBarActions import com.vayunmathur.library.ui.game.formatDuration import com.vayunmathur.library.util.NavBackStack +@OptIn(ExperimentalMaterial3Api::class) @Composable fun HomeScreen( backStack: NavBackStack<Route>,@@ -43,9 +51,8 @@ fun HomeScreen( ) { val uiState by viewModel.uiState.collectAsState() val answerMode by viewModel.answerMode.collectAsState()- var showConfig by remember { mutableStateOf(false) }- // Seeds the picker with the last thing played, so a player grinding one setting taps twice.- var lastConfig by remember { mutableStateOf(GameConfig()) }+ var size by rememberSaveable { mutableStateOf(GameConfig().size) }+ var difficulty by rememberSaveable { mutableStateOf(GameConfig().difficulty) } AppScaffold( title = stringResource(R.string.app_name),@@ -60,6 +67,7 @@ fun HomeScreen( Column( Modifier .fillMaxSize()+ .verticalScroll(rememberScrollState()) .padding(Spacing.lg), horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = Arrangement.spacedBy(Spacing.md),@@ -69,19 +77,54 @@ fun HomeScreen( Button( onClick = { backStack.add(- Route.Game(GameConfig(active.size, active.difficulty))+ Route.Game(GameConfig(active.size, active.difficulty, active.answerMode)) ) }, modifier = Modifier.fillMaxWidth(), ) { Text(stringResource(UiR.string.continue_label)) } }- Button(- onClick = { showConfig = true },- modifier = Modifier.fillMaxWidth(),- ) { Text(stringResource(UiR.string.new_game)) }-- // The only setting: whether new puzzles reject wrong digits. Changing it never touches- // a game in progress — the board snapshots the value when it is dealt.+ Text(+ stringResource(R.string.board_size),+ style = MaterialTheme.typography.labelLarge,+ )+ SingleChoiceSegmentedButtonRow(Modifier.fillMaxWidth()) {+ BoardSize.entries.forEachIndexed { index, value ->+ SegmentedButton(+ shape = SegmentedButtonDefaults.itemShape(index, BoardSize.entries.size),+ onClick = { size = value },+ selected = size == value,+ modifier = Modifier.weight(1f),+ ) {+ Text(+ value.displayName(),+ maxLines = 1,+ softWrap = false,+ overflow = TextOverflow.Ellipsis,+ )+ }+ }+ }+ Text(+ stringResource(R.string.difficulty),+ style = MaterialTheme.typography.labelLarge,+ )+ SingleChoiceSegmentedButtonRow(Modifier.fillMaxWidth()) {+ Difficulty.entries.forEachIndexed { index, value ->+ SegmentedButton(+ shape = SegmentedButtonDefaults.itemShape(index, Difficulty.entries.size),+ onClick = { difficulty = value },+ selected = difficulty == value,+ modifier = Modifier.weight(1f),+ ) {+ Text(+ value.displayName(),+ maxLines = 1,+ softWrap = false,+ overflow = TextOverflow.Ellipsis,+ )+ }+ }+ } SettingsSwitchRow( title = stringResource(R.string.answer_mode), supportingText = stringResource(R.string.answer_mode_description),@@ -89,31 +132,26 @@ fun HomeScreen( onCheckedChange = { viewModel.setAnswerMode(it) }, modifier = Modifier.fillMaxWidth(), )+ Button(+ onClick = {+ val config = GameConfig(size, difficulty, answerMode)+ viewModel.newGame(config)+ backStack.add(Route.Game(config))+ },+ modifier = Modifier.fillMaxWidth(),+ ) { Text(stringResource(UiR.string.new_game)) } Row( Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.spacedBy(Spacing.sm), ) {- BoardSize.entries.forEach { size ->- SizeStatsCard(size, viewModel, Modifier.weight(1f))+ BoardSize.entries.forEach { boardSize ->+ SizeStatsCard(boardSize, viewModel, Modifier.weight(1f)) } } } } }-- if (showConfig) {- GameConfigDialog(- initial = lastConfig,- onStart = { config ->- showConfig = false- lastConfig = config- viewModel.newGame(config)- backStack.add(Route.Game(config))- },- onDismiss = { showConfig = false },- )- } } @Composable
games/sudoku/src/main/java/com/vayunmathur/games/sudoku/ui/dialogs/GameConfigDialog.kt
diff --git a/games/sudoku/src/main/java/com/vayunmathur/games/sudoku/ui/dialogs/GameConfigDialog.kt b/games/sudoku/src/main/java/com/vayunmathur/games/sudoku/ui/dialogs/GameConfigDialog.ktdeleted file mode 100644index b9d09cbfa4..0000000000--- a/games/sudoku/src/main/java/com/vayunmathur/games/sudoku/ui/dialogs/GameConfigDialog.kt+++ /dev/null@@ -1,110 +0,0 @@-package com.vayunmathur.games.sudoku.ui.dialogs--import androidx.compose.foundation.layout.Arrangement-import androidx.compose.foundation.layout.Column-import androidx.compose.foundation.layout.fillMaxWidth-import androidx.compose.runtime.Composable-import androidx.compose.runtime.getValue-import androidx.compose.runtime.mutableStateOf-import androidx.compose.runtime.remember-import androidx.compose.runtime.setValue-import androidx.compose.ui.Alignment-import androidx.compose.ui.Modifier-import androidx.compose.ui.res.stringResource-import androidx.compose.ui.text.style.TextOverflow-import com.vayunmathur.games.sudoku.R-import com.vayunmathur.games.sudoku.data.BoardSize-import com.vayunmathur.games.sudoku.data.Difficulty-import com.vayunmathur.games.sudoku.data.GameConfig-import com.vayunmathur.games.sudoku.ui.displayName-import com.vayunmathur.library.ui.AlertDialog-import com.vayunmathur.library.ui.Button-import com.vayunmathur.library.ui.ExperimentalMaterial3Api-import com.vayunmathur.library.ui.MaterialTheme-import com.vayunmathur.library.ui.R as UiR-import com.vayunmathur.library.ui.SegmentedButton-import com.vayunmathur.library.ui.SegmentedButtonDefaults-import com.vayunmathur.library.ui.SingleChoiceSegmentedButtonRow-import com.vayunmathur.library.ui.Spacing-import com.vayunmathur.library.ui.Text-import com.vayunmathur.library.ui.TextButton--/**- * Picks the board size and difficulty for a new puzzle.- *- * Both choices are local state: nothing reaches the ViewModel until the player confirms, so backing- * out cannot disturb a game already in progress.- */-@OptIn(ExperimentalMaterial3Api::class)-@Composable-fun GameConfigDialog(- initial: GameConfig,- onStart: (GameConfig) -> Unit,- onDismiss: () -> Unit,-) {- var size by remember { mutableStateOf(initial.size) }- var difficulty by remember { mutableStateOf(initial.difficulty) }-- AlertDialog(- onDismissRequest = onDismiss,- title = { Text(stringResource(R.string.new_puzzle)) },- text = {- Column(- Modifier.fillMaxWidth(),- horizontalAlignment = Alignment.CenterHorizontally,- verticalArrangement = Arrangement.spacedBy(Spacing.md),- ) {- Text(- stringResource(R.string.board_size),- style = MaterialTheme.typography.labelLarge,- )- SingleChoiceSegmentedButtonRow(Modifier.fillMaxWidth()) {- BoardSize.entries.forEachIndexed { index, value ->- SegmentedButton(- shape = SegmentedButtonDefaults.itemShape(index, BoardSize.entries.size),- onClick = { size = value },- selected = size == value,- modifier = Modifier.weight(1f),- ) {- Text(- value.displayName(),- maxLines = 1,- softWrap = false,- overflow = TextOverflow.Ellipsis,- )- }- }- }- Text(- stringResource(R.string.difficulty),- style = MaterialTheme.typography.labelLarge,- )- SingleChoiceSegmentedButtonRow(Modifier.fillMaxWidth()) {- Difficulty.entries.forEachIndexed { index, value ->- SegmentedButton(- shape = SegmentedButtonDefaults.itemShape(index, Difficulty.entries.size),- onClick = { difficulty = value },- selected = difficulty == value,- modifier = Modifier.weight(1f),- ) {- Text(- value.displayName(),- maxLines = 1,- softWrap = false,- overflow = TextOverflow.Ellipsis,- )- }- }- }- Button(- onClick = { onStart(GameConfig(size, difficulty)) },- modifier = Modifier.fillMaxWidth(),- ) { Text(stringResource(UiR.string.new_game)) }- }- },- confirmButton = {},- dismissButton = {- TextButton(onClick = onDismiss) { Text(stringResource(UiR.string.back)) }- },- )-}
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 9510d2a4b8..a476fbc5eb 100644--- a/games/sudoku/src/main/res/values/strings.xml+++ b/games/sudoku/src/main/res/values/strings.xml@@ -3,18 +3,16 @@ <!-- Manifest / HomeScreen TopAppBar: App name. Noun, proper --> <string name="app_name" translatable="false">Sudoku</string> - <!-- GameConfigDialog: Title of the size and difficulty picker dialog. Verb phrase -->- <string name="new_puzzle">New Puzzle</string>- <!-- GameConfigDialog: Label above the board size segmented buttons. Noun -->+ <!-- HomeScreen: Label above the board size segmented buttons. Noun --> <string name="board_size">Board size</string>- <!-- GameConfigDialog: Label above the difficulty segmented buttons. Noun -->+ <!-- HomeScreen: Label above the difficulty segmented buttons. Noun --> <string name="difficulty">Difficulty</string> - <!-- GameConfigDialog: Board size option, a 6 by 6 grid of 2x3 boxes. Noun, dimensions -->+ <!-- HomeScreen: 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 -->+ <!-- HomeScreen: 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 -->+ <!-- HomeScreen: Board size option, a 12 by 12 grid of 3x4 boxes. Noun, dimensions --> <string name="size_twelve" translatable="false">12x12</string> <!-- HomeScreen: Title of the answer-mode setting row. Noun phrase -->@@ -30,7 +28,7 @@ <!-- GameOverOverlay: Starts a fresh puzzle with the same size and difficulty. Verb phrase --> <string name="try_again">Try Again</string> - <!-- GameConfigDialog: Hardest difficulty, fewest clues given. Adjective -->+ <!-- HomeScreen: Hardest difficulty, fewest clues given. Adjective --> <string name="difficulty_expert">Expert</string> <!-- GameBoardScreen action bar: Toggles whether taps write pencil marks. Noun used as a label -->