Modern Apps git

← Modern-Apps.git

minesweeper: cascades now travel through already-open zeroes

Commit
e2d7e75650610ca474c3eabdc01c319627e4f871
Author
Vayun Mathur <[email protected]>
When
2026-12-01 06:11 UTC (5 days ago)
Parents
69825df
The flood-fill BFS used  as its visited set, so
already-revealed neighbours were never queued and the flood
could not spread through a zero an earlier dig had opened.
On a 4x4 field (mine at 15): flag 0 and 2, dig 1 (cascade
opens everything else), unflag both, dig 0 — cell 2 is only
reachable through the open zero at 1, so it stayed covered
and the cleared field never recognised the win. Any cell
flagged during an earlier cascade and later unflagged hit
the same strand.

Expansion now tracks this flood's visited set separately
from revealed: open zeroes carry the flood onward, flagged
cells still neither open nor spread, numbered cells still
stop expansion, and each cell enqueues at most once; chord
inherits the fix via reveal. A brute-force comparison
against the standard zero-component flood over random boards
confirmed the divergence, encoded as a regression test that
failed pre-fix at revealed[2]; a second test guards that
flagged cells still block the flood. Chord, first-click
safety, the counter and the save codec were audited and
cleared. Verified: :games:minesweeper:testDevUnitTest 100
tests pass; :games:minesweeper:lint passes.

Diff

games/minesweeper/src/main/java/com/vayunmathur/games/minesweeper/domain/MinesweeperRules.kt
diff --git a/games/minesweeper/src/main/java/com/vayunmathur/games/minesweeper/domain/MinesweeperRules.kt b/games/minesweeper/src/main/java/com/vayunmathur/games/minesweeper/domain/MinesweeperRules.ktindex 493ba8dcbd..98433dbcca 100644--- a/games/minesweeper/src/main/java/com/vayunmathur/games/minesweeper/domain/MinesweeperRules.kt+++ b/games/minesweeper/src/main/java/com/vayunmathur/games/minesweeper/domain/MinesweeperRules.kt@@ -30,6 +30,12 @@ object MinesweeperRules {          val revealed = state.revealed.toMutableList()         // Iterative flood fill; a recursive one overflows the stack on a large open field.+        // `visited` is tracked apart from `revealed`: the flood must still spread+        // through a zero an earlier dig opened, because a neighbour that was+        // flagged at that time and has since been unflagged is reachable only+        // through it. Treating every revealed neighbour as a dead end stranded+        // such cells — covered, unflagged, and opened by no later cascade.+        val visited = mutableSetOf(index)         val queue = ArrayDeque<Int>()         queue.addLast(index)         revealed[index] = true@@ -37,7 +43,8 @@ object MinesweeperRules {             val current = queue.removeFirst()             if (state.neighbourCounts[current] != 0) continue             for (neighbour in state.neighbours(current)) {-                if (revealed[neighbour] || state.flagged[neighbour]) continue+                if (neighbour in visited || state.flagged[neighbour]) continue+                visited.add(neighbour)                 revealed[neighbour] = true                 if (state.neighbourCounts[neighbour] == 0) queue.addLast(neighbour)             }
games/minesweeper/src/test/java/com/vayunmathur/games/minesweeper/domain/MinesweeperRulesTest.kt
diff --git a/games/minesweeper/src/test/java/com/vayunmathur/games/minesweeper/domain/MinesweeperRulesTest.kt b/games/minesweeper/src/test/java/com/vayunmathur/games/minesweeper/domain/MinesweeperRulesTest.ktindex ff74d93785..eb1d1d5952 100644--- a/games/minesweeper/src/test/java/com/vayunmathur/games/minesweeper/domain/MinesweeperRulesTest.kt+++ b/games/minesweeper/src/test/java/com/vayunmathur/games/minesweeper/domain/MinesweeperRulesTest.kt@@ -75,6 +75,59 @@ class MinesweeperRulesTest {         assertFalse(next.revealed[5], "flagged cell should stay covered")     } +    @Test+    fun cascadeCarriesThroughAnOpenZeroToACellUnflaggedSince() {+        // Mine in the far corner, so every other cell is a zero or borders one.+        val state = field(+            """+            ....+            ....+            ....+            ...*+            """+        )+        // Flag two zeroes, then dig between them: the cascade opens the field+        // around the flags but cannot open the flagged cells themselves.+        val flagged = MinesweeperRules.toggleFlag(MinesweeperRules.toggleFlag(state, 0), 2)+        val opened = MinesweeperRules.reveal(flagged, 1)+        assertFalse(opened.revealed[0])+        assertFalse(opened.revealed[2])+        assertEquals(GameOutcome.PLAYING, opened.outcome)++        // Both flags turn out to be wrong and come off. Digging cell 0 must now+        // carry the flood through the already-open zero at 1 to cell 2: cell 2+        // is reachable from 0 through open zeroes, and opening it clears the+        // field. Before the fix the flood stopped at every already-revealed+        // neighbour, so cell 2 stayed covered and the cleared field never won.+        val unflagged = MinesweeperRules.toggleFlag(MinesweeperRules.toggleFlag(opened, 0), 2)+        val next = MinesweeperRules.reveal(unflagged, 0)+        assertTrue(next.revealed[0])+        assertTrue(next.revealed[2], "the flood must pass through the open zero at 1 to cell 2")+        assertEquals(GameOutcome.WON, next.outcome)+        assertEquals(0, next.minesRemaining)+    }++    @Test+    fun cascadeStillCannotPassAFlaggedCell() {+        val state = field(+            """+            ....+            ....+            ....+            ...*+            """+        )+        // Same shape as above, but cell 2 keeps its flag: the flood through the+        // open zero at 1 may reach it, yet must neither open it nor win.+        val flagged = MinesweeperRules.toggleFlag(MinesweeperRules.toggleFlag(state, 0), 2)+        val opened = MinesweeperRules.reveal(flagged, 1)+        val unflaggedZeroOnly = MinesweeperRules.toggleFlag(opened, 0)+        val next = MinesweeperRules.reveal(unflaggedZeroOnly, 0)+        assertTrue(next.revealed[0])+        assertFalse(next.revealed[2], "a flagged cell stays covered even when the flood reaches it")+        assertEquals(GameOutcome.PLAYING, next.outcome)+    }+     @Test     fun flaggingIsAToggleAndOnlyAppliesToCoveredCells() {         val state = field(