Modern Apps git

← Modern-Apps.git

sudoku: deal a new puzzle from the win overlay's Play Again (#ux-audit)

Commit
8c7501e0951a80581038920699f953686899aa6a
Author
Vayun Mathur <[email protected]>
When
2026-11-13 01:10 UTC (23 days ago)
Parents
10e3b7b
Symptom: after solving a puzzle, "Play Again" restarted the very same
grid. Because a puzzle's win is recorded only once (winRecorded, set in
checkWin), solving the replay showed the win overlay again but never
counted it: no win, no streak, no progress achievements. A player
chaining games from the overlay silently stopped earning anything.

Root cause: GameBoardScreen wired the win overlay's onPlayAgain to
actions.restart(), which keeps the givens and solution and only clears
the player's work. That is the right behaviour for the out-of-lives
"Try again", but not for a win.

Fix: new SudokuActions.playAgain() (no-op default, so previews and
test fakes are unaffected). SudokuViewModel implements it by calling
newGame() with a GameConfig built from the finished game's size,
difficulty and its own answer-mode snapshot, so the next puzzle follows
the same rules as the one just won even if the home-screen default was
changed meanwhile. The win overlay calls playAgain(); the out-of-lives
overlay keeps restart(). winRecorded still guards the restart path.

User-visible effect: Play Again after a win deals a fresh puzzle of
the same size/difficulty/mode (with the usual generating spinner), and
winning it is recorded like any other game. Try Again after running
out of lives still retries the same puzzle.

Verification: new SudokuViewModelTest
playAgainDealsANewPuzzleWithTheSameSettingsAndItsWinCounts (same
settings, blank entries, gamesPlayed and gamesWon/streak reach 2);
SudokuScreensTest asserts Play Again calls playAgain() and not
restart(), and Try Again still calls restart();
:games:sudoku:testDevUnitTest in the toolchain container.

Diff

