pipes: stray taps on dots no longer clear lines or cost moves (#ux-audit)
- Commit
37aa132788fbfa8a1ae5a3ca90151af0fc01da14- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-13 01:21 UTC (23 days ago)
- Parents
92f677b
Symptom: brushing a dot of a finished line deleted the whole line and added a move (hurting the best score), and the player had to redraw it or find Undo. Tapping a line without dragging also cost a move. Root cause: startDraw lifted the colour's line the moment an endpoint was touched (clearColor), and commitDraw pushed an undo step for any change to that, and for every stroke of two or more cells even when it redrew the identical line. Fix: - PipesUiState.leftStart tracks whether the finger has left the cell it touched down on. startDraw no longer changes the board; the preview (and so the lifted line) starts with the first extendPath into a different cell. Jitter inside the touched cell is ignored. - commitDraw keeps the board as it was if the finger never left the cell, and only pushes undo history / counts a move / bumps the pipes-placed counter when the resulting board differs from the pre-stroke board. Dragging off a dot and back to it still clears the line, as an undoable move, since the player clearly dragged. - GameScreen passes the active colour to the board only once the stroke has left its start, so the untouched line keeps drawing while the finger rests on its dot. Product note: the same "no change until you drag" rule now also covers grabbing a line part-way (previously a tap there truncated the line). That is the conservative reading of "clear only once the drag leaves the cell"; dragging behaves exactly as before. User-visible effect: a tap on a dot or line changes nothing and costs no move; redrawing a line identically costs no move. Verification: PipesViewModelTest's endpoint-tap test now asserts the line survives a tap (with jitter) at no move cost, and that drag-off- and-back clears it as one undoable move; the part-way-grab case in drawExtendCommitUndoAndRestart now expects no extra undo step.
Diff
games/pipes/src/main/java/com/vayunmathur/games/pipes/platform/PipesViewModel.kt
diff --git a/games/pipes/src/main/java/com/vayunmathur/games/pipes/platform/PipesViewModel.kt b/games/pipes/src/main/java/com/vayunmathur/games/pipes/platform/PipesViewModel.ktindex e8fc8e9493..ea2f2526dc 100644--- a/games/pipes/src/main/java/com/vayunmathur/games/pipes/platform/PipesViewModel.kt+++ b/games/pipes/src/main/java/com/vayunmathur/games/pipes/platform/PipesViewModel.kt@@ -42,7 +42,12 @@ data class PipesUiState( val isLevelWon: Boolean = false, val activeColor: Int? = null, val activePath: List<CellPos> = emptyList(),- val preDrawState: PipesGameState? = null+ val preDrawState: PipesGameState? = null,+ /**+ * Whether the open stroke's finger has left the cell it touched down on. Until it has,+ * the board is left exactly as it was: a stray tap on a dot or a line changes nothing.+ */+ val leftStart: Boolean = false, ) class PipesViewModel(application: Application) : AndroidViewModel(application), PipesActions {@@ -204,6 +209,7 @@ class PipesViewModel(application: Application) : AndroidViewModel(application), activeColor = null, activePath = emptyList(), preDrawState = null,+ leftStart = false, ) } }@@ -213,9 +219,8 @@ class PipesViewModel(application: Application) : AndroidViewModel(application), val base = s.gameState val (color, stroke) = if (endpointColor != null) {- // Grabbing an endpoint always restarts that color: its current line breaks- // right away (like grabbing a line partway does) so the drag is free to take- // a different route. Releasing without drawing leaves the line cleared.+ // Grabbing an endpoint restarts that color, but its current line only breaks+ // once the drag leaves the dot (see extendPath), so a stray tap is harmless. endpointColor to listOf(cell) } else { val ownerColor = base.cellOwner[cell] ?: return@@ -227,8 +232,9 @@ class PipesViewModel(application: Application) : AndroidViewModel(application), it.copy( activeColor = color, activePath = stroke,- gameState = BoardEdits.applyStroke(levelData, base, color, stroke),+ gameState = base, preDrawState = base,+ leftStart = false, ) } }@@ -243,11 +249,17 @@ class PipesViewModel(application: Application) : AndroidViewModel(application), val activeColor = s.activeColor ?: return val levelData = s.levelData ?: return if (s.isLevelWon) return+ // Jitter inside the touched cell is not a drag yet.+ if (!s.leftStart && cell == s.activePath.lastOrNull()) return val base = s.preDrawState ?: s.gameState val next = StrokeRules.extend(levelData, base.cellOwner, activeColor, s.activePath, cell)- if (next == s.activePath) return+ if (next == s.activePath && s.leftStart) return _uiState.update {- it.copy(activePath = next, gameState = BoardEdits.applyStroke(levelData, base, activeColor, next))+ it.copy(+ activePath = next,+ gameState = BoardEdits.applyStroke(levelData, base, activeColor, next),+ leftStart = true,+ ) } } @@ -259,10 +271,14 @@ class PipesViewModel(application: Application) : AndroidViewModel(application), val newPath = s.activePath val preDrawState = s.preDrawState ?: s.gameState- val newGameState = BoardEdits.applyStroke(levelData, preDrawState, activeColor, newPath)- // A stroke that only broke a line (released on its own start cell) counts as a move- // only when it actually removed something.- val counts = newPath.size >= 2 || newGameState != preDrawState+ val newGameState = if (s.leftStart) {+ BoardEdits.applyStroke(levelData, preDrawState, activeColor, newPath)+ } else {+ preDrawState+ }+ // Only a stroke that changed the board is a move: a tap, or a redraw of the same+ // line, leaves the move count and the undo history alone.+ val counts = newGameState != preDrawState _uiState.update { it.copy(@@ -270,10 +286,11 @@ class PipesViewModel(application: Application) : AndroidViewModel(application), history = if (counts) it.history + preDrawState else it.history, activeColor = null, activePath = emptyList(),- preDrawState = null+ preDrawState = null,+ leftStart = false, ) }- if (newPath.size < 2) return+ if (!counts || newPath.size < 2) return viewModelScope.launch(Dispatchers.IO) { repository.incrementTotalPipesPlaced()
games/pipes/src/main/java/com/vayunmathur/games/pipes/ui/GameScreen.kt
diff --git a/games/pipes/src/main/java/com/vayunmathur/games/pipes/ui/GameScreen.kt b/games/pipes/src/main/java/com/vayunmathur/games/pipes/ui/GameScreen.ktindex 2dba45f81f..1983e54cfb 100644--- a/games/pipes/src/main/java/com/vayunmathur/games/pipes/ui/GameScreen.kt+++ b/games/pipes/src/main/java/com/vayunmathur/games/pipes/ui/GameScreen.kt@@ -32,5 +32,5 @@ fun GameScreen(backStack: NavBackStack<Route>, viewModel: PipesViewModel, packIn if (levels == null || levelData == null) { Box(Modifier.fillMaxSize(), Alignment.Center) { CircularProgressIndicator() }; return } val isReady = uiState.packIndex == packIndex && uiState.levelIndex == levelIndex && uiState.levelData != null val currentLevelStats = levelStats[levelData.id]- GameBoardScreen(state = GameBoardUiState(levelData = if (isReady) uiState.levelData!! else levelData, levelIndex = levelIndex, maxLevelIndex = levels.lastIndex, gameState = if (isReady) uiState.gameState else PipesGameState(), activeColor = if (isReady) uiState.activeColor else null, activePath = if (isReady) uiState.activePath else emptyList(), isLevelWon = isReady && uiState.isLevelWon, isCompleted = currentLevelStats != null, colorblind = colorblind, moves = if (isReady) viewModel.getCurrentMoves() else 0, bestScore = currentLevelStats?.bestScore, canUndo = isReady && uiState.history.isNotEmpty()), actions = viewModel, onBack = { backStack.pop() }, onLevelChange = { newIndex -> val clamped = newIndex.coerceIn(0, levels.lastIndex); backStack.setLast(if (isDaily) Route.DailyGame(clamped) else Route.Game(packIndex, clamped)) })+ GameBoardScreen(state = GameBoardUiState(levelData = if (isReady) uiState.levelData!! else levelData, levelIndex = levelIndex, maxLevelIndex = levels.lastIndex, gameState = if (isReady) uiState.gameState else PipesGameState(), activeColor = if (isReady && uiState.leftStart) uiState.activeColor else null, activePath = if (isReady) uiState.activePath else emptyList(), isLevelWon = isReady && uiState.isLevelWon, isCompleted = currentLevelStats != null, colorblind = colorblind, moves = if (isReady) viewModel.getCurrentMoves() else 0, bestScore = currentLevelStats?.bestScore, canUndo = isReady && uiState.history.isNotEmpty()), actions = viewModel, onBack = { backStack.pop() }, onLevelChange = { newIndex -> val clamped = newIndex.coerceIn(0, levels.lastIndex); backStack.setLast(if (isDaily) Route.DailyGame(clamped) else Route.Game(packIndex, clamped)) }) }
games/pipes/src/test/java/com/vayunmathur/games/pipes/platform/PipesViewModelTest.kt
diff --git a/games/pipes/src/test/java/com/vayunmathur/games/pipes/platform/PipesViewModelTest.kt b/games/pipes/src/test/java/com/vayunmathur/games/pipes/platform/PipesViewModelTest.ktindex 2d9ac69b3e..4fbf0db142 100644--- a/games/pipes/src/test/java/com/vayunmathur/games/pipes/platform/PipesViewModelTest.kt+++ b/games/pipes/src/test/java/com/vayunmathur/games/pipes/platform/PipesViewModelTest.kt@@ -122,12 +122,13 @@ class PipesViewModelTest { assertEquals(listOf(start, next), vm.uiState.value.gameState.paths[ep.colorIndex]) assertEquals(1, vm.getCurrentMoves()) assertNull(vm.uiState.value.activeColor)- // Grabbing the drawn line partway truncates it+ // Grabbing the drawn line and releasing without moving changes nothing and is not a move vm.startDraw(next) assertEquals(listOf(start, next), vm.uiState.value.activePath) vm.commitDraw()- // Each commit pushed one undo step: first undo restores the drawn line, second the empty board- vm.onUndo(); assertEquals(listOf(start, next), vm.uiState.value.gameState.paths[ep.colorIndex])+ assertEquals(listOf(start, next), vm.uiState.value.gameState.paths[ep.colorIndex])+ assertEquals(1, vm.getCurrentMoves())+ // The single real move undoes back to the empty board vm.onUndo(); assertTrue(vm.uiState.value.gameState.paths.isEmpty()); assertEquals(0, vm.getCurrentMoves()) vm.onRestart(); assertTrue(vm.uiState.value.gameState.paths.isEmpty()) // Draw again then restart clears@@ -136,16 +137,26 @@ class PipesViewModelTest { assertTrue(vm.uiState.value.gameState.paths.isEmpty()); assertEquals(0, vm.getCurrentMoves()) } - @Test fun tappingEndpointWithoutDrawingBreaksLineUndoably() {+ @Test fun strayTapOnEndpointKeepsLineAndIsNotAMove() { val vm = vm(); vm.loadLevel(0, 0) val level = vm.uiState.value.levelData!! val ep = level.endpoints[0] val next = (level.adjacency[ep.cells[0]] ?: emptyList()).first { n -> level.endpoints.all { n !in it.cells } } draw(vm, listOf(ep.cells[0], next))- assertEquals(1, vm.uiState.value.gameState.paths.size)- vm.startDraw(ep.cells[0]) // grabs endpoint: line breaks immediately+ assertEquals(1, vm.getCurrentMoves())+ // Touching the dot keeps the line while the finger stays on it (jitter included)+ vm.startDraw(ep.cells[0])+ vm.extendPath(ep.cells[0])+ assertEquals(listOf(ep.cells[0], next), vm.uiState.value.gameState.paths[ep.colorIndex])+ vm.commitDraw()+ assertEquals(listOf(ep.cells[0], next), vm.uiState.value.gameState.paths[ep.colorIndex])+ assertEquals(1, vm.getCurrentMoves())+ // Dragging off the dot and back clears the line: that is a real, undoable move+ vm.startDraw(ep.cells[0])+ vm.extendPath(next)+ vm.extendPath(ep.cells[0]) assertTrue(vm.uiState.value.gameState.paths.isEmpty())- vm.commitDraw() // released without drawing: break stays, and is undoable+ vm.commitDraw() assertTrue(vm.uiState.value.gameState.paths.isEmpty()) assertEquals(2, vm.getCurrentMoves()) vm.onUndo()