Modern Apps git

← Modern-Apps.git

games/minesweeper: started-but-unrevealed saves no longer deal a rigged first click

Commit
22091f3bb13ff29d6df1b956e1e89786892a89e9
Author
Vayun Mathur <[email protected]>
When
2026-12-01 02:29 UTC (5 days ago)
Parents
aad4905
The progress codec accepted a save with started=true and
zero revealed cells — a state real play can never produce,
since dig lays mines and reveals the tapped cell atomically
and only that result is saved. The restored ViewModel trusts
started, so it never re-lays, never records the game as
played, runs the clock before any reveal, and the first tap
can land on an already-laid mine: an opening-move loss on a
tampered/corrupt save, in a game whose contract is a safe
first tap.

decode now drops started-with-nothing-revealed saves, so
restore falls back to dealing a fresh field whose first tap
is laid safely as usual. Two regression tests failed pre-fix,
one at the codec and one end-to-end (tapping the tampered
mine cell no longer loses). Generation, flood reveal, chord,
win settle, flags and the timer were audited and cleared.
Verified: :games:minesweeper:testDevUnitTest 98 tests and
lint pass.

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 cdd74a197e..bb2eaae32a 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@@ -58,6 +58,11 @@ object MinesweeperProgressCodec {         val started = root.getValue("started").jsonPrimitive.content.toBooleanStrict()         if (started && mines.count { it } != mineCount) return null         if (!started && (mines.any { it } || revealed.any { it } || flagged.any { it })) return null+        // Laying the mines and opening the tapped cell are one step, and only the+        // opened result is ever saved, so a started field always has a cell open.+        // Restoring one with none open would trust started, never re-lay, and let+        // the opening tap land on a laid mine.+        if (started && revealed.none { it }) return null         if (revealed.indices.any { revealed[it] && flagged[it] }) return null         val elapsed = root.getValue("elapsed").jsonPrimitive.int         if (elapsed < 0) return null
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 8461621aaf..d71b1f4fbf 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@@ -78,6 +78,24 @@ class MinesweeperProgressCodecTest {         assertNull(MinesweeperProgressCodec.decode(MinesweeperProgressCodec.encode(tampered), config))     } +    @Test+    fun saveWithStartedFieldButNothingRevealedIsDropped() {+        // Mines are laid and the tapped cell opened in the same step (dig lays,+        // then reveals, and only the revealed result is ever saved), so a started+        // field always has at least the opening cell open. A save claiming+        // started with nothing revealed restores as a field the ViewModel will+        // not re-lay (it trusts started), so the first tap could hit a laid+        // mine and lose on the opening move; it must be dropped, not restored.+        val saved = midGame()+        val tampered = saved.copy(+            game = saved.game.copy(+                revealed = List(saved.game.cellCount) { false },+                flagged = List(saved.game.cellCount) { false },+            ),+        )+        assertNull(MinesweeperProgressCodec.decode(MinesweeperProgressCodec.encode(tampered), config))+    }+     @Test     fun midGameRoundTripsThroughStore() {         val store = InProgressField(app)
games/minesweeper/src/test/java/com/vayunmathur/games/minesweeper/platform/MinesweeperViewModelTest.kt
diff --git a/games/minesweeper/src/test/java/com/vayunmathur/games/minesweeper/platform/MinesweeperViewModelTest.kt b/games/minesweeper/src/test/java/com/vayunmathur/games/minesweeper/platform/MinesweeperViewModelTest.ktindex bc468ce5f1..ddcb0bbb86 100644--- a/games/minesweeper/src/test/java/com/vayunmathur/games/minesweeper/platform/MinesweeperViewModelTest.kt+++ b/games/minesweeper/src/test/java/com/vayunmathur/games/minesweeper/platform/MinesweeperViewModelTest.kt@@ -6,7 +6,9 @@ import com.vayunmathur.games.minesweeper.data.BoardSize import com.vayunmathur.games.minesweeper.data.Difficulty import com.vayunmathur.games.minesweeper.data.GameConfig import com.vayunmathur.games.minesweeper.data.GameOutcome+import com.vayunmathur.games.minesweeper.data.InProgressField import com.vayunmathur.games.minesweeper.data.MinesweeperGameState+import com.vayunmathur.games.minesweeper.data.SavedMinesweeperGame import com.vayunmathur.games.minesweeper.data.TapMode import com.vayunmathur.games.minesweeper.data.neighbourIndices import kotlin.test.Test@@ -185,6 +187,44 @@ class MinesweeperViewModelTest {         assertTrue(vm.uiState.value.game!!.revealed.count { it } > before.revealed.count { it })     } +    @Test+    fun ensureGameDropsAStartedSaveWithNothingRevealedAndDealsFresh() {+        // A started save with no cell open cannot come from play (laying and the+        // first reveal are one step), and restoring it would let the opening tap+        // land on an already-laid mine. ensureGame must fall back to a blank+        // field whose first tap is laid safely like any other.+        val config = GameConfig(BoardSize.SMALL, Difficulty.EASY)+        val blank = MinesweeperGameState.empty(config)+        val cells = blank.cellCount+        val mines = List(cells) { it in setOf(20, 21, 30, 40, 50, 60, 70, 79) }+        val counts = List(cells) { index ->+            if (mines[index]) 0 else neighbourIndices(blank.cols, blank.rows, index).count { mines[it] }+        }+        val tampered = SavedMinesweeperGame(+            game = blank.copy(mines = mines, neighbourCounts = counts, started = true),+            elapsedSeconds = 42,+            tapMode = TapMode.DIG,+            placedAnyFlag = false,+        )+        val store = InProgressField(app)+        store.clear(config.variant)+        store.save(config, tampered)++        val vm = vm()+        vm.ensureGame(config)+        val game = vm.uiState.value.game!!+        assertFalse(game.started, "the tampered save must not restore as a started field")+        assertTrue(game.mines.none { it }, "a fresh blank field is dealt instead")++        // Cell 20 is a mine in the tampered layout; as an opening tap on the+        // fresh field it can never lose.+        vm.tapCell(20)+        val opened = vm.uiState.value.game!!+        assertTrue(opened.revealed[20])+        assertFalse(opened.outcome == GameOutcome.LOST, "the opening tap can never lose")+        store.clear(config.variant)+    }+     @Test     fun giveUpClearsGameTimerStopsWhenOverOrUnstarted() {         val vm = vm()