Modern Apps git

← Modern-Apps.git

games/pipes: restore boundary now rejects overlapping pipes

Commit
8ef6cca8ae870d11c8388a2b63895f902b2daa6d
Author
Vayun Mathur <[email protected]>
When
2026-11-30 23:47 UTC (5 days ago)
Parents
2db5fc6
ProgressCodec.decode validated each line in isolation but
never enforced BoardState's cross-line invariant — one line
per cell, two only at a bridge. A hand-crafted (or corrupted)
save could therefore restore two pipes sharing cells, and
BoardStatus.isSolved — which trusts the invariant and only
counts coverage plus pairwise connection — read the overlap
as a solved board. In real play the board is unreachable:
applyStroke cuts any line a stroke crosses outside a bridge.

decodeState now rejects any saved state or history entry
where a cell is covered by 2+ lines outside a bridge, or by
3+ at a bridge; the save starts fresh exactly like the
existing does-not-fit cases. Legitimate bridge saves still
round-trip. Win detection, generation (all 15 variant levels
brute-force verified solvable under the game's own rules),
undo and save-clearing were audited and cleared. Verified:
:games:pipes:testDevUnitTest + lint BUILD SUCCESSFUL.

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 709a5b75a7..cee5927908 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@@ -36,7 +36,7 @@ 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).+     * touching another colour's dot, two lines sharing a cell outside a bridge).      * A level whose layout changed under a saved game then simply starts fresh.      */     fun decode(json: String, level: LevelData): SavedProgress? = runCatching {@@ -73,6 +73,14 @@ object ProgressCodec {             val foreignDots = level.endpoints.filter { it.colorIndex != color }.flatMap { it.cells }             if (path.any { it in foreignDots }) 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+        // counts owners and checks each pair is joined — so lines that share a cell would+        // restore as a board that reads as solved while its pipes overlap.+        val coverCounts = paths.values.flatten().groupingBy { it }.eachCount()+        if (coverCounts.any { (cell, count) -> count > 1 && (cell !in level.bridges || count > 2) }) {+            return null+        }         val bridgeOwners = obj["o"]?.jsonObject.orEmpty().entries.associate { (k, v) ->             val (r, c) = k.split(",").map { it.toInt() }             CellPos(r, c) to v.jsonPrimitive.int
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 32eb046ea4..c9783b7359 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@@ -7,6 +7,7 @@ import com.vayunmathur.games.pipes.data.computeAdjacency import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertNull+import kotlin.test.assertTrue  class ProgressCodecTest {     private fun p(r: Int, c: Int) = CellPos(r, c)@@ -36,6 +37,30 @@ class ProgressCodecTest {         assertEquals(empty, ProgressCodec.decode(ProgressCodec.encode(empty), level))     } +    @Test fun overlappingLinesOutsideABridgeAreDropped() {+        // 3x4: red (0,0)-(0,3) on top, blue (2,0)-(2,3) below, no bridges. Both lines+        // below join their dots and together cover all 12 cells, but they share (1,1)+        // and (1,2) — impossible in play, where drawing into a line cuts it. Restored+        // unchecked, the board satisfied isSolved (owners counted 12, both pairs joined).+        val cells34 = buildSet { for (r in 0 until 3) for (c in 0 until 4) add(p(r, c)) }+        val level34 = LevelData(+            "o", 3, 4, cells34, computeAdjacency(cells34), null,+            listOf(EndpointPair(0, listOf(p(0, 0), p(0, 3))), EndpointPair(1, listOf(p(2, 0), p(2, 3)))),+            emptySet(), 2,+        )+        val red = listOf(p(0, 0), p(0, 1), p(1, 1), p(1, 2), p(0, 2), p(0, 3))+        val blue = listOf(p(2, 0), p(1, 0), p(1, 1), p(2, 1), p(2, 2), p(1, 2), p(1, 3), p(2, 3))+        val paths = mapOf(0 to red, 1 to blue)+        assertTrue(BoardStatus.isSolved(level34, BoardState(paths, BoardEdits.owners(level34, paths))))+        val json = """{"v":1,"s":{"p":{"0":[0,0,0,1,1,1,1,2,0,2,0,3],""" ++            """"1":[2,0,1,0,1,1,2,1,2,2,1,2,1,3,2,3]},"o":{}},"h":[]}"""+        assertNull(ProgressCodec.decode(json, level34))+        // The same overlap inside the saved history is dropped too.+        val inHistory = """{"v":1,"s":{"p":{},"o":{}},"h":[{"p":{"0":[0,0,0,1,1,1,1,2,0,2,0,3],""" ++            """"1":[2,0,1,0,1,1,2,1,2,2,1,2,1,3,2,3]},"o":{}}]}"""+        assertNull(ProgressCodec.decode(inHistory, level34))+    }+     @Test fun progressThatDoesNotFitTheLevelIsDropped() {         fun json(path: String, color: Int = 0) = """{"v":1,"s":{"p":{"$color":[$path]},"o":{}},"h":[]}"""         assertEquals(listOf(p(0, 0), p(0, 1)), ProgressCodec.decode(json("0,0,0,1"), level)!!.state.paths[0])