games-sudoku: record each puzzle's win only once
- Commit
cdfa86d61b846be7e4400a5f90d8ddd07ab8806d- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-07 20:00 UTC (28 days ago)
- Parents
8d0506e
checkWin recorded stats and achievement progress every time the grid completed, and restart() deals the same givens and solution back with isWon cleared. Winning, tapping Play Again and re-entering the known solution therefore counted a second win, extended the win streak and advanced the wins_10/wins_50 progress -- repeatably, off one puzzle. A winRecorded flag now gates the recording (stats, streak, best time, achievements); it is set on the first completion of a dealt puzzle and cleared only by newGame. The restarted board still shows its win overlay; it just does not count again. Out-of-lives retries are unaffected: no win was recorded for that puzzle yet. Covered by SudokuViewModelTest.replayingASolvedPuzzleDoesNotRecordASecondWin.
Diff
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 11cbf10d73..6aa4a9bb02 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@@ -62,6 +62,16 @@ class SudokuViewModel(application: Application) : AndroidViewModel(application), /** Set when the player ever tries a digit that disagrees with the solution. */ private var madeMistake = false + /**+ * 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.+ */+ private var winRecorded = false+ /** Sequence numbers for answer-mode rejections, so repeats of one digit still animate. */ private var rejectionAttempt = 0L @@ -89,6 +99,7 @@ class SudokuViewModel(application: Application) : AndroidViewModel(application), fun newGame(config: GameConfig) { history.clear() madeMistake = false+ winRecorded = false rejectionAttempt = 0 _uiState.value = SudokuUiState(generating = true) viewModelScope.launch {@@ -304,6 +315,9 @@ class SudokuViewModel(application: Application) : AndroidViewModel(application), if (game.isWon || !game.isComplete) return mutate { it.copy(isWon = true, selected = -1) } + // The board still shows its win on a restart replay; only the recording is once-only.+ if (winRecorded) return+ winRecorded = true statsRepository.recordGameWon(game.size, game.difficulty, game.elapsedSeconds) val manager = achievementsManager manager.onAchievementUnlocked("first_win")
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 ac4cdf394d..606c964c05 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@@ -325,6 +325,31 @@ class SudokuViewModelTest { assertEquals(entries, vm.uiState.value.game!!.entries) } + @Test+ fun replayingASolvedPuzzleDoesNotRecordASecondWin() {+ val vm = startedVm()+ 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()+ 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.+ vm.restart()+ solve()+ val statsAfter = stats.getStats(BoardSize.SIX, Difficulty.EASY)+ assertEquals(1, statsAfter.gamesWon, "the same puzzle records one win")+ assertEquals(1, statsAfter.currentWinStreak)+ }+ @Test fun answerModeRejectsWrongDigitsCostsLivesAndEndsGame() { stats.recordGameWon(BoardSize.SIX, Difficulty.EASY, 60)