Modern Apps git

← Modern-Apps.git

minesweeper: first tap always digs; new fields start in Dig mode (#ux-audit)

Commit
770286f4b81c7ae746029d70dbe88a7bbb5482bb
Author
Vayun Mathur <[email protected]>
When
2026-11-13 01:16 UTC (23 days ago)
Parents
d2c0c13
Symptom: if the player had switched to Flag mode on a previous field,
the first tap on a fresh field did nothing at all - no reveal, no
flag, no feedback - while the hint under the field said "Tap to open,
long press to flag". Players thought the game had frozen.

Root cause: newGame() deliberately carried tapMode over from the
previous field, and tapCell() routed a Flag-mode tap to toggleFlag(),
which is a no-op before the first dig because no mines are laid yet.
The hint text was fixed and ignored the mode.

Fix:
- tapCell() digs whenever the field has not started, whatever the
  mode. The opening is generated safe, so this can never cost a game.
- newGame() (and therefore restart/New Field/Play Again) always deals
  the field in Dig mode instead of inheriting the previous mode. This
  is the audit's requested behaviour; the player can still switch to
  Flag mode for the rest of the field and it stays for that field.
- The pre-start hint follows the toggle: Dig shows the existing "Tap
  to open, long press to flag"; Flag shows the new "First tap opens
  the field, then tap to flag, long press to open".

User-visible effect: the opening tap on a new field always opens it,
new fields always show Dig selected, and the hint never contradicts
the selected mode.

Verification: MinesweeperViewModelTest - newGameDealsBlankFieldInDigMode
(Flag before newGame -> Dig after) and the new
firstTapDigsEvenInFlagModeAndRestartResetsToDig; MinesweeperBoardTest
howToPlayHintFollowsTheTapMode switches the toggle and checks the
text. :games:minesweeper:testDevUnitTest in the toolchain container.

Diff

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 74903cbdde..adde53bcae 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@@ -58,14 +58,19 @@ class MinesweeperViewModel(application: Application) :      fun getSizeStats(size: BoardSize) = statsRepository.getSizeStats(size) -    /** Deals a blank field for [config]. Mines are not laid until the first dig. */+    /**+     * Deals a blank field for [config]. Mines are not laid until the first dig.+     *+     * Every field starts in dig mode. Flag mode used to carry over from the previous field, so a+     * player who finished in flag mode met a fresh field where the first tap did nothing at all โ€”+     * there is nothing to flag before the mines are laid โ€” while the hint said "Tap to open".+     */     fun newGame(config: GameConfig) {         placedAnyFlag = false         _uiState.value = MinesweeperUiState(             config = config,             game = MinesweeperGameState.empty(config),-            // Carried over: a player who prefers flag mode should not have to re-pick it every field.-            tapMode = _uiState.value.tapMode,+            tapMode = TapMode.DIG,         )     } @@ -75,11 +80,18 @@ class MinesweeperViewModel(application: Application) :      * 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.+     *+     * 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.      */     override fun tapCell(index: Int) {         val state = _uiState.value         val game = state.game ?: return         if (game.isOver) return+        if (!game.started) {+            dig(state, game, index)+            return+        }          if (game.started && game.revealed[index]) {             chord(game, index)
games/minesweeper/src/main/java/com/vayunmathur/games/minesweeper/ui/GameBoardScreen.kt
diff --git a/games/minesweeper/src/main/java/com/vayunmathur/games/minesweeper/ui/GameBoardScreen.kt b/games/minesweeper/src/main/java/com/vayunmathur/games/minesweeper/ui/GameBoardScreen.ktindex 868da3b59b..b214b6b5b0 100644--- a/games/minesweeper/src/main/java/com/vayunmathur/games/minesweeper/ui/GameBoardScreen.kt+++ b/games/minesweeper/src/main/java/com/vayunmathur/games/minesweeper/ui/GameBoardScreen.kt@@ -126,8 +126,12 @@ fun GameBoardScreen(                 )                  if (!game.started) {+                    // Follows the mode, so the hint never describes a gesture the toggle has swapped.                     Text(-                        stringResource(R.string.how_to_play),+                        stringResource(+                            if (state.tapMode == TapMode.FLAG) R.string.how_to_play_flag+                            else R.string.how_to_play+                        ),                         style = MaterialTheme.typography.bodySmall,                         color = MaterialTheme.colorScheme.onSurfaceVariant,                     )
games/minesweeper/src/main/res/values/strings.xml
diff --git a/games/minesweeper/src/main/res/values/strings.xml b/games/minesweeper/src/main/res/values/strings.xmlindex c9958622cb..7fb29f4f44 100644--- a/games/minesweeper/src/main/res/values/strings.xml+++ b/games/minesweeper/src/main/res/values/strings.xml@@ -20,6 +20,8 @@      <!-- GameBoardScreen: Hint under the field explaining the two gestures. Imperative sentence -->     <string name="how_to_play">Tap to open, long press to flag</string>+    <!-- GameBoardScreen: Hint under the field while flag mode is selected, before the first move. Imperative sentence -->+    <string name="how_to_play_flag">First tap opens the field, then tap to flag, long press to open</string>     <!-- TapModeToggle: Tap uncovers a cell. Verb, imperative -->     <string name="mode_dig">Dig</string>     <!-- TapModeToggle: Tap plants a flag on a suspected mine. Noun used as a verb, imperative -->
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 389d77d970..3230341700 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@@ -69,12 +69,12 @@ class MinesweeperViewModelTest {     }      @Test-    fun newGameDealsBlankFieldAndKeepsTapMode() {+    fun newGameDealsBlankFieldInDigMode() {         val vm = vm()         vm.setTapMode(TapMode.FLAG)         vm.newGame(GameConfig(BoardSize.SMALL, Difficulty.EASY))         val state = vm.uiState.value-        assertEquals(TapMode.FLAG, state.tapMode)+        assertEquals(TapMode.DIG, state.tapMode, "a fresh field always starts in dig mode")         val game = state.game!!         assertEquals(80, game.cellCount)         assertTrue(game.mines.none { it })@@ -100,6 +100,22 @@ class MinesweeperViewModelTest {         assertEquals(1, vm.uiState.value.game!!.elapsedSeconds)     } +    @Test+    fun firstTapDigsEvenInFlagModeAndRestartResetsToDig() {+        val vm = vm()+        vm.newGame(GameConfig(BoardSize.SMALL, Difficulty.EASY))+        vm.setTapMode(TapMode.FLAG)+        vm.tapCell(0)+        val game = vm.uiState.value.game!!+        assertTrue(game.started, "the opening tap digs whatever the mode")+        assertTrue(game.revealed[0])+        assertEquals(0, game.flagsPlaced)+        assertEquals(TapMode.FLAG, vm.uiState.value.tapMode, "the chosen mode stays for this field")++        vm.restart()+        assertEquals(TapMode.DIG, vm.uiState.value.tapMode)+    }+     @Test     fun flagBeforeStartDoesNothingModeToggleFlagsAfterStart() {         val vm = vm()
games/minesweeper/src/test/java/com/vayunmathur/games/minesweeper/ui/MinesweeperBoardTest.kt
diff --git a/games/minesweeper/src/test/java/com/vayunmathur/games/minesweeper/ui/MinesweeperBoardTest.kt b/games/minesweeper/src/test/java/com/vayunmathur/games/minesweeper/ui/MinesweeperBoardTest.ktindex f67f275a90..965cfa90fc 100644--- a/games/minesweeper/src/test/java/com/vayunmathur/games/minesweeper/ui/MinesweeperBoardTest.kt+++ b/games/minesweeper/src/test/java/com/vayunmathur/games/minesweeper/ui/MinesweeperBoardTest.kt@@ -1,5 +1,8 @@ package com.vayunmathur.games.minesweeper.ui +import androidx.compose.runtime.getValue+import androidx.compose.runtime.mutableStateOf+import androidx.compose.runtime.setValue import androidx.compose.ui.test.assertHasClickAction import androidx.compose.ui.test.assertIsDisplayed import androidx.compose.ui.test.junit4.createComposeRule@@ -108,6 +111,18 @@ class MinesweeperBoardTest {         assertEquals(null, mode)     } +    @Test+    fun howToPlayHintFollowsTheTapMode() {+        val config = GameConfig(BoardSize.SMALL, Difficulty.EASY)+        val game = MinesweeperGameState.empty(config)+        var state by mutableStateOf(MinesweeperUiState(config = config, game = game))+        composeRule.setContent { GameBoardScreen(state, MinesweeperActions.Noop, onExit = {}) }+        composeRule.onNodeWithText(str(R.string.how_to_play)).assertExists()+        state = state.copy(tapMode = TapMode.FLAG)+        composeRule.onNodeWithText(str(R.string.how_to_play_flag)).assertExists()+        composeRule.onNodeWithText(str(R.string.how_to_play)).assertDoesNotExist()+    }+     @Test     fun boardScreenLoadingWhenNoGame() {         composeRule.setContent {