games/sudoku/src/main/java/com/vayunmathur/games/sudoku/platform/SudokuUiContract.kt
diff --git a/games/sudoku/src/main/java/com/vayunmathur/games/sudoku/platform/SudokuUiContract.kt b/games/sudoku/src/main/java/com/vayunmathur/games/sudoku/platform/SudokuUiContract.ktindex a92c3b2977..535aae8402 100644--- a/games/sudoku/src/main/java/com/vayunmathur/games/sudoku/platform/SudokuUiContract.kt+++ b/games/sudoku/src/main/java/com/vayunmathur/games/sudoku/platform/SudokuUiContract.kt@@ -19,7 +19,11 @@ interface SudokuActions {     fun toggleNotesMode() {}     fun hint() {}     fun undo() {}+    /** Same puzzle again, work cleared: the out-of-lives "Try again". */     fun restart() {}++    /** A fresh puzzle with the finished game's size, difficulty and answer mode. */+    fun playAgain() {}     fun giveUp() {}      companion object { val Noop: SudokuActions = object : SudokuActions {} }
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 2bc847404b..4732dd3e9c 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@@ -78,10 +78,11 @@ class SudokuViewModel(application: Application) : AndroidViewModel(application),     /**      * Set once the dealt puzzle's win has been written to stats and achievements.      *-     * Restarting a solved puzzle (the win overlay's play-again) keeps the same givens-     * and solution, so without this the player could re-enter a solution they already-     * know and farm wins, streaks and progress achievements off one puzzle. Cleared-     * only by [newGame]: a fresh deal is a fresh record, a restart is not.+     * [restart] keeps the same givens and solution, so without this a player could+     * re-enter a solution they already know and farm wins, streaks and progress+     * achievements off one puzzle. The win overlay deals a fresh puzzle through+     * [playAgain] instead, so this only guards restart paths. Cleared only by+     * [newGame]: a fresh deal is a fresh record, a restart is not.      */     private var winRecorded = false @@ -322,6 +323,20 @@ class SudokuViewModel(application: Application) : AndroidViewModel(application),         )     } +    /**+     * Deals a new puzzle with the settings the current one was dealt with.+     *+     * This is the win overlay's "Play again". It used to call [restart], which re-dealt the same+     * grid; because a puzzle's win is recorded once (see [winRecorded]) the replay could never+     * count, so a player chaining games after a win silently stopped earning wins and streaks.+     * The game's own answer-mode snapshot is reused rather than the home-screen default, so the+     * next puzzle follows the same rules as the one just finished.+     */+    override fun playAgain() {+        val game = _uiState.value.game ?: return+        newGame(GameConfig(game.size, game.difficulty, game.answerMode))+    }+     /** Abandons an unfinished puzzle, which breaks the win streak for that variant. */     override fun giveUp() {         val game = _uiState.value.game ?: return
games/sudoku/src/main/java/com/vayunmathur/games/sudoku/ui/GameBoardScreen.kt
diff --git a/games/sudoku/src/main/java/com/vayunmathur/games/sudoku/ui/GameBoardScreen.kt b/games/sudoku/src/main/java/com/vayunmathur/games/sudoku/ui/GameBoardScreen.ktindex 44a26c160f..0b4eb609da 100644--- a/games/sudoku/src/main/java/com/vayunmathur/games/sudoku/ui/GameBoardScreen.kt+++ b/games/sudoku/src/main/java/com/vayunmathur/games/sudoku/ui/GameBoardScreen.kt@@ -178,7 +178,8 @@ fun GameBoardScreen(                 GameResultOverlay(                     title = stringResource(R.string.congratulations),                     won = true,-                    onPlayAgain = { actions.restart() },+                    // A fresh puzzle: replaying the solved grid could never count as a win.+                    onPlayAgain = { actions.playAgain() },                     onBack = onExit,                 ) {                     Text("${stringResource(UiR.string.game_time)}: ${formatDuration(game.elapsedSeconds)}")
games/sudoku/src/test/java/com/vayunmathur/games/sudoku/platform/SudokuAchievementsAndMiscTest.kt
diff --git a/games/sudoku/src/test/java/com/vayunmathur/games/sudoku/platform/SudokuAchievementsAndMiscTest.kt b/games/sudoku/src/test/java/com/vayunmathur/games/sudoku/platform/SudokuAchievementsAndMiscTest.ktindex f817caed07..42fbd62833 100644--- a/games/sudoku/src/test/java/com/vayunmathur/games/sudoku/platform/SudokuAchievementsAndMiscTest.kt+++ b/games/sudoku/src/test/java/com/vayunmathur/games/sudoku/platform/SudokuAchievementsAndMiscTest.kt@@ -85,7 +85,7 @@ class SudokuAchievementsAndMiscTest {     fun noopActionsDoNothing() {         val actions = SudokuActions.Noop         actions.selectCell(1); actions.enterDigit(1); actions.clearCell()-        actions.toggleNotesMode(); actions.hint(); actions.undo(); actions.restart(); actions.giveUp()+        actions.toggleNotesMode(); actions.hint(); actions.undo(); actions.restart(); actions.playAgain(); actions.giveUp()         // Reaching here without effect or crash is the contract; state is the caller's.         assertTrue(actions === SudokuActions.Noop)     }
games/sudoku/src/test/java/com/vayunmathur/games/sudoku/platform/SudokuViewModelTest.kt
diff --git a/games/sudoku/src/test/java/com/vayunmathur/games/sudoku/platform/SudokuViewModelTest.kt b/games/sudoku/src/test/java/com/vayunmathur/games/sudoku/platform/SudokuViewModelTest.ktindex d96efc6959..0920175231 100644--- a/games/sudoku/src/test/java/com/vayunmathur/games/sudoku/platform/SudokuViewModelTest.kt+++ b/games/sudoku/src/test/java/com/vayunmathur/games/sudoku/platform/SudokuViewModelTest.kt@@ -77,7 +77,7 @@ class SudokuViewModelTest {         assertNull(vm.uiState.value.game)         assertFalse(vm.hasActiveGame())         vm.selectCell(0); vm.enterDigit(1); vm.clearCell(); vm.toggleNotesMode()-        vm.hint(); vm.undo(); vm.restart(); vm.giveUp(); vm.incrementTimer()+        vm.hint(); vm.undo(); vm.restart(); vm.playAgain(); vm.giveUp(); vm.incrementTimer()         vm.dismissAchievementNotification()         assertNull(vm.uiState.value.game)         assertFalse(vm.uiState.value.canUndo)@@ -393,8 +393,8 @@ class SudokuViewModelTest {         solve()         assertEquals(1, stats.getStats(BoardSize.SIX, Difficulty.EASY).gamesWon) -        // Play-again on the win overlay restarts the same puzzle; solving it again-        // shows the win but must not count it twice.+        // A restart keeps the same puzzle; solving it again shows the win but must+        // not count it twice.         vm.restart()         solve()         val statsAfter = stats.getStats(BoardSize.SIX, Difficulty.EASY)@@ -402,6 +402,38 @@ class SudokuViewModelTest {         assertEquals(1, statsAfter.currentWinStreak)     } +    @Test+    fun playAgainDealsANewPuzzleWithTheSameSettingsAndItsWinCounts() {+        val vm = startedVm(GameConfig(BoardSize.SIX, Difficulty.EASY, answerMode = true))+        fun solve() {+            var guard = 0+            while (guard++ < 100) {+                val game = vm.uiState.value.game!!+                val next = game.blankIndices().firstOrNull() ?: break+                vm.selectCell(next)+                vm.enterDigit(game.solution[next])+            }+            assertTrue(vm.uiState.value.game!!.isWon)+        }+        solve()+        val first = vm.uiState.value.game!!++        vm.playAgain()+        await("next puzzle dealt") { vm.uiState.value.game != null }+        val next = vm.uiState.value.game!!+        assertEquals(first.size, next.size)+        assertEquals(first.difficulty, next.difficulty)+        assertTrue(next.answerMode, "keeps the finished game's answer mode")+        assertFalse(next.isWon)+        assertEquals(List(next.size.cellCount) { 0 }, next.entries)+        assertEquals(2, stats.getStats(BoardSize.SIX, Difficulty.EASY).gamesPlayed)++        solve()+        val statsAfter = stats.getStats(BoardSize.SIX, Difficulty.EASY)+        assertEquals(2, statsAfter.gamesWon, "the replayed game's win is recorded")+        assertEquals(2, statsAfter.currentWinStreak)+    }+     @Test     fun answerModeRejectsWrongDigitsCostsLivesAndEndsGame() {         stats.recordGameWon(BoardSize.SIX, Difficulty.EASY, 60)
games/sudoku/src/test/java/com/vayunmathur/games/sudoku/ui/SudokuScreensTest.kt
diff --git a/games/sudoku/src/test/java/com/vayunmathur/games/sudoku/ui/SudokuScreensTest.kt b/games/sudoku/src/test/java/com/vayunmathur/games/sudoku/ui/SudokuScreensTest.ktindex 22e27becb5..9b6df5b275 100644--- a/games/sudoku/src/test/java/com/vayunmathur/games/sudoku/ui/SudokuScreensTest.kt+++ b/games/sudoku/src/test/java/com/vayunmathur/games/sudoku/ui/SudokuScreensTest.kt@@ -45,6 +45,7 @@ class SudokuScreensTest {         var hints = 0         var undos = 0         var restarts = 0+        var playAgains = 0         var giveUps = 0         override fun selectCell(index: Int) { cells += index }         override fun enterDigit(digit: Int) { digits += digit }@@ -53,6 +54,7 @@ class SudokuScreensTest {         override fun hint() { hints++ }         override fun undo() { undos++ }         override fun restart() { restarts++ }+        override fun playAgain() { playAgains++ }         override fun giveUp() { giveUps++ }     } @@ -140,7 +142,7 @@ class SudokuScreensTest {     }      @Test-    fun wonBoardShowsOverlayStatsAndPlayAgainRestarts() {+    fun wonBoardShowsOverlayStatsAndPlayAgainDealsNewPuzzle() {         val actions = RecordingActions()         val base = freshGame()         val entries = List(base.size.cellCount) { if (base.isGiven(it)) 0 else base.solution[it] }@@ -151,7 +153,8 @@ class SudokuScreensTest {         rule.onNodeWithText(str(R.string.congratulations)).assertExists()         rule.onAllNodesWithText("${str(R.string.hints_used)}: 2").assertCountEquals(2)         rule.onNodeWithText("Play Again").performClick()-        assertEquals(1, actions.restarts)+        assertEquals(1, actions.playAgains)+        assertEquals(0, actions.restarts, "the win overlay must not replay the same grid")     }      @Test@@ -187,6 +190,7 @@ class SudokuScreensTest {         rule.onNodeWithText(str(R.string.out_of_lives_message)).assertExists()         rule.onNodeWithText(str(R.string.try_again)).performClick()         assertEquals(1, actions.restarts)+        assertEquals(0, actions.playAgains, "out of lives retries the same puzzle")     }      @Test