Modern Apps git

← Modern-Apps.git

games-nonogram: make the generation-failed retry actually reload the board

Commit
729ccf16c75c6d1634fa082cf99759e2c9161e7d
Author
Vayun Mathur <[email protected]>
When
2026-11-08 04:26 UTC (28 days ago)
Parents
7896a9e
When seeded generation found no solvable puzzle, the failure screen's
Try again button called restartLevel(), which only clears marks. No
marks flow feeds the load, so the failed screen was stuck: the same
seed would deterministically find the same nothing, and nothing ever
re-ran the search.

The button now calls a new NonogramGameActions.retryGeneration(),
which bumps a generation-attempt counter that is part of the load
session, so the board reloads with the same seed but a widened search:
each attempt adds RETRY_EXTRA_ROUNDS rounds (capped by
MAX_GENERATION_ROUNDS), keeping saved marks valid across a relaunch. A
fresh seed was rejected because it would orphan marks already saved
against the level.

User-visible effect: Try again on the generation-failed screen now
searches harder and loads a board instead of sitting on the dead
screen.

Verified with :games:nonogram:testDevUnitTest (68 tests green),
including the new retryGenerationReloadsBoard test and the updated
generationFailedOffersRetry screen test asserting the button retries
rather than restarts.

Diff

games/nonogram/src/main/java/com/vayunmathur/games/nonogram/domain/NonogramGenerator.kt
diff --git a/games/nonogram/src/main/java/com/vayunmathur/games/nonogram/domain/NonogramGenerator.kt b/games/nonogram/src/main/java/com/vayunmathur/games/nonogram/domain/NonogramGenerator.ktindex f221edd2ca..63d7827ed7 100644--- a/games/nonogram/src/main/java/com/vayunmathur/games/nonogram/domain/NonogramGenerator.kt+++ b/games/nonogram/src/main/java/com/vayunmathur/games/nonogram/domain/NonogramGenerator.kt@@ -62,7 +62,12 @@ object NonogramGenerator {     const val DEFAULT_FILL_RATIO = 0.55      private const val DEFAULT_ATTEMPTS = 60-    private const val DEFAULT_ROUNDS = 8++    /** Rounds the seeded search runs per attempt; retries add more rounds beyond this base. */+    const val DEFAULT_ROUNDS = 8++    /** Extra rounds each generation retry adds, so a retry widens a search that found nothing. */+    const val RETRY_EXTRA_ROUNDS = 8      /** Keeps the per-round seeds far apart so consecutive rounds do not repaint near-identical grids. */     private const val SEED_STRIDE = 1_000_003L
games/nonogram/src/main/java/com/vayunmathur/games/nonogram/platform/NonogramUiContract.kt
diff --git a/games/nonogram/src/main/java/com/vayunmathur/games/nonogram/platform/NonogramUiContract.kt b/games/nonogram/src/main/java/com/vayunmathur/games/nonogram/platform/NonogramUiContract.ktindex d7078f2268..93268f93c3 100644--- a/games/nonogram/src/main/java/com/vayunmathur/games/nonogram/platform/NonogramUiContract.kt+++ b/games/nonogram/src/main/java/com/vayunmathur/games/nonogram/platform/NonogramUiContract.kt@@ -24,6 +24,14 @@ interface NonogramGameActions {     fun setMarkMode(mode: MarkMode) {}     fun nextLevel() {}     fun restartLevel() {}++    /**+     * Retries a failed puzzle generation with a widened seed search.+     *+     * This is separate from [restartLevel] on purpose: restart only clears marks, which cannot+     * retrigger generation, and re-running the same seed would find the same nothing.+     */+    fun retryGeneration() {}     fun setGameMode(mode: GameMode) {}      companion object { val Noop: NonogramGameActions = object : NonogramGameActions {} }
games/nonogram/src/main/java/com/vayunmathur/games/nonogram/platform/NonogramViewModel.kt
diff --git a/games/nonogram/src/main/java/com/vayunmathur/games/nonogram/platform/NonogramViewModel.kt b/games/nonogram/src/main/java/com/vayunmathur/games/nonogram/platform/NonogramViewModel.ktindex 01e5ac7c8b..61c78c6954 100644--- a/games/nonogram/src/main/java/com/vayunmathur/games/nonogram/platform/NonogramViewModel.kt+++ b/games/nonogram/src/main/java/com/vayunmathur/games/nonogram/platform/NonogramViewModel.kt@@ -52,6 +52,14 @@ class NonogramViewModel(application: Application) : AndroidViewModel(application     private var casualPuzzle: Pair<Int, NonogramPuzzle>? = null     private var dailyPuzzle: Pair<Long, NonogramPuzzle>? = null +    /**+     * How many generation retries have been asked for. Part of the session so a retry reloads the+     * board; the attempt count only ever widens the seed search, never changes its start, so a+     * puzzle found on retry N is the same one a fresh process finds on its own retry N โ€” which is+     * what keeps saved marks valid across a relaunch.+     */+    private val _generationAttempt = MutableStateFlow(0)+     val reminderEnabled: StateFlow<Boolean> = reminderSettings.enabled         .stateIn(viewModelScope, SharingStarted.WhileSubscribed(5_000), false) @@ -78,7 +86,8 @@ class NonogramViewModel(application: Application) : AndroidViewModel(application                 dataStore.currentLevel,                 dataStore.markMode,                 dailyStore.currentStreak,-            ) { mode, level, markMode, streak -> Session(mode, level, markMode, streak) }+                _generationAttempt,+            ) { mode, level, markMode, streak, attempt -> Session(mode, level, markMode, streak, attempt) }                 .collectLatest { session -> load(session) }         }     }@@ -89,6 +98,7 @@ class NonogramViewModel(application: Application) : AndroidViewModel(application         val level: Int,         val markMode: MarkMode,         val streak: Long,+        val attempt: Int,     )      private suspend fun load(session: Session) {@@ -111,7 +121,14 @@ class NonogramViewModel(application: Application) : AndroidViewModel(application         val puzzle = withContext(Dispatchers.Default) {             val cached = if (daily) dailyPuzzle?.takeIf { it.first == day }?.second             else casualPuzzle?.takeIf { it.first == session.level }?.second-            cached ?: NonogramGenerator.generateSeeded(size, seed)+            // A retry keeps the same seed but searches more rounds: re-running the base search+            // would deterministically find the same nothing, while a fresh seed would orphan any+            // marks already saved against this level.+            val rounds = (+                NonogramGenerator.DEFAULT_ROUNDS ++                    session.attempt * NonogramGenerator.RETRY_EXTRA_ROUNDS+                ).coerceAtMost(MAX_GENERATION_ROUNDS)+            cached ?: NonogramGenerator.generateSeeded(size, seed, rounds)         }          if (puzzle == null) {@@ -249,6 +266,16 @@ class NonogramViewModel(application: Application) : AndroidViewModel(application         viewModelScope.launch { dataStore.clearMarks(daily) }     } +    /**+     * Retries generation after [NonogramUiState.generationFailed].+     *+     * Clearing marks cannot help here โ€” no marks flow feeds the load, so `restartLevel` leaves the+     * failed screen stuck โ€” and only a bump of [_generationAttempt] re-runs the search.+     */+    override fun retryGeneration() {+        _generationAttempt.value += 1+    }+     override fun setGameMode(mode: GameMode) {         viewModelScope.launch { dataStore.setGameMode(mode) }     }@@ -332,5 +359,13 @@ class NonogramViewModel(application: Application) : AndroidViewModel(application          /** Matches the "15x15" wording of the `big_picture` description. */         private const val BIG_PICTURE_SIZE = 15++        /**+         * Caps the widening retry search: eight attempts of [NonogramGenerator.RETRY_EXTRA_ROUNDS].+         * Each round is 60 solver checks over at most a 15x15 grid, so this bounds one retry at a+         * few seconds on [Dispatchers.Default] with the spinner showing, instead of growing a+         * longer search on every tap of Try again.+         */+        private const val MAX_GENERATION_ROUNDS = 64     } }
games/nonogram/src/main/java/com/vayunmathur/games/nonogram/ui/NonogramGameScreen.kt
diff --git a/games/nonogram/src/main/java/com/vayunmathur/games/nonogram/ui/NonogramGameScreen.kt b/games/nonogram/src/main/java/com/vayunmathur/games/nonogram/ui/NonogramGameScreen.ktindex c6058896f7..cbc4476229 100644--- a/games/nonogram/src/main/java/com/vayunmathur/games/nonogram/ui/NonogramGameScreen.kt+++ b/games/nonogram/src/main/java/com/vayunmathur/games/nonogram/ui/NonogramGameScreen.kt@@ -152,7 +152,7 @@ fun NonogramGameScreen(                         color = MaterialTheme.colorScheme.error,                         textAlign = TextAlign.Center,                     )-                    Button(onClick = { actions.restartLevel() }) {+                    Button(onClick = { actions.retryGeneration() }) {                         Text(stringResource(R.string.try_again))                     }                 }
games/nonogram/src/test/java/com/vayunmathur/games/nonogram/platform/NonogramViewModelTest.kt
diff --git a/games/nonogram/src/test/java/com/vayunmathur/games/nonogram/platform/NonogramViewModelTest.kt b/games/nonogram/src/test/java/com/vayunmathur/games/nonogram/platform/NonogramViewModelTest.ktindex a092b643df..dc34cd84db 100644--- a/games/nonogram/src/test/java/com/vayunmathur/games/nonogram/platform/NonogramViewModelTest.kt+++ b/games/nonogram/src/test/java/com/vayunmathur/games/nonogram/platform/NonogramViewModelTest.kt@@ -124,6 +124,15 @@ class NonogramViewModelTest {         await("daily completion recorded") { runBlocking { vm.dataStore.puzzlesCompleted.first() } > before }     } +    @Test fun retryGenerationReloadsBoard() {+        val vm = loadedVm()+        val before = vm.uiState.value.game!!+        vm.retryGeneration()+        await("reload after retry") { !vm.uiState.value.generating && vm.uiState.value.game != null }+        assertFalse(vm.uiState.value.generationFailed)+        assertEquals(before.puzzle, vm.uiState.value.game!!.puzzle)+    }+     @Test fun reminderActionsUpdateState() {         val vm = loadedVm()         val enabledSeen = java.util.Collections.synchronizedList(mutableListOf<Boolean>())@@ -147,6 +156,7 @@ class NonogramViewModelTest {         NonogramGameActions.Noop.tapCell(0); NonogramGameActions.Noop.crossCell(0)         NonogramGameActions.Noop.setMarkMode(MarkMode.CROSS); NonogramGameActions.Noop.nextLevel()         NonogramGameActions.Noop.restartLevel(); NonogramGameActions.Noop.setGameMode(GameMode.DAILY)+        NonogramGameActions.Noop.retryGeneration()         SettingsActions.Noop.setReminderEnabled(true); SettingsActions.Noop.setReminderTime(1, 2)         assertEquals(listOf("settings", "datastore_default"), AppBackupAgent().datastoreNames)     }
games/nonogram/src/test/java/com/vayunmathur/games/nonogram/ui/NonogramScreensTest.kt
diff --git a/games/nonogram/src/test/java/com/vayunmathur/games/nonogram/ui/NonogramScreensTest.kt b/games/nonogram/src/test/java/com/vayunmathur/games/nonogram/ui/NonogramScreensTest.ktindex 7c22610fbc..b96419be60 100644--- a/games/nonogram/src/test/java/com/vayunmathur/games/nonogram/ui/NonogramScreensTest.kt+++ b/games/nonogram/src/test/java/com/vayunmathur/games/nonogram/ui/NonogramScreensTest.kt@@ -39,12 +39,13 @@ class NonogramScreensTest {     class Recording : NonogramGameActions {         val taps = mutableListOf<Int>(); val crosses = mutableListOf<Int>()         val modes = mutableListOf<MarkMode>(); val gameModes = mutableListOf<GameMode>()-        var next = 0; var restarts = 0+        var next = 0; var restarts = 0; var retries = 0         override fun tapCell(index: Int) { taps += index }         override fun crossCell(index: Int) { crosses += index }         override fun setMarkMode(mode: MarkMode) { modes += mode }         override fun nextLevel() { next++ }         override fun restartLevel() { restarts++ }+        override fun retryGeneration() { retries++ }         override fun setGameMode(mode: GameMode) { gameModes += mode }     } @@ -67,7 +68,8 @@ class NonogramScreensTest {         rule.setContent { NonogramGameScreen(NonogramUiState(generationFailed = true), a, {}, {}) }         rule.onNodeWithText(str(R.string.generation_failed)).assertIsDisplayed()         rule.onNodeWithText(str(R.string.try_again)).performClick()-        assertEquals(1, a.restarts)+        assertEquals(1, a.retries, "try-again must widen the seed search, not just clear marks")+        assertEquals(0, a.restarts)     }      @Test fun inProgressShowsHintToggleAndRestartWithoutConfirm() {