Modern Apps git

← Modern-Apps.git

games/minesweeper: restore drops finished boards instead of reviving them

Commit
6e471f3bc3316c920a419517f8dfc5ba8a99ecf0
Author
Vayun Mathur <[email protected]>
When
2026-11-30 20:21 UTC (5 days ago)
Parents
b0bfb62
The progress codec hard-codes outcome = PLAYING at decode, which
made its finished-game guard dead code: a save with a revealed mine
decoded to a playable lost field (winnable by clearing the rest),
and a fully-cleared save decoded to PLAYING so the next dig could
turn a recorded win into a loss. Finished fields are never
legitimately written (writeInProgress clears the slot for isOver
games), so such saves are always inconsistent.

Decode now additionally drops a save when any revealed cell is a
mine or the board isCleared. Two regression tests build exactly
those states through the existing fixture. First-click safety,
flood-fill/chording, flags, win/lose and timers were audited and
cleared. Verified: :games:minesweeper:testDevUnitTest 96 tests
pass, lint green.

Diff

games/minesweeper/src/main/java/com/vayunmathur/games/minesweeper/data/InProgressField.kt
diff --git a/games/minesweeper/src/main/java/com/vayunmathur/games/minesweeper/data/InProgressField.kt b/games/minesweeper/src/main/java/com/vayunmathur/games/minesweeper/data/InProgressField.ktindex 8e9e14f6d6..cdd74a197e 100644--- a/games/minesweeper/src/main/java/com/vayunmathur/games/minesweeper/data/InProgressField.kt+++ b/games/minesweeper/src/main/java/com/vayunmathur/games/minesweeper/data/InProgressField.kt@@ -74,6 +74,12 @@ object MinesweeperProgressCodec {             started = started,         )         if (game.explodedAt != -1 || game.isOver) return null+        // A revealed mine is a lost field and a fully revealed safe field is a won+        // one; neither is ever written (finished fields clear their slot), so a+        // save claiming either while PLAYING is inconsistent and must be dropped+        // rather than restored as a game that can still be won or lost.+        if (revealed.indices.any { revealed[it] && mines[it] }) return null+        if (game.isCleared) return null         SavedMinesweeperGame(             game = game,             elapsedSeconds = elapsed,
games/minesweeper/src/test/java/com/vayunmathur/games/minesweeper/data/MinesweeperProgressCodecTest.kt
diff --git a/games/minesweeper/src/test/java/com/vayunmathur/games/minesweeper/data/MinesweeperProgressCodecTest.kt b/games/minesweeper/src/test/java/com/vayunmathur/games/minesweeper/data/MinesweeperProgressCodecTest.ktindex 18ca261ae7..8461621aaf 100644--- a/games/minesweeper/src/test/java/com/vayunmathur/games/minesweeper/data/MinesweeperProgressCodecTest.kt+++ b/games/minesweeper/src/test/java/com/vayunmathur/games/minesweeper/data/MinesweeperProgressCodecTest.kt@@ -50,6 +50,34 @@ class MinesweeperProgressCodecTest {         assertNull(MinesweeperProgressCodec.decode("""{"v":2}""", config))     } +    @Test+    fun saveWithARevealedMineIsDropped() {+        // Index 20 is a mine in the fixture. A revealed mine means the field was+        // lost, so it cannot restore as a PLAYING game with that mine open.+        val saved = midGame()+        val tampered = saved.copy(+            game = saved.game.copy(+                revealed = saved.game.revealed.mapIndexed { i, v -> v || i == 20 },+                flagged = saved.game.flagged.mapIndexed { i, v -> if (i == 20) false else v },+            ),+        )+        assertNull(MinesweeperProgressCodec.decode(MinesweeperProgressCodec.encode(tampered), config))+    }++    @Test+    fun saveWithEverySafeCellRevealedIsDropped() {+        // Every non-mine cell open is a won field; restoring it as PLAYING would+        // let the next dig on a mine turn a finished win into a loss.+        val saved = midGame()+        val tampered = saved.copy(+            game = saved.game.copy(+                revealed = saved.game.mines.map { !it },+                flagged = List(saved.game.cellCount) { false },+            ),+        )+        assertNull(MinesweeperProgressCodec.decode(MinesweeperProgressCodec.encode(tampered), config))+    }+     @Test     fun midGameRoundTripsThroughStore() {         val store = InProgressField(app)