Modern Apps git

← Modern-Apps.git

minesweeper: stop Flag-mode taps from chording open cells (#ux-audit)

Commit
dfa2c6517501073bf87562ce7da9a0aa5790890b
Author
Vayun Mathur <[email protected]>
When
2026-11-13 01:17 UTC (23 days ago)
Parents
770286f
Symptom: in Flag mode - the mode whose whole point is that a tap can
never uncover anything - tapping an open number whose flag count was
satisfied chorded it, opening its neighbours. With a misplaced flag
that uncovered a mine and ended the game.

Root cause: tapCell() checked "revealed -> chord" before looking at
the tap mode, on the reasoning that a tap on an open number is
unambiguous. But a chord opens cells and trusts the player's flags, so
it is exactly the risky action Flag mode exists to fence off.

Fix: a tap on an open cell chords only in Dig mode; in Flag mode it
does nothing. So that chording is not lost in Flag mode, the long
press - which in Flag mode is the dig gesture - chords when it lands
on an open number (and digs a covered cell as before). Dig mode is
unchanged: tap chords, long press flags.

User-visible effect: a Flag-mode tap can no longer open anything; to
chord in Flag mode, long-press the number.

Verification: new MinesweeperViewModelTest cases build a field with a
satisfied number and a covered safe neighbour, then assert a Flag-mode
tap leaves revealed/flagged unchanged, a Flag-mode long press opens
cells, and a Dig-mode tap still chords.
:games:minesweeper:testDevUnitTest in the toolchain container.

Diff

games/minesweeper/src/main/java/com/vayunmathur/games/minesweeper/platform/MinesweeperUiContract.kt
diff --git a/games/minesweeper/src/main/java/com/vayunmathur/games/minesweeper/platform/MinesweeperUiContract.kt b/games/minesweeper/src/main/java/com/vayunmathur/games/minesweeper/platform/MinesweeperUiContract.ktindex ab076dcca1..496625c830 100644--- a/games/minesweeper/src/main/java/com/vayunmathur/games/minesweeper/platform/MinesweeperUiContract.kt+++ b/games/minesweeper/src/main/java/com/vayunmathur/games/minesweeper/platform/MinesweeperUiContract.kt@@ -15,10 +15,10 @@ import com.vayunmathur.games.minesweeper.data.TapMode  * Every member has a no-op default, so [Noop] is the whole implementation a preview needs.  */ interface MinesweeperActions {-    /** Tap: digs or flags depending on the current [TapMode]; chords an already-open number. */+    /** Tap: digs or flags depending on the current [TapMode]; in Dig mode chords an open number. */     fun tapCell(index: Int) {} -    /** Long press: the other action from whatever the mode is. */+    /** Long press: the other action from whatever the mode is (in Flag mode, digs or chords). */     fun flagCell(index: Int) {}      fun setTapMode(mode: TapMode) {}
games/minesweeper/src/main/java/com/vayunmathur/games/minesweeper/platform/MinesweeperViewModel.kt
diff --git a/games/minesweeper/src/main/java/com/vayunmathur/games/minesweeper/platform/MinesweeperViewModel.kt b/games/minesweeper/src/main/java/com/vayunmathur/games/minesweeper/platform/MinesweeperViewModel.ktindex adde53bcae..84b6341445 100644--- a/games/minesweeper/src/main/java/com/vayunmathur/games/minesweeper/platform/MinesweeperViewModel.kt+++ b/games/minesweeper/src/main/java/com/vayunmathur/games/minesweeper/platform/MinesweeperViewModel.kt@@ -78,8 +78,10 @@ class MinesweeperViewModel(application: Application) :      * Tap: digs, flags, or chords, depending on the mode and what is under the finger.      *      * In flag mode a tap can never uncover anything, which is the whole point — clearing a run of-     * suspected mines should not risk ending the game on a mis-tap. Chording still works in flag mode,-     * because tapping an already-open number is unambiguous whichever mode is active.+     * suspected mines should not risk ending the game on a mis-tap. That includes chording: a chord+     * opens cells (and trusts flags that may be wrong), so a flag-mode tap on an open number used to+     * uncover a mine and end the game in the one mode promising it cannot. In flag mode the chord+     * moves to the long press, the mode's dig gesture.      *      * The opening tap always digs, whatever the mode: before it no mines exist, so a flag-mode tap      * could only be a silent no-op, and the opening is guaranteed safe anyway.@@ -93,8 +95,8 @@ class MinesweeperViewModel(application: Application) :             return         } -        if (game.started && game.revealed[index]) {-            chord(game, index)+        if (game.revealed[index]) {+            if (state.tapMode == TapMode.DIG) chord(game, index)             return         }         if (state.tapMode == TapMode.FLAG) {@@ -104,12 +106,21 @@ class MinesweeperViewModel(application: Application) :         dig(state, game, index)     } -    /** Long press: the other action from whatever the mode is. */+    /**+     * Long press: the other action from whatever the mode is.+     *+     * In flag mode that is digging, so a long press on an open number chords — the opening gesture+     * stays available in flag mode, just never on the plain tap.+     */     override fun flagCell(index: Int) {         val state = _uiState.value         val game = state.game ?: return         if (game.isOver) return-        if (state.tapMode == TapMode.FLAG) dig(state, game, index) else toggleFlag(game, index)+        when {+            state.tapMode == TapMode.DIG -> toggleFlag(game, index)+            game.revealed[index] -> chord(game, index)+            else -> dig(state, game, index)+        }     }      override fun setTapMode(mode: TapMode) {
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 3230341700..46420b52ec 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@@ -135,6 +135,50 @@ class MinesweeperViewModelTest {         assertEquals(1, vm.uiState.value.game!!.flagsPlaced)     } +    /**+     * A started field with an open number whose mines are all flagged and which still has a covered+     * safe neighbour, i.e. a legal chord that would open something.+     */+    private fun chordReady(vm: MinesweeperViewModel): Int {+        repeat(200) {+            vm.newGame(GameConfig(BoardSize.MEDIUM, Difficulty.MEDIUM))+            vm.tapCell(70)+            val game = vm.uiState.value.game!!+            if (game.isOver) return@repeat+            val number = game.revealed.indices.firstOrNull { i ->+                game.revealed[i] && game.neighbourCounts[i] > 0 &&+                    game.neighbours(i).any { !game.revealed[it] && !game.mines[it] }+            } ?: return@repeat+            game.neighbours(number).filter { game.mines[it] }.forEach { vm.flagCell(it) }+            return number+        }+        error("no chordable field in 200 deals")+    }++    @Test+    fun flagModeTapNeverChordsButLongPressDoes() {+        val vm = vm()+        val number = chordReady(vm)+        vm.setTapMode(TapMode.FLAG)+        val before = vm.uiState.value.game!!+        vm.tapCell(number)+        assertEquals(before.revealed, vm.uiState.value.game!!.revealed, "a flag-mode tap opens nothing")+        assertEquals(before.flagged, vm.uiState.value.game!!.flagged, "and flags nothing on an open cell")++        vm.flagCell(number)+        val after = vm.uiState.value.game!!+        assertTrue(after.revealed.count { it } > before.revealed.count { it }, "long press chords")+    }++    @Test+    fun digModeTapStillChords() {+        val vm = vm()+        val number = chordReady(vm)+        val before = vm.uiState.value.game!!+        vm.tapCell(number)+        assertTrue(vm.uiState.value.game!!.revealed.count { it } > before.revealed.count { it })+    }+     @Test     fun giveUpClearsGameTimerStopsWhenOverOrUnstarted() {         val vm = vm()