Modern Apps git

← Modern-Apps.git

games: redrawing a line from its other endpoint is a no-op, not a move

Commit
75a6d0861bc333f210abc5fddc108095f792c9f2
Author
Vayun Mathur <[email protected]>
When
2026-12-01 12:30 UTC (4 days ago)
Parents
afd4f61
applyStroke stored the stroke verbatim, so tracing a
completed line backwards from its other dot produced a
different stored path for a geometrically identical board.
commitDraw only suppresses a move when the states compare
equal, so the redraw cost a move (extra undo step, extra
pipes_1000 increment, could cost the optimal-move star)
despite the documented 'a redraw of the same line leaves
the move count and undo history alone'.

When the incoming stroke is the exact reverse of the
colour's stored line, the stored direction is now kept —
the board compares equal and preview, commit counting,
undo and persistence all treat it as a no-op with no
ViewModel change. Regression tests at both the board and
stroke layers (including a single undo clearing the line)
failed pre-fix. Generation, variants and assets were
audited and cleared. Verified: :games:pipes:
testDevUnitTest, lint and ktlintCheck BUILD SUCCESSFUL.

Diff

games/pipes/src/main/java/com/vayunmathur/games/pipes/domain/BoardState.kt
diff --git a/games/pipes/src/main/java/com/vayunmathur/games/pipes/domain/BoardState.kt b/games/pipes/src/main/java/com/vayunmathur/games/pipes/domain/BoardState.ktindex a1a700dbb1..68f3cb607c 100644--- a/games/pipes/src/main/java/com/vayunmathur/games/pipes/domain/BoardState.kt+++ b/games/pipes/src/main/java/com/vayunmathur/games/pipes/domain/BoardState.kt@@ -36,7 +36,13 @@ object BoardEdits {             val kept = if (cut < 0) path else path.subList(0, cut).toList()             if (kept.size >= 2) paths[c] = kept         }-        if (stroke.size >= 2) paths[color] = stroke+        if (stroke.size >= 2) {+            // The same line traced from its other dot is the same line: keep the stored+            // direction so the board compares equal and it is not counted as a new move.+            val existing = base.paths[color]+            paths[color] =+                if (existing != null && stroke == existing.reversed()) existing else stroke+        }         return BoardState(paths, owners(level, paths, base.cellOwner, color))     } 
games/pipes/src/test/java/com/vayunmathur/games/pipes/domain/BoardEditsTest.kt
diff --git a/games/pipes/src/test/java/com/vayunmathur/games/pipes/domain/BoardEditsTest.kt b/games/pipes/src/test/java/com/vayunmathur/games/pipes/domain/BoardEditsTest.ktindex 620ae670e6..78f1118c70 100644--- a/games/pipes/src/test/java/com/vayunmathur/games/pipes/domain/BoardEditsTest.kt+++ b/games/pipes/src/test/java/com/vayunmathur/games/pipes/domain/BoardEditsTest.kt@@ -60,6 +60,13 @@ class BoardEditsTest {         assertEquals(1, out.cellOwner[p(2, 2)])     } +    @Test fun redrawingALineFromItsOtherEndpointIsAnIdentity() {+        val level = level()+        val line = listOf(p(2, 4), p(2, 3), p(2, 2), p(2, 1), p(2, 0))+        val base = BoardState(mapOf(1 to line), BoardEdits.owners(level, mapOf(1 to line)))+        assertEquals(base, BoardEdits.applyStroke(level, base, 1, line.reversed()))+    }+     @Test fun shortStrokeRemovesTheColourAndBaseIsUntouched() {         val level = level()         val base = board(level)
games/pipes/src/test/java/com/vayunmathur/games/pipes/platform/PipesStrokeTest.kt
diff --git a/games/pipes/src/test/java/com/vayunmathur/games/pipes/platform/PipesStrokeTest.kt b/games/pipes/src/test/java/com/vayunmathur/games/pipes/platform/PipesStrokeTest.ktindex 040f7e5618..8381074dd9 100644--- a/games/pipes/src/test/java/com/vayunmathur/games/pipes/platform/PipesStrokeTest.kt+++ b/games/pipes/src/test/java/com/vayunmathur/games/pipes/platform/PipesStrokeTest.kt@@ -148,6 +148,23 @@ class PipesStrokeTest {         assertNull(vm.uiState.value.gameState.paths[0])     } +    @Test fun redrawingALineFromItsOtherEndpointIsNotAMove() {+        // 5x5_001 colour 2: (1,2)-(0,4). The same line traced back from the other dot+        // covers exactly the same cells, so it must not add a move or an undo step.+        val vm = vm()+        val green = listOf(CellPos(1, 2), CellPos(1, 3), CellPos(1, 4), CellPos(0, 4))+        draw(vm, green)+        assertEquals(1, vm.getCurrentMoves())+        draw(vm, green.reversed())+        assertEquals(green, vm.uiState.value.gameState.paths[2])+        assertEquals(1, vm.getCurrentMoves())+        vm.onUndo()+        assertTrue(+            vm.uiState.value.gameState.paths+                .isEmpty(),+        )+    }+     @Test fun progressSurvivesLeavingTheLevelAndANewViewModel() {         val vm = vm()         val green = listOf(CellPos(1, 2), CellPos(1, 3), CellPos(1, 4), CellPos(0, 4))