Modern Apps git

← Modern-Apps.git

games: a flagged save claiming no flag was ever placed is rejected

Commit
f8b4d64b780aadb3ee276d096185c3b2650137d6
Author
Vayun Mathur <[email protected]>
When
2026-12-01 12:59 UTC (5 days ago)
Parents
8f8486a
The progress codec validated many cross-field invariants
but never related placedFlag to the flagged bits. A flag
can only appear via toggleFlag (which sets placedAnyFlag
first) or win auto-flagging (which produces finished boards
whose slots are cleared, not saved), so
flagged.any() && !placedAnyFlag is unreachable through
play — the same class of corrupt/restored save the codec
already drops. decode now rejects it; ensureGame already
falls back to a fresh board.

Two regression tests (codec drop, ViewModel fresh deal)
failed pre-fix. First-click safety, flood/chord (a 900k-step
reference comparison) and timer/records were audited and
cleared. Verified: :games:minesweeper:testDevUnitTest 102
tests and lint BUILD SUCCESSFUL.

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 bb2eaae32a..daede02f3f 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@@ -64,6 +64,12 @@ object MinesweeperProgressCodec {         // 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 placedAnyFlag = root.getValue("placedFlag").jsonPrimitive.content.toBooleanStrict()+        // A flag can only appear through the ViewModel, which records the+        // placement before the field can be saved. A board carrying a flag+        // while claiming none was placed is inconsistent: restoring it would+        // let the flagged field be cleared as though no flag had been used.+        if (!placedAnyFlag && flagged.any { it }) return null         val elapsed = root.getValue("elapsed").jsonPrimitive.int         if (elapsed < 0) return null         val game = MinesweeperGameState(@@ -89,7 +95,7 @@ object MinesweeperProgressCodec {             game = game,             elapsedSeconds = elapsed,             tapMode = TapMode.valueOf(root.getValue("tapMode").jsonPrimitive.content),-            placedAnyFlag = root.getValue("placedFlag").jsonPrimitive.content.toBooleanStrict(),+            placedAnyFlag = placedAnyFlag,         )     }.getOrNull() 
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 d71b1f4fbf..341e7c5495 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@@ -9,6 +9,7 @@ import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertNotNull import kotlin.test.assertNull+import kotlin.test.assertTrue  @RunWith(RobolectricTestRunner::class) @Config(sdk = [34])@@ -96,6 +97,21 @@ class MinesweeperProgressCodecTest {         assertNull(MinesweeperProgressCodec.decode(MinesweeperProgressCodec.encode(tampered), config))     } +    @Test+    fun saveWithAFlagButNoPlacedFlagIsDropped() {+        // The fixture has a flag on the mine at index 20. The only way a flag+        // appears during play is ViewModel.toggleFlag, which records that a+        // flag was placed before the field is ever saved, so a save carrying a+        // flag while claiming none was placed cannot come from play. Restoring+        // it would clear the field with that flag on the board yet still earn+        // "clear a field without placing a single flag".+        val saved = midGame()+        assertTrue(saved.game.flagged[20])+        assertTrue(saved.placedAnyFlag)+        val tampered = saved.copy(placedAnyFlag = 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 ddcb0bbb86..943a61c405 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@@ -225,6 +225,46 @@ class MinesweeperViewModelTest {         store.clear(config.variant)     } +    @Test+    fun ensureGameDropsAFlaggedSaveThatClaimsNoFlagWasPlacedAndDealsFresh() {+        // Same mine layout as the codec fixture, with a flag on mine 20, but+        // placedFlag=false. A flag can only be placed through the ViewModel,+        // which records the placement before any save, so this cannot be a+        // played field; restoring it would let the flagged board be cleared+        // while the restored flag history still claims none was ever placed.+        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,+                revealed = List(cells) { it == 0 || it == 1 },+                flagged = List(cells) { it == 20 },+                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 inconsistent flag save must not be restored")+        assertTrue(game.mines.none { it }, "a fresh blank field is dealt instead")+        assertEquals(0, game.flagsPlaced)+        assertEquals(0, vm.elapsedSeconds.value)+        store.clear(config.variant)+    }+     @Test     fun giveUpClearsGameTimerStopsWhenOverOrUnstarted() {         val vm = vm()