pipes: a restored line can no longer corner on a bridge
- Commit
2c6212ed61928b081f643c4313b5da06194b9255- Author
- Vayun Mathur <[email protected]>
- When
- 2026-12-01 06:32 UTC (5 days ago)
- Parents
bb510d3
ProgressCodec.decode re-checked adjacency as raw graph steps, which permits turning at a bridge cell; the live-play rule (pipes run straight through a bridge) lives in StrokeRules and was never applied to restored paths. BoardStatus.isSolved trusts the stored paths, so a save whose colour 0 line corners on the bridge — (0,1)(1,1)(1,2) (1,3) entering from the north and exiting east on the 3x4 board — restored as a 'solved' board that is impossible to draw: all 12 cells covered, every pair joined, and no legal way to produce it. decodeState now validates every step of each restored path through StrokeRules.isValidStep, the same check live drawing uses (portal/wrap long-range steps keep their exemption), so illegal saves start fresh instead of restoring an undrawable board. The regression test failed pre-fix at decode returning the save. Solver enumeration, pack solvability and undo/restore ordering were audited and cleared. Verified: :games:pipes:testDevUnitTest 137 tests pass; lint passes; ktlintCheck clean.
Diff
games/pipes/src/main/java/com/vayunmathur/games/pipes/domain/ProgressCodec.kt
diff --git a/games/pipes/src/main/java/com/vayunmathur/games/pipes/domain/ProgressCodec.kt b/games/pipes/src/main/java/com/vayunmathur/games/pipes/domain/ProgressCodec.ktindex 767cf12a3e..d4e0aabafa 100644--- a/games/pipes/src/main/java/com/vayunmathur/games/pipes/domain/ProgressCodec.kt+++ b/games/pipes/src/main/java/com/vayunmathur/games/pipes/domain/ProgressCodec.kt@@ -37,7 +37,8 @@ object ProgressCodec { /** * Decodes [json] for [level], or null when it is unreadable or does not fit the level * (a cell off the board, a broken or self-crossing line, an unknown colour, a line- * touching another colour's dot, two lines sharing a cell outside a bridge).+ * touching another colour's dot, a line cornering on a bridge, two lines sharing+ * a cell outside a bridge). * A level whose layout changed under a saved game then simply starts fresh. */ fun decode(@@ -100,6 +101,13 @@ object ProgressCodec { if (path.zipWithNext().any { (a, b) -> b !in level.adjacency[a].orEmpty() }) return null val foreignDots = level.endpoints.filter { it.colorIndex != color }.flatMap { it.cells } if (path.any { it in foreignDots }) return null+ // Every step must be one live play could have drawn. Adjacency alone lets a+ // line corner on a bridge, which StrokeRules forbids (a pipe runs straight+ // through a crossing): restored unchecked, such a line can join its dots+ // and fill the board, so the save reads as solved while undrawable.+ for (i in 1 until path.size) {+ if (!StrokeRules.isValidStep(level, color, path.subList(0, i), path[i])) return null+ } } // BoardState's invariant is one line per cell outside a bridge (two at a bridge, // the under-pipe owning it). BoardStatus.isSolved trusts that invariant — it only
games/pipes/src/test/java/com/vayunmathur/games/pipes/domain/ProgressCodecTest.kt
diff --git a/games/pipes/src/test/java/com/vayunmathur/games/pipes/domain/ProgressCodecTest.kt b/games/pipes/src/test/java/com/vayunmathur/games/pipes/domain/ProgressCodecTest.ktindex 8fc78200c9..153c5f3369 100644--- a/games/pipes/src/test/java/com/vayunmathur/games/pipes/domain/ProgressCodecTest.kt+++ b/games/pipes/src/test/java/com/vayunmathur/games/pipes/domain/ProgressCodecTest.kt@@ -6,6 +6,7 @@ import com.vayunmathur.games.pipes.data.LevelData import com.vayunmathur.games.pipes.data.computeAdjacency import kotlin.test.Test import kotlin.test.assertEquals+import kotlin.test.assertFalse import kotlin.test.assertNull import kotlin.test.assertTrue @@ -83,6 +84,43 @@ class ProgressCodecTest { assertNull(ProgressCodec.decode(inHistory, level34)) } + @Test fun aLineCorneringOnABridgeIsDropped() {+ // 3x4, bridge at (1,1). Colour 0 runs (0,1) down through the bridge and out+ // east to (1,3): a corner on the crossing, which StrokeRules forbids in play+ // (a pipe may only run straight through a bridge). Colours 1 and 2 fill the+ // rest, so every pair is joined and all 12 cells are covered — restored+ // unchecked, the board satisfied isSolved despite being undrawable.+ val cells34 = buildSet { for (r in 0 until 3) for (c in 0 until 4) add(p(r, c)) }+ val bridged =+ LevelData(+ "bridge-corner",+ 3,+ 4,+ cells34,+ computeAdjacency(cells34),+ null,+ listOf(+ EndpointPair(0, listOf(p(0, 1), p(1, 3))),+ EndpointPair(1, listOf(p(0, 0), p(2, 3))),+ EndpointPair(2, listOf(p(0, 2), p(0, 3))),+ ),+ setOf(p(1, 1)),+ 3,+ )+ assertFalse(StrokeRules.isValidStep(bridged, 0, listOf(p(0, 1), p(1, 1)), p(1, 2)))+ val paths =+ mapOf(+ 0 to listOf(p(0, 1), p(1, 1), p(1, 2), p(1, 3)),+ 1 to listOf(p(0, 0), p(1, 0), p(2, 0), p(2, 1), p(2, 2), p(2, 3)),+ 2 to listOf(p(0, 2), p(0, 3)),+ )+ assertTrue(BoardStatus.isSolved(bridged, BoardState(paths, BoardEdits.owners(bridged, paths))))+ val json =+ """{"v":1,"s":{"p":{"0":[0,1,1,1,1,2,1,3],""" ++ """"1":[0,0,1,0,2,0,2,1,2,2,2,3],"2":[0,2,0,3]},"o":{}},"h":[]}"""+ assertNull(ProgressCodec.decode(json, bridged))+ }+ @Test fun progressThatDoesNotFitTheLevelIsDropped() { fun json( path: String,