Modern Apps git

← Modern-Apps.git

games-arrows: fix dead retry on generation failure

Commit
04e30d573630f1486eb693b60384fe378a99b8d2
Author
Vayun Mathur <[email protected]>
When
2026-11-07 20:06 UTC (29 days ago)
Parents
563ec6b
The generation-failed screen wired its Try again button to
restartLevel(), which only resets saved progress. Loads run solely
from the session flow (mode, level, show-routes, streak) and a
failed load returns before any progress collector starts, so
nothing ever re-ran generation and the player was stuck on the
error until changing mode or restarting the app.

Add a retryGeneration() action backed by a retry counter folded
into the session flow, so collectLatest reruns load with exactly
one load in flight. Because generation is deterministic per seed,
retrying with the same seed would fail identically, so the retry
count also salts the seed; retry zero keeps the canonical board
every player sees, and only boards nobody ever saw get rebuilt
from a different seed.

Diff

games/arrows/src/main/java/com/vayunmathur/games/arrows/platform/ArrowsUiContract.kt
diff --git a/games/arrows/src/main/java/com/vayunmathur/games/arrows/platform/ArrowsUiContract.kt b/games/arrows/src/main/java/com/vayunmathur/games/arrows/platform/ArrowsUiContract.ktindex d723dc9685..33b7ed9b90 100644--- a/games/arrows/src/main/java/com/vayunmathur/games/arrows/platform/ArrowsUiContract.kt+++ b/games/arrows/src/main/java/com/vayunmathur/games/arrows/platform/ArrowsUiContract.kt@@ -22,6 +22,8 @@ interface ArrowsGameActions {      fun nextLevel() {}     fun restartLevel() {}+    /** Rebuilds the board after generation failed. Restarting cannot do this: it only resets progress. */+    fun retryGeneration() {}     fun setGameMode(mode: GameMode) {}      companion object { val Noop: ArrowsGameActions = object : ArrowsGameActions {} }
games/arrows/src/main/java/com/vayunmathur/games/arrows/platform/ArrowsViewModel.kt
diff --git a/games/arrows/src/main/java/com/vayunmathur/games/arrows/platform/ArrowsViewModel.kt b/games/arrows/src/main/java/com/vayunmathur/games/arrows/platform/ArrowsViewModel.ktindex b27680ca2f..00cfed9bb6 100644--- a/games/arrows/src/main/java/com/vayunmathur/games/arrows/platform/ArrowsViewModel.kt+++ b/games/arrows/src/main/java/com/vayunmathur/games/arrows/platform/ArrowsViewModel.kt@@ -72,6 +72,17 @@ class ArrowsViewModel(application: Application) : AndroidViewModel(application),         ArrowsAchievementsManager(application, json, dataStore, dailyStore)     } +    /**+     * Bumped to make [load] run again after a failed generation.+     *+     * Nothing else re-triggers it: a failed build leaves no progress collector running, so the+     * error screen's retry has to ask for a fresh pass explicitly. Feeding the count through the+     * same session flow keeps exactly one load in flight, since `collectLatest` cancels the+     * previous one. The count also salts the generation seed (see [buildBoard]): generation is+     * deterministic per seed, so retrying with the same seed would fail identically forever.+     */+    private val retryCount = MutableStateFlow(0)+     init {         achievementsManager.checkExistingAchievements() @@ -81,7 +92,10 @@ class ArrowsViewModel(application: Application) : AndroidViewModel(application),                 dataStore.currentLevel,                 dataStore.showRoutes,                 dailyStore.currentStreak,-            ) { mode, level, showRoutes, streak -> Session(mode, level, showRoutes, streak) }+                retryCount,+            ) { mode, level, showRoutes, streak, retries ->+                Session(mode, level, showRoutes, streak, retries)+            }                 .collectLatest { session -> load(session) }         }     }@@ -92,6 +106,7 @@ class ArrowsViewModel(application: Application) : AndroidViewModel(application),         val level: Int,         val showRoutes: Boolean,         val streak: Long,+        val retries: Int = 0,     )      private suspend fun load(session: Session) {@@ -111,7 +126,7 @@ class ArrowsViewModel(application: Application) : AndroidViewModel(application),         val puzzle = withContext(Dispatchers.Default) {             val cached = if (daily) dailyBoard?.takeIf { it.first == day }?.second             else casualBoard?.takeIf { it.first == session.level }?.second-            cached ?: buildBoard(daily, session.level, day)+            cached ?: buildBoard(daily, session.level, day, session.retries)         }          if (puzzle == null) {@@ -123,7 +138,12 @@ class ArrowsViewModel(application: Application) : AndroidViewModel(application),         observeProgress(session, puzzle, day)     } -    private fun buildBoard(daily: Boolean, level: Int, day: Long): ArrowsPuzzle? =+    /**+     * [retries] salts the seed: the first attempt for a level or day always uses retry zero, so+     * every player sees the same board, and only a board that failed to generate at all — which no+     * player ever saw — is rebuilt from a different seed on retry.+     */+    private fun buildBoard(daily: Boolean, level: Int, day: Long, retries: Int): ArrowsPuzzle? =         if (daily) {             val (cols, rows) = DAILY_BOARD             ArrowsGenerator.generateSeeded(@@ -131,7 +151,7 @@ class ArrowsViewModel(application: Application) : AndroidViewModel(application),                 rows = rows,                 targetPieces = DAILY_ARROWS,                 mirrorCount = DAILY_MIRRORS,-                seed = DAILY_SEED_OFFSET + day,+                seed = DAILY_SEED_OFFSET + day + retries * RETRY_SEED_STRIDE,             )         } else {             val (cols, rows) = boardSizeForLevel(level)@@ -140,7 +160,7 @@ class ArrowsViewModel(application: Application) : AndroidViewModel(application),                 rows = rows,                 targetPieces = arrowCountForLevel(level),                 mirrorCount = mirrorCountForLevel(level),-                seed = level.toLong(),+                seed = level.toLong() + retries * RETRY_SEED_STRIDE,             )         } @@ -251,6 +271,17 @@ class ArrowsViewModel(application: Application) : AndroidViewModel(application),         }     } +    /**+     * Asks for a fresh generation pass after [load] found no board.+     *+     * Goes through [retryCount] rather than calling [load] directly, so the session collector+     * stays the only place loads run and can cancel this one like any other if the session+     * changes underneath it.+     */+    override fun retryGeneration() {+        retryCount.value += 1+    }+     override fun nextLevel() {         val state = _uiState.value         if (state.mode == GameMode.DAILY) return@@ -351,5 +382,11 @@ class ArrowsViewModel(application: Application) : AndroidViewModel(application),          * 1.1B nonogram).          */         private const val DAILY_SEED_OFFSET = 1_300_000_000L++        /**+         * Keeps retry seeds far from the seeds any first attempt uses, mirroring the generator's+         * own per-round stride so a retried board is genuinely a different search.+         */+        private const val RETRY_SEED_STRIDE = 1_000_003L     } }
games/arrows/src/main/java/com/vayunmathur/games/arrows/ui/ArrowsGameScreen.kt
diff --git a/games/arrows/src/main/java/com/vayunmathur/games/arrows/ui/ArrowsGameScreen.kt b/games/arrows/src/main/java/com/vayunmathur/games/arrows/ui/ArrowsGameScreen.ktindex b5d9710867..e6a7212940 100644--- a/games/arrows/src/main/java/com/vayunmathur/games/arrows/ui/ArrowsGameScreen.kt+++ b/games/arrows/src/main/java/com/vayunmathur/games/arrows/ui/ArrowsGameScreen.kt@@ -114,7 +114,7 @@ fun ArrowsGameScreen(                         color = MaterialTheme.colorScheme.error,                         textAlign = TextAlign.Center,                     )-                    Button(onClick = { actions.restartLevel() }) {+                    Button(onClick = { actions.retryGeneration() }) {                         Text(stringResource(R.string.try_again))                     }                 }