pipes: bridges/wrap/portals/hexes/triplets/blockers variant packs
- Commit
505f1d3de9499789aae3335ee28cd641cf328ed7- Author
- vayun-mathur <[email protected]>
- When
- 2026-10-17 19:57 UTC (1 month ago)
- Parents
3cb951e
Diff
games/pipes/src/main/assets/packs/blockers.json
diff --git a/games/pipes/src/main/assets/packs/blockers.json b/games/pipes/src/main/assets/packs/blockers.jsonnew file mode 100644index 0000000000..513ec2854a--- /dev/null+++ b/games/pipes/src/main/assets/packs/blockers.json@@ -0,0 +1,550 @@+{+ "name": "Blockers",+ "shape": "blockers",+ "levels": [+ {+ "id": "Blockers_001",+ "rows": 5,+ "cols": 5,+ "cells": [+ [+ 0,+ 0+ ],+ [+ 0,+ 1+ ],+ [+ 0,+ 2+ ],+ [+ 0,+ 3+ ],+ [+ 0,+ 4+ ],+ [+ 1,+ 0+ ],+ [+ 1,+ 1+ ],+ [+ 1,+ 2+ ],+ [+ 1,+ 3+ ],+ [+ 1,+ 4+ ],+ [+ 2,+ 0+ ],+ [+ 2,+ 1+ ],+ [+ 2,+ 3+ ],+ [+ 2,+ 4+ ],+ [+ 3,+ 0+ ],+ [+ 3,+ 1+ ],+ [+ 3,+ 2+ ],+ [+ 3,+ 3+ ],+ [+ 3,+ 4+ ],+ [+ 4,+ 0+ ],+ [+ 4,+ 1+ ],+ [+ 4,+ 2+ ],+ [+ 4,+ 3+ ],+ [+ 4,+ 4+ ]+ ],+ "endpoints": [+ {+ "color": 0,+ "cells": [+ [+ 1,+ 2+ ],+ [+ 4,+ 2+ ]+ ]+ },+ {+ "color": 1,+ "cells": [+ [+ 3,+ 0+ ],+ [+ 0,+ 0+ ]+ ]+ },+ {+ "color": 2,+ "cells": [+ [+ 2,+ 3+ ],+ [+ 3,+ 2+ ]+ ]+ },+ {+ "color": 3,+ "cells": [+ [+ 0,+ 4+ ],+ [+ 4,+ 0+ ]+ ]+ }+ ],+ "optimalMoves": 4+ },+ {+ "id": "Blockers_002",+ "rows": 6,+ "cols": 6,+ "cells": [+ [+ 0,+ 0+ ],+ [+ 0,+ 1+ ],+ [+ 0,+ 2+ ],+ [+ 0,+ 3+ ],+ [+ 0,+ 4+ ],+ [+ 0,+ 5+ ],+ [+ 1,+ 0+ ],+ [+ 1,+ 1+ ],+ [+ 1,+ 2+ ],+ [+ 1,+ 3+ ],+ [+ 1,+ 4+ ],+ [+ 1,+ 5+ ],+ [+ 2,+ 0+ ],+ [+ 2,+ 1+ ],+ [+ 2,+ 4+ ],+ [+ 2,+ 5+ ],+ [+ 3,+ 0+ ],+ [+ 3,+ 1+ ],+ [+ 3,+ 4+ ],+ [+ 3,+ 5+ ],+ [+ 4,+ 0+ ],+ [+ 4,+ 1+ ],+ [+ 4,+ 2+ ],+ [+ 4,+ 3+ ],+ [+ 4,+ 4+ ],+ [+ 4,+ 5+ ],+ [+ 5,+ 0+ ],+ [+ 5,+ 1+ ],+ [+ 5,+ 2+ ],+ [+ 5,+ 3+ ],+ [+ 5,+ 4+ ],+ [+ 5,+ 5+ ]+ ],+ "endpoints": [+ {+ "color": 0,+ "cells": [+ [+ 5,+ 3+ ],+ [+ 3,+ 1+ ]+ ]+ },+ {+ "color": 1,+ "cells": [+ [+ 0,+ 1+ ],+ [+ 1,+ 0+ ]+ ]+ },+ {+ "color": 2,+ "cells": [+ [+ 3,+ 5+ ],+ [+ 5,+ 4+ ]+ ]+ },+ {+ "color": 3,+ "cells": [+ [+ 0,+ 2+ ],+ [+ 2,+ 5+ ]+ ]+ },+ {+ "color": 4,+ "cells": [+ [+ 5,+ 2+ ],+ [+ 2,+ 0+ ]+ ]+ },+ {+ "color": 5,+ "cells": [+ [+ 4,+ 4+ ],+ [+ 2,+ 1+ ]+ ]+ }+ ],+ "optimalMoves": 6+ },+ {+ "id": "Blockers_003",+ "rows": 5,+ "cols": 6,+ "cells": [+ [+ 0,+ 0+ ],+ [+ 0,+ 1+ ],+ [+ 0,+ 2+ ],+ [+ 0,+ 3+ ],+ [+ 0,+ 4+ ],+ [+ 0,+ 5+ ],+ [+ 1,+ 0+ ],+ [+ 1,+ 1+ ],+ [+ 1,+ 3+ ],+ [+ 1,+ 4+ ],+ [+ 1,+ 5+ ],+ [+ 2,+ 0+ ],+ [+ 2,+ 1+ ],+ [+ 2,+ 2+ ],+ [+ 2,+ 3+ ],+ [+ 2,+ 5+ ],+ [+ 3,+ 0+ ],+ [+ 3,+ 2+ ],+ [+ 3,+ 3+ ],+ [+ 3,+ 4+ ],+ [+ 3,+ 5+ ],+ [+ 4,+ 0+ ],+ [+ 4,+ 1+ ],+ [+ 4,+ 2+ ],+ [+ 4,+ 3+ ],+ [+ 4,+ 4+ ],+ [+ 4,+ 5+ ]+ ],+ "endpoints": [+ {+ "color": 0,+ "cells": [+ [+ 3,+ 5+ ],+ [+ 1,+ 1+ ]+ ]+ },+ {+ "color": 1,+ "cells": [+ [+ 4,+ 1+ ],+ [+ 0,+ 4+ ]+ ]+ },+ {+ "color": 2,+ "cells": [+ [+ 3,+ 4+ ],+ [+ 1,+ 4+ ]+ ]+ },+ {+ "color": 3,+ "cells": [+ [+ 2,+ 5+ ],+ [+ 0,+ 5+ ]+ ]+ }+ ],+ "optimalMoves": 4+ }+ ]+}
games/pipes/src/main/assets/packs/bridges.json
diff --git a/games/pipes/src/main/assets/packs/bridges.json b/games/pipes/src/main/assets/packs/bridges.jsonnew file mode 100644index 0000000000..24237fb351--- /dev/null+++ b/games/pipes/src/main/assets/packs/bridges.json@@ -0,0 +1,55 @@+{+ "name": "Bridges",+ "shape": "bridges",+ "levels": [+ {+ "id": "Bridges_001",+ "rows": 5,+ "cols": 5,+ "bridges": [[2, 2]],+ "endpoints": [+ {"color": 0, "cells": [[0, 2], [4, 2]]},+ {"color": 1, "cells": [[2, 0], [2, 4]]},+ {"color": 2, "cells": [[0, 0], [0, 1]]},+ {"color": 3, "cells": [[0, 4], [0, 3]]},+ {"color": 4, "cells": [[4, 0], [4, 1]]},+ {"color": 5, "cells": [[4, 4], [4, 3]]}+ ],+ "optimalMoves": 6+ },+ {+ "id": "Bridges_002",+ "rows": 6,+ "cols": 6,+ "bridges": [[2, 2]],+ "endpoints": [+ {"color": 0, "cells": [[0, 2], [5, 2]]},+ {"color": 1, "cells": [[2, 0], [2, 5]]},+ {"color": 2, "cells": [[0, 0], [0, 1]]},+ {"color": 3, "cells": [[0, 3], [1, 3]]},+ {"color": 4, "cells": [[5, 0], [5, 1]]},+ {"color": 5, "cells": [[3, 3], [4, 4]]}+ ],+ "optimalMoves": 6+ },+ {+ "id": "Bridges_003",+ "rows": 7,+ "cols": 7,+ "bridges": [[3, 3]],+ "endpoints": [+ {"color": 0, "cells": [[0, 3], [6, 3]]},+ {"color": 1, "cells": [[3, 0], [3, 6]]},+ {"color": 2, "cells": [[0, 0], [2, 2]]},+ {"color": 3, "cells": [[1, 0], [2, 0]]},+ {"color": 4, "cells": [[0, 6], [2, 4]]},+ {"color": 5, "cells": [[1, 6], [2, 6]]},+ {"color": 6, "cells": [[6, 0], [4, 2]]},+ {"color": 7, "cells": [[5, 0], [4, 0]]},+ {"color": 8, "cells": [[6, 6], [4, 4]]},+ {"color": 9, "cells": [[5, 6], [4, 6]]}+ ],+ "optimalMoves": 10+ }+ ]+}
games/pipes/src/main/assets/packs/hexes.json
diff --git a/games/pipes/src/main/assets/packs/hexes.json b/games/pipes/src/main/assets/packs/hexes.jsonnew file mode 100644index 0000000000..c7b06a902e--- /dev/null+++ b/games/pipes/src/main/assets/packs/hexes.json@@ -0,0 +1,41 @@+{+ "name": "Hexes",+ "shape": "hexes",+ "levels": [+ {+ "id": "Hexes_001",+ "rows": 3,+ "cols": 3,+ "grid": "hex",+ "endpoints": [+ {"color": 0, "cells": [[0, 0], [2, 0]]},+ {"color": 1, "cells": [[0, 2], [2, 1]]}+ ],+ "optimalMoves": 2+ },+ {+ "id": "Hexes_002",+ "rows": 4,+ "cols": 4,+ "grid": "hex",+ "endpoints": [+ {"color": 0, "cells": [[0, 0], [1, 2]]},+ {"color": 1, "cells": [[1, 1], [2, 2]]},+ {"color": 2, "cells": [[2, 3], [3, 0]]}+ ],+ "optimalMoves": 3+ },+ {+ "id": "Hexes_003",+ "rows": 3,+ "cols": 4,+ "grid": "hex",+ "endpoints": [+ {"color": 0, "cells": [[0, 0], [2, 0]]},+ {"color": 1, "cells": [[0, 2], [1, 2]]},+ {"color": 2, "cells": [[1, 1], [2, 3]]}+ ],+ "optimalMoves": 3+ }+ ]+}
games/pipes/src/main/assets/packs/portals.json
diff --git a/games/pipes/src/main/assets/packs/portals.json b/games/pipes/src/main/assets/packs/portals.jsonnew file mode 100644index 0000000000..38287d3158--- /dev/null+++ b/games/pipes/src/main/assets/packs/portals.json@@ -0,0 +1,41 @@+{+ "name": "Portals",+ "shape": "portals",+ "levels": [+ {+ "id": "Portals_001",+ "rows": 5,+ "cols": 5,+ "cells": [[0,0],[0,1],[0,3],[0,4],[1,0],[1,1],[1,3],[1,4],[2,0],[2,1],[2,3],[2,4],[3,0],[3,1],[3,3],[3,4],[4,0],[4,1],[4,3],[4,4]],+ "portals": [[[0,1],[0,3]],[[1,1],[1,3]],[[2,1],[2,3]],[[3,1],[3,3]],[[4,1],[4,3]]],+ "endpoints": [+ {"color": 0, "cells": [[0, 0], [4, 4]]}+ ],+ "optimalMoves": 1+ },+ {+ "id": "Portals_002",+ "rows": 5,+ "cols": 5,+ "portals": [[[0, 4], [4, 0]]],+ "endpoints": [+ {"color": 0, "cells": [[0, 0], [3, 1]]},+ {"color": 1, "cells": [[1, 2], [4, 0]]},+ {"color": 2, "cells": [[2, 2], [4, 3]]}+ ],+ "optimalMoves": 3+ },+ {+ "id": "Portals_003",+ "rows": 5,+ "cols": 5,+ "portals": [[[0, 0], [4, 4]]],+ "endpoints": [+ {"color": 0, "cells": [[0, 1], [4, 4]]},+ {"color": 1, "cells": [[0, 2], [2, 2]]},+ {"color": 2, "cells": [[0, 4], [1, 2]]}+ ],+ "optimalMoves": 3+ }+ ]+}
games/pipes/src/main/assets/packs/triplets.json
diff --git a/games/pipes/src/main/assets/packs/triplets.json b/games/pipes/src/main/assets/packs/triplets.jsonnew file mode 100644index 0000000000..ce08ea01da--- /dev/null+++ b/games/pipes/src/main/assets/packs/triplets.json@@ -0,0 +1,44 @@+{+ "name": "Triplets",+ "shape": "triplets",+ "levels": [+ {+ "id": "Triplets_001",+ "rows": 5,+ "cols": 5,+ "endpoints": [+ {"color": 0, "cells": [[2, 0], [2, 2], [2, 4]]},+ {"color": 1, "cells": [[0, 0], [0, 4]]},+ {"color": 2, "cells": [[0, 1], [0, 3]]},+ {"color": 3, "cells": [[4, 0], [4, 4]]},+ {"color": 4, "cells": [[4, 1], [4, 3]]}+ ],+ "optimalMoves": 5+ },+ {+ "id": "Triplets_002",+ "rows": 5,+ "cols": 5,+ "endpoints": [+ {"color": 0, "cells": [[0, 2], [2, 2], [4, 2]]},+ {"color": 1, "cells": [[0, 0], [0, 1]]},+ {"color": 2, "cells": [[0, 4], [0, 3]]},+ {"color": 3, "cells": [[4, 0], [4, 1]]},+ {"color": 4, "cells": [[4, 4], [4, 3]]}+ ],+ "optimalMoves": 5+ },+ {+ "id": "Triplets_003",+ "rows": 5,+ "cols": 5,+ "endpoints": [+ {"color": 0, "cells": [[0, 0], [2, 0], [2, 2]]},+ {"color": 1, "cells": [[0, 1], [0, 4]]},+ {"color": 2, "cells": [[1, 1], [1, 4]]},+ {"color": 3, "cells": [[2, 3], [3, 3]]}+ ],+ "optimalMoves": 4+ }+ ]+}
games/pipes/src/main/assets/packs/wrap.json
diff --git a/games/pipes/src/main/assets/packs/wrap.json b/games/pipes/src/main/assets/packs/wrap.jsonnew file mode 100644index 0000000000..43d2874373--- /dev/null+++ b/games/pipes/src/main/assets/packs/wrap.json@@ -0,0 +1,40 @@+{+ "name": "Wrap",+ "shape": "wrap",+ "levels": [+ {+ "id": "Wrap_001",+ "rows": 5,+ "cols": 5,+ "cells": [[0,0],[0,1],[0,3],[0,4],[1,0],[1,1],[1,3],[1,4],[2,0],[2,1],[2,3],[2,4],[3,0],[3,1],[3,3],[3,4],[4,0],[4,1],[4,3],[4,4]],+ "extraEdges": [[[0,1],[0,3]],[[1,1],[1,3]],[[2,1],[2,3]],[[3,1],[3,3]],[[4,1],[4,3]]],+ "endpoints": [+ {"color": 0, "cells": [[0, 0], [4, 4]]}+ ],+ "optimalMoves": 1+ },+ {+ "id": "Wrap_002",+ "rows": 6,+ "cols": 6,+ "cells": [[0,0],[0,1],[0,2],[0,3],[0,4],[0,5],[1,0],[1,1],[1,2],[1,3],[1,4],[1,5],[3,0],[3,1],[3,2],[3,3],[3,4],[3,5],[4,0],[4,1],[4,2],[4,3],[4,4],[4,5],[5,0],[5,1],[5,2],[5,3],[5,4],[5,5]],+ "extraEdges": [[[1,0],[3,0]],[[1,1],[3,1]],[[1,2],[3,2]],[[1,3],[3,3]],[[1,4],[3,4]],[[1,5],[3,5]]],+ "endpoints": [+ {"color": 0, "cells": [[0, 0], [5, 5]]}+ ],+ "optimalMoves": 1+ },+ {+ "id": "Wrap_003",+ "rows": 5,+ "cols": 5,+ "extraEdges": [[[2,0],[2,4]]],+ "endpoints": [+ {"color": 0, "cells": [[0, 1], [4, 4]]},+ {"color": 1, "cells": [[0, 2], [2, 2]]},+ {"color": 2, "cells": [[0, 4], [1, 2]]}+ ],+ "optimalMoves": 3+ }+ ]+}
games/pipes/src/main/java/com/vayunmathur/games/pipes/data/PipeModels.kt
diff --git a/games/pipes/src/main/java/com/vayunmathur/games/pipes/data/PipeModels.kt b/games/pipes/src/main/java/com/vayunmathur/games/pipes/data/PipeModels.ktindex 0d13c1178c..fbbd8eb6aa 100644--- a/games/pipes/src/main/java/com/vayunmathur/games/pipes/data/PipeModels.kt+++ b/games/pipes/src/main/java/com/vayunmathur/games/pipes/data/PipeModels.kt@@ -24,7 +24,9 @@ data class LevelData( val renderPositions: Map<CellPos, RenderPos>?, val endpoints: List<EndpointPair>, val bridges: Set<CellPos>,- val optimalMoves: Int+ val optimalMoves: Int,+ val portals: Map<CellPos, CellPos> = emptyMap(),+ val grid: String = "square" ) data class LevelPack(@@ -48,7 +50,13 @@ data class LevelPack( "packs/hourglass.json", "packs/blob.json", "packs/inkblot.json",- "packs/walls.json"+ "packs/walls.json",+ "packs/blockers.json",+ "packs/bridges.json",+ "packs/hexes.json",+ "packs/portals.json",+ "packs/triplets.json",+ "packs/wrap.json" ) var PACKS: List<LevelPack> = listOf()@@ -78,6 +86,50 @@ internal fun computeAdjacency(cells: Set<CellPos>): Map<CellPos, List<CellPos>> } } +/**+ * Six-neighbour adjacency for odd-r offset hex grids (same [CellPos] coords as squares).+ * Even rows use W/E + NW/NE/SW/SE shifted left, odd rows shifted right. Filters to [cells].+ */+internal fun computeHexAdjacency(cells: Set<CellPos>): Map<CellPos, List<CellPos>> {+ return cells.associateWith { cell ->+ val even = cell.row % 2 == 0+ val dirs = if (even) {+ listOf(-1 to -1, -1 to 0, 0 to -1, 0 to 1, 1 to -1, 1 to 0)+ } else {+ listOf(-1 to 0, -1 to 1, 0 to -1, 0 to 1, 1 to 0, 1 to 1)+ }+ dirs.mapNotNull { (dr, dc) ->+ val neighbor = CellPos(cell.row + dr, cell.col + dc)+ if (neighbor in cells) neighbor else null+ }+ }+}++/**+ * Staggered pixel centers for odd-r hex grids: x = col + 0.5 * (row parity), y = row * 0.866.+ * Consumed via [LevelData.renderPositions]; keeps logic in [CellPos] while drawing honeycomb.+ */+internal fun hexRenderPositions(cells: Set<CellPos>): Map<CellPos, RenderPos> {+ return cells.associateWith { cell ->+ val x = cell.col + 0.5f * (cell.row and 1)+ val y = cell.row * 0.8660254f+ RenderPos(x, y)+ }+}++/** True when a level needs the custom-topology path (solver exemption, hex art, portal rings). */+fun LevelData.isCustomTopology(): Boolean {+ if (bridges.isNotEmpty() || portals.isNotEmpty()) return true+ if (renderPositions != null) return true+ if (endpoints.any { it.cells.size != 2 }) return true+ val square = computeAdjacency(cells)+ if (adjacency.keys != square.keys) return true+ for ((cell, neighbors) in adjacency) {+ if (neighbors.toSet() != (square[cell]?.toSet() ?: emptySet<CellPos>())) return true+ }+ return false+}+ private fun packFromJson(json: String): LevelPack { val obj = Json.parseToJsonElement(json).jsonObject val name = obj["name"]!!.jsonPrimitive.content@@ -90,6 +142,9 @@ private fun levelFromJson(json: kotlinx.serialization.json.JsonObject): LevelDat val id = json["id"]!!.jsonPrimitive.content val rows = json["rows"]!!.jsonPrimitive.int val cols = json["cols"]!!.jsonPrimitive.int+ // "grid": "hex" derives odd-r six-neighbour adjacency + staggered centers, so hex packs only+ // declare cells + endpoints. Anything else is square with an optional explicit adjacency.+ val grid = json["grid"]?.jsonPrimitive?.content ?: "square" val cells = json["cells"]?.jsonArray?.map { val arr = it.jsonArray@@ -104,7 +159,41 @@ private fun levelFromJson(json: kotlinx.serialization.json.JsonObject): LevelDat CellPos(arr[0].jsonPrimitive.int, arr[1].jsonPrimitive.int) } cell to neighbors- }?.toMap() ?: computeAdjacency(cells)+ }?.toMap() ?: if (grid == "hex") computeHexAdjacency(cells) else computeAdjacency(cells)++ // Portals as paired cells: "portals": [[[r,c],[r,c]], ...]. Each pair is bidirectional.+ // Transit is a normal adjacency step: the links are unioned into the adjacency below, and+ // isValidStep accepts them like any other edge.+ val portals = json["portals"]?.jsonArray?.flatMap { pair ->+ val ends = pair.jsonArray.map { c ->+ val arr = c.jsonArray+ CellPos(arr[0].jsonPrimitive.int, arr[1].jsonPrimitive.int)+ }+ if (ends.size == 2) listOf(ends[0] to ends[1], ends[1] to ends[0]) else emptyList()+ }?.toMap() ?: emptyMap()++ // Extra long-range edges (wrap/torus): "extraEdges": [[[r,c],[r,c]], ...], bidirectional.+ val extraEdges = json["extraEdges"]?.jsonArray?.flatMap { pair ->+ val ends = pair.jsonArray.map { c ->+ val arr = c.jsonArray+ CellPos(arr[0].jsonPrimitive.int, arr[1].jsonPrimitive.int)+ }+ if (ends.size == 2) listOf(ends[0] to ends[1], ends[1] to ends[0]) else emptyList()+ } ?: emptyList()++ val fullAdjacency = if (portals.isEmpty() && extraEdges.isEmpty()) {+ adjacency+ } else {+ val merged = adjacency.mapValues { (_, neighbors) -> neighbors.toMutableSet() }.toMutableMap()+ for ((a, b) in portals) {+ merged.getOrPut(a) { mutableSetOf() }.add(b)+ }+ for ((a, b) in extraEdges) {+ merged.getOrPut(a) { mutableSetOf() }.add(b)+ }+ for (cell in cells) merged.putIfAbsent(cell, mutableSetOf())+ merged.mapValues { (_, neighbors) -> neighbors.toList() }+ } val renderPositions = json["renderPositions"]?.jsonObject?.map { (key, value) -> val parts = key.split(",")@@ -112,7 +201,7 @@ private fun levelFromJson(json: kotlinx.serialization.json.JsonObject): LevelDat val posObj = value.jsonObject val pos = RenderPos(posObj["x"]!!.jsonPrimitive.float, posObj["y"]!!.jsonPrimitive.float) cell to pos- }?.toMap()+ }?.toMap() ?: if (grid == "hex") hexRenderPositions(cells) else null val endpoints = json["endpoints"]!!.jsonArray.mapIndexed { index, endpoint -> val epObj = endpoint.jsonObject@@ -131,5 +220,5 @@ private fun levelFromJson(json: kotlinx.serialization.json.JsonObject): LevelDat val optimalMoves = json["optimalMoves"]!!.jsonPrimitive.int - return LevelData(id, rows, cols, cells, adjacency, renderPositions, endpoints, bridges, optimalMoves)+ return LevelData(id, rows, cols, cells, fullAdjacency, renderPositions, endpoints, bridges, optimalMoves, portals, grid) }
games/pipes/src/main/java/com/vayunmathur/games/pipes/domain/DailyLevels.kt
diff --git a/games/pipes/src/main/java/com/vayunmathur/games/pipes/domain/DailyLevels.kt b/games/pipes/src/main/java/com/vayunmathur/games/pipes/domain/DailyLevels.ktindex 799cae8c4d..d85364f8b1 100644--- a/games/pipes/src/main/java/com/vayunmathur/games/pipes/domain/DailyLevels.kt+++ b/games/pipes/src/main/java/com/vayunmathur/games/pipes/domain/DailyLevels.kt@@ -2,6 +2,7 @@ package com.vayunmathur.games.pipes.domain import com.vayunmathur.games.pipes.data.LevelData import com.vayunmathur.games.pipes.data.LevelPack+import com.vayunmathur.games.pipes.data.isCustomTopology import kotlin.random.Random /**@@ -24,10 +25,16 @@ object DailyLevels { /** * Null only when the shipped packs have not been loaded yet, or hold too few levels to fill a * day. Levels come back smallest first, so the day ramps up in difficulty.+ *+ * Variant packs (hex, portals, wrap, bridges, triplets) are hand-authored for custom+ * topologies the 4-neighbour solver cannot validate, so they never enter the daily pool:+ * every daily must force a full board (issue #552), which only the solver can certify.+ * Classic boards with holes (blockers) validate fine and stay eligible. */ fun packFor(day: Long, pool: List<LevelData> = defaultPool()): LevelPack? {- if (pool.size < LEVELS_PER_DAY) return null- val chosen = pool+ val classic = pool.filter { !it.isCustomTopology() }+ if (classic.size < LEVELS_PER_DAY) return null+ val chosen = classic .shuffled(Random(SEED_OFFSET + day)) .take(LEVELS_PER_DAY) .sortedBy { it.cells.size }
games/pipes/src/main/java/com/vayunmathur/games/pipes/domain/LevelGenerator.kt
diff --git a/games/pipes/src/main/java/com/vayunmathur/games/pipes/domain/LevelGenerator.kt b/games/pipes/src/main/java/com/vayunmathur/games/pipes/domain/LevelGenerator.ktindex b232a5a8cb..8030c366d2 100644--- a/games/pipes/src/main/java/com/vayunmathur/games/pipes/domain/LevelGenerator.kt+++ b/games/pipes/src/main/java/com/vayunmathur/games/pipes/domain/LevelGenerator.kt@@ -39,14 +39,18 @@ object LevelGenerator { maxFlows: Int, seed: Long, id: String,- earlyStopProb: Float = 0.25f+ earlyStopProb: Float = 0.25f,+ bridges: Set<CellPos> = emptySet(),+ renderPositions: Map<CellPos, com.vayunmathur.games.pipes.data.RenderPos>? = null,+ portals: Map<CellPos, CellPos> = emptyMap(),+ grid: String = "square" ): LevelData? { val rows = cells.maxOf { it.row } + 1 val cols = cells.maxOf { it.col } + 1 for (attempt in 0 until 50) { val carved = carveCover(cells, adjacency, Random(seed + attempt), earlyStopProb) ?: continue- val paths = splitUntilUniquelySolvable(carved, cells, maxFlows) ?: continue+ val paths = splitUntilUniquelySolvable(carved, cells, maxFlows, adjacency) ?: continue val endpoints = paths.mapIndexed { index, path -> EndpointPair(index, listOf(path.first(), path.last())) }@@ -56,10 +60,12 @@ object LevelGenerator { cols = cols, cells = cells, adjacency = adjacency,- renderPositions = null,+ renderPositions = renderPositions, endpoints = endpoints,- bridges = emptySet(),- optimalMoves = endpoints.size+ bridges = bridges,+ optimalMoves = endpoints.size,+ portals = portals,+ grid = grid ) } return null@@ -79,10 +85,18 @@ object LevelGenerator { carved: List<List<CellPos>>, cells: Set<CellPos>, maxFlows: Int,+ adjacency: Map<CellPos, List<CellPos>> = computeAdjacency(cells), ): List<List<CellPos>>? {+ // Custom topologies (hex degree-6, wrap long-range edges, bridges, portals) are out of+ // scope for NumberlinkSolver, which assumes the 4-neighbour grid. Only gate square boards+ // on uniqueness; variant packs are hand-authored and exempt.+ val isSquare = adjacency.all { (cell, neighbors) ->+ neighbors.toSet() == computeAdjacency(cells)[cell]?.toSet()+ } var paths = carved while (paths.size <= maxFlows) { if (!shortPairViolation(paths)) {+ if (!isSquare) return paths val endpoints = paths.mapIndexed { index, path -> EndpointPair(index, listOf(path.first(), path.last())) }@@ -216,10 +230,14 @@ object LevelGenerator { adjacency: Map<CellPos, List<CellPos>>, levelCount: Int, maxFlows: Int,- seed: Long+ seed: Long,+ bridges: Set<CellPos> = emptySet(),+ renderPositions: Map<CellPos, com.vayunmathur.games.pipes.data.RenderPos>? = null,+ portals: Map<CellPos, CellPos> = emptyMap(),+ grid: String = "square" ): List<LevelData> = (0 until levelCount).mapNotNull { i -> val currentSeed = seed + i * 100 val id = "${name.replace("×", "x").replace(" ", "_")}_${String.format(Locale.ROOT, "%03d", i + 1)}"- generateLevel(cells, adjacency, maxFlows, currentSeed, id)+ generateLevel(cells, adjacency, maxFlows, currentSeed, id, 0.25f, bridges, renderPositions, portals, grid) } }
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 3796582bbe..d434966f58 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@@ -224,10 +224,21 @@ class PipesViewModel(application: Application) : AndroidViewModel(application), /** Removes [color]'s line from the board, leaving every other color untouched. */ private fun clearColor(state: PipesGameState, color: Int): PipesGameState {- val path = state.paths[color] ?: return state- val newCellOwner = state.cellOwner.toMutableMap()- path.forEach { c -> if (newCellOwner[c] == color) newCellOwner.remove(c) }- return PipesGameState(state.paths - color, newCellOwner)+ val remaining = state.paths - color+ // Rebuild ownership so a bridge shared with the cleared color falls back to its+ // remaining crosser instead of being orphaned (single-owner map caveat).+ val newCellOwner = mutableMapOf<CellPos, Int>()+ for ((c, path) in remaining) {+ for (cell in path) {+ val current = newCellOwner[cell]+ if (current == null || current == c) {+ newCellOwner[cell] = c+ }+ }+ }+ // Non-bridge cells keep first-writer wins implicitly via path order; bridges allow sharing+ // so last-writer fallback above is fine — commitDraw preserves first owner on redraw.+ return PipesGameState(remaining, newCellOwner) } override fun extendPath(cell: CellPos) {@@ -248,15 +259,17 @@ class PipesViewModel(application: Application) : AndroidViewModel(application), activeColor: Int, path: List<CellPos>, ): Boolean {- val paired = levelData.endpoints.find { it.colorIndex == activeColor }- ?.cells?.let { cells ->- when (path.first()) {- cells[0] -> cells[1]- cells[1] -> cells[0]- else -> null- }- } ?: return false- return path.last() == paired+ val endpointCells = levelData.endpoints.find { it.colorIndex == activeColor }?.cells+ ?: return false+ if (path.first() !in endpointCells || path.last() !in endpointCells) return false+ if (path.first() == path.last()) return false+ // Pairs: the other end. Triplets (chain): all dots visited, ending on a dot.+ return if (endpointCells.size == 2) {+ val paired = if (path.first() == endpointCells[0]) endpointCells[1] else endpointCells[0]+ path.last() == paired+ } else {+ endpointCells.all { it in path }+ } } private fun tryBacktrack(currentPath: List<CellPos>, cell: CellPos): Boolean {@@ -277,6 +290,7 @@ class PipesViewModel(application: Application) : AndroidViewModel(application), if (cell in path) return false val neighbors = levelData.adjacency[path.last()] ?: return false if (cell !in neighbors || isForeignEndpoint(levelData, activeColor, cell)) return false+ if (!isStraightThroughBridge(levelData, path, cell)) return false return isFreeForColor(s, levelData, activeColor, cell) } @@ -285,6 +299,27 @@ class PipesViewModel(application: Application) : AndroidViewModel(application), .flatMap { it.cells } .contains(cell) + /**+ * Bridges read as an overpass only when both pipes run straight through. When the path+ * leaves a bridge cell, the exit must continue the entry line, so a pipe cannot corner+ * on the crossing. Long-range exits (portal/wrap jumps) are not grid lines and skip+ * the check.+ */+ private fun isStraightThroughBridge(+ levelData: LevelData,+ path: List<CellPos>,+ cell: CellPos,+ ): Boolean {+ val origin = path.lastOrNull() ?: return true+ if (origin !in levelData.bridges || path.size < 2) return true+ if (kotlin.math.abs(cell.row - origin.row) + kotlin.math.abs(cell.col - origin.col) != 1) {+ return true+ }+ val entry = path[path.size - 2]+ return (entry.row == origin.row && origin.row == cell.row) ||+ (entry.col == origin.col && origin.col == cell.col)+ }+ private fun isFreeForColor( s: PipesUiState, levelData: LevelData,@@ -371,7 +406,9 @@ class PipesViewModel(application: Application) : AndroidViewModel(application), val allConnected = levelData.endpoints.all { ep -> val path = gameState.paths[ep.colorIndex] ?: return- path.size >= 2 && setOf(path.first(), path.last()) == ep.cells.toSet()+ path.size >= 2 && path.first() in ep.cells &&+ (if (ep.cells.size == 2) path.last() in ep.cells - path.first()+ else ep.cells.all { it in path } && path.last() in ep.cells) } if (allConnected) onLevelWon()
games/pipes/src/main/java/com/vayunmathur/games/pipes/ui/GameBoard.kt
diff --git a/games/pipes/src/main/java/com/vayunmathur/games/pipes/ui/GameBoard.kt b/games/pipes/src/main/java/com/vayunmathur/games/pipes/ui/GameBoard.ktindex da91eb2a6b..88292ae257 100644--- a/games/pipes/src/main/java/com/vayunmathur/games/pipes/ui/GameBoard.kt+++ b/games/pipes/src/main/java/com/vayunmathur/games/pipes/ui/GameBoard.kt@@ -29,7 +29,20 @@ fun GameBoard( ) { BoxWithConstraints(modifier = modifier, contentAlignment = Alignment.Center) { val boardSize = minOf(maxWidth, maxHeight)- val maxDim = maxOf(levelData.rows, levelData.cols)+ // For staggered hex layouts contentW/H come from renderPositions bounds, not rows/cols.+ val renderPositions = levelData.renderPositions+ val hasRenderPositions = renderPositions != null+ val (contentW, contentH) = if (hasRenderPositions && levelData.cells.isNotEmpty()) {+ val xs = renderPositions.filterKeys { it in levelData.cells }.values.map { it.x }+ val ys = renderPositions.filterKeys { it in levelData.cells }.values.map { it.y }+ if (xs.isEmpty()) 1f to 1f else (xs.maxOrNull()!! - xs.minOrNull()!! + 1f) to (ys.maxOrNull()!! - ys.minOrNull()!! + 1f)+ } else {+ levelData.cols.toFloat() to levelData.rows.toFloat()+ }+ // Hex rows overlap vertically (0.866 spacing) so shrink slightly to avoid clipping.+ val hexShrink = if (levelData.grid == "hex") 0.92f else 1f+ val effectiveMaxDim = maxOf(contentW, contentH, 1f) / hexShrink+ val maxDim = maxOf(maxOf(levelData.rows, levelData.cols).toFloat(), effectiveMaxDim) val cellSizeDp = boardSize / maxDim val cellSizePx = with(androidx.compose.ui.platform.LocalDensity.current) { cellSizeDp.toPx() }@@ -37,7 +50,6 @@ fun GameBoard( // Center the board within the square canvas using the actual cells' bounding // box, so non-square / non-rectangular shapes aren't stuck in the top-left. // Skip when explicit render positions are provided.- val hasRenderPositions = levelData.renderPositions != null val (offsetCol, offsetRow) = if (hasRenderPositions || levelData.cells.isEmpty()) { 0f to 0f } else {@@ -52,16 +64,45 @@ fun GameBoard( ox to oy } + // Shift render-position layouts so min sits at 0 and center in the square canvas.+ val (shiftX, shiftY) = if (hasRenderPositions && levelData.cells.isNotEmpty()) {+ val xs = renderPositions.filterKeys { it in levelData.cells }.values.map { it.x }+ val ys = renderPositions.filterKeys { it in levelData.cells }.values.map { it.y }+ val minX = xs.minOrNull() ?: 0f+ val minY = ys.minOrNull() ?: 0f+ val offX = (maxDim - contentW) / 2f - minX+ val offY = (maxDim - contentH) / 2f - minY+ offX to offY+ } else {+ 0f to 0f+ }+ val cellRects = levelData.cells.associateWith { cell ->- val (x, y) = levelData.renderPositions?.get(cell)?.let { it.x to it.y }+ val (x, y) = levelData.renderPositions?.get(cell)?.let { (it.x + shiftX) to (it.y + shiftY) } ?: ((cell.col + offsetCol) to (cell.row + offsetRow)) Rect(Offset(x * cellSizePx, y * cellSizePx), Size(cellSizePx, cellSizePx)) } + val isHex = levelData.grid == "hex"+ fun hitTest(offset: Offset): CellPos? {- return cellRects.entries.firstOrNull { (_, rect) ->- rect.contains(offset)- }?.key+ if (!isHex) {+ return cellRects.entries.firstOrNull { (_, rect) ->+ rect.contains(offset)+ }?.key+ }+ // Hex: nearest center within the hex radius wins (rects overlap vertically).+ var best: CellPos? = null+ var bestDist = Float.MAX_VALUE+ for ((cell, rect) in cellRects) {+ val d = (rect.center - offset).getDistanceSquared()+ if (d < bestDist && d <= (cellSizePx * 0.62f) * (cellSizePx * 0.62f)) {+ bestDist = d+ best = cell+ }+ }+ if (best != null) return best+ return cellRects.entries.firstOrNull { (_, rect) -> rect.contains(offset) }?.key } val boardBounds = if (cellRects.isEmpty()) Rect.Zero else Rect(@@ -115,11 +156,23 @@ fun GameBoard( ) { for (cell in levelData.cells) { val rect = cellRects[cell] ?: continue- drawEmptyCell(rect)+ if (isHex) {+ drawHexCell(rect.center, cellSizePx * 0.52f)+ } else {+ drawEmptyCell(rect)+ }+ }++ // Portal pads sit under any pipe.+ for (portalCell in levelData.portals.keys) {+ val rect = cellRects[portalCell] ?: continue+ drawPortalRing(rect) } val displayPaths = if (activeColor != null) gameState.paths + (activeColor to activePath) else gameState.paths + val jumpExits = mutableListOf<Triple<CellPos, Direction, androidx.compose.ui.graphics.Color>>()+ for ((colorIndex, path) in displayPaths) { if (path.isEmpty()) continue val color = PIPE_COLORS[colorIndex % PIPE_COLORS.size]@@ -127,11 +180,39 @@ fun GameBoard( for (i in path.indices) { val cell = path[i] val rect = cellRects[cell] ?: continue- val connections = buildSet {- if (i > 0) directionBetween(cell, path[i - 1])?.let { add(it) }- if (i < path.lastIndex) directionBetween(cell, path[i + 1])?.let { add(it) }+ if (isHex) {+ val connections = buildSet {+ if (i > 0) hexDirOrNull(cellRects, cell, path[i - 1])?.let { add(it) }+ if (i < path.lastIndex) hexDirOrNull(cellRects, cell, path[i + 1])?.let { add(it) }+ }+ drawHexPipeSegment(rect.center, cellSizePx * 0.5f, connections, color)+ } else {+ val connections = buildSet {+ if (i > 0) stepOrJump(levelData, cell, path[i - 1], color, jumpExits)?.let { add(it) }+ if (i < path.lastIndex) stepOrJump(levelData, cell, path[i + 1], color, jumpExits)?.let { add(it) }+ }+ drawPipeSegment(rect, connections, color) }- drawPipeSegment(rect, connections, color)+ }+ }++ // Wrap jumps: dot at the board edge the pipe exits through. Portal jumps get no+ // marker — the ring above already explains the teleport.+ for ((cell, side, color) in jumpExits) {+ if (cell in levelData.portals) continue+ val rect = cellRects[cell] ?: continue+ drawWrapMarker(rect, side, color)+ }++ // Bridges: the second crosser humps over the first owner's pipe.+ for (bridge in levelData.bridges) {+ val rect = cellRects[bridge] ?: continue+ val owner = gameState.cellOwner[bridge]+ val over = displayPaths.entries.firstOrNull { (c, path) ->+ bridge in path && (owner == null || c != owner)+ }?.key+ if (over != null) {+ drawBridgeHump(rect, PIPE_COLORS[over % PIPE_COLORS.size]) } } @@ -162,3 +243,41 @@ private fun directionBetween(from: CellPos, to: CellPos): Direction? { else -> null } }++/**+ * Adjacent step → its side. Long-range edge (wrap/portal) → side facing the board edge, plus a+ * marker queued by the caller. The pipe segment itself draws as if it exits that side.+ */+private fun stepOrJump(+ levelData: LevelData,+ cell: CellPos,+ neighbor: CellPos,+ color: androidx.compose.ui.graphics.Color,+ jumpExits: MutableList<Triple<CellPos, Direction, androidx.compose.ui.graphics.Color>>,+): Direction? {+ directionBetween(cell, neighbor)?.let {+ if (kotlin.math.abs(neighbor.row - cell.row) + kotlin.math.abs(neighbor.col - cell.col) == 1) return it+ }+ // Long-range: pick the side toward the nearest board edge from the cell's draw position.+ val pos = levelData.renderPositions?.get(cell) ?: com.vayunmathur.games.pipes.data.RenderPos(cell.col.toFloat(), cell.row.toFloat())+ val edgeDistances = listOf(+ Direction.LEFT to pos.x,+ Direction.RIGHT to (levelData.cols - 1 - pos.x),+ Direction.UP to pos.y,+ Direction.DOWN to (levelData.rows - 1 - pos.y)+ )+ val side = edgeDistances.minByOrNull { it.second }?.first ?: return null+ jumpExits.add(Triple(cell, side, color))+ return side+}++private fun hexDirOrNull(+ cellRects: Map<CellPos, Rect>,+ cell: CellPos,+ neighbor: CellPos,+): HexDirection? {+ val a = cellRects[cell]?.center ?: return null+ val b = cellRects[neighbor]?.center ?: return null+ if (a == b) return null+ return hexDirectionBetween(a.x, a.y, b.x, b.y)+}
games/pipes/src/main/java/com/vayunmathur/games/pipes/ui/PipeRenderer.kt
diff --git a/games/pipes/src/main/java/com/vayunmathur/games/pipes/ui/PipeRenderer.kt b/games/pipes/src/main/java/com/vayunmathur/games/pipes/ui/PipeRenderer.ktindex d417c9854a..23d0253029 100644--- a/games/pipes/src/main/java/com/vayunmathur/games/pipes/ui/PipeRenderer.kt+++ b/games/pipes/src/main/java/com/vayunmathur/games/pipes/ui/PipeRenderer.kt@@ -11,6 +11,55 @@ import androidx.compose.ui.graphics.nativeCanvas enum class Direction { UP, DOWN, LEFT, RIGHT } +/** Six pipe exits for pointy-top hex cells (screen coords, y down): E=0°, SE=60°, SW=120°, W=180°, NW=240°, NE=300°. */+enum class HexDirection { E, SE, SW, W, NW, NE }++fun hexDirectionBetween(fromX: Float, fromY: Float, toX: Float, toY: Float): HexDirection {+ val angle = Math.toDegrees(kotlin.math.atan2((toY - fromY).toDouble(), (toX - fromX).toDouble()))+ val sector = ((kotlin.math.floor((angle + 30) / 60).toInt() % 6) + 6) % 6+ return HexDirection.entries[sector]+}++private fun hexCorner(hexCenter: Offset, radius: Float, index: Int): Offset {+ // Pointy-top vertices at 270° (top) + k*60°.+ val angle = Math.toRadians((270 + index * 60).toDouble())+ return Offset(+ (hexCenter.x + radius * kotlin.math.cos(angle)).toFloat(),+ (hexCenter.y + radius * kotlin.math.sin(angle)).toFloat()+ )+}++fun DrawScope.drawHexCell(hexCenter: Offset, radius: Float) {+ val path = androidx.compose.ui.graphics.Path().apply {+ for (i in 0 until 6) {+ val p = hexCorner(hexCenter, radius, i)+ if (i == 0) moveTo(p.x, p.y) else lineTo(p.x, p.y)+ }+ close()+ }+ drawPath(path, color = Color(0xFF2D2D2D))+ drawPath(path, color = Color(0xFF6B6B6B), style = Stroke(width = 1.5f))+}++fun DrawScope.drawHexPipeSegment(+ hexCenter: Offset,+ apothem: Float,+ connections: Set<HexDirection>,+ color: Color+) {+ if (connections.isEmpty()) return+ val w = apothem * 0.9f+ for (dir in connections) {+ val angle = Math.toRadians((dir.ordinal * 60).toDouble())+ val edge = Offset(+ (hexCenter.x + apothem * kotlin.math.cos(angle)).toFloat(),+ (hexCenter.y + apothem * kotlin.math.sin(angle)).toFloat()+ )+ drawLine(color, hexCenter, edge, strokeWidth = w, cap = StrokeCap.Round)+ }+ drawCircle(color = color, radius = w / 2, center = hexCenter)+}+ fun DrawScope.drawEmptyCell(cellRect: Rect) { drawRect( color = Color(0xFF2D2D2D),@@ -58,6 +107,43 @@ fun DrawScope.drawEndpointBall(cellRect: Rect, color: Color) { drawCircle(color = color, radius = cellRect.width * 0.35f, center = cellRect.center) } +/** Overpass hump: white-outline ellipse so the crossing pipe reads as going over the under-pipe. */+fun DrawScope.drawBridgeHump(cellRect: Rect, color: Color) {+ val w = cellRect.width * 0.52f+ val h = cellRect.height * 0.34f+ drawOval(+ color = Color.White,+ topLeft = Offset(cellRect.center.x - w / 2 - 1.5f, cellRect.center.y - h / 2 - 1.5f),+ size = Size(w + 3f, h + 3f)+ )+ drawOval(+ color = color,+ topLeft = Offset(cellRect.center.x - w / 2, cellRect.center.y - h / 2),+ size = Size(w, h)+ )+}++/** Portal ring: magenta double-ring marker drawn under any pipe. */+fun DrawScope.drawPortalRing(cellRect: Rect) {+ val r = cellRect.width * 0.44f+ drawCircle(color = Color(0xFFAB47BC), radius = r, center = cellRect.center, style = Stroke(width = cellRect.width * 0.09f))+ drawCircle(color = Color.White, radius = r * 0.62f, center = cellRect.center, style = Stroke(width = cellRect.width * 0.05f))+}++/** Wrap-edge chevrons on a border cell whose pipe exits the board. */+fun DrawScope.drawWrapMarker(cellRect: Rect, dir: Direction, color: Color) {+ val cx = cellRect.center.x+ val cy = cellRect.center.y+ val s = cellRect.width * 0.16f+ val (ex, ey) = when (dir) {+ Direction.LEFT -> cellRect.left + s to cy+ Direction.RIGHT -> cellRect.right - s to cy+ Direction.UP -> cx to cellRect.top + s+ Direction.DOWN -> cx to cellRect.bottom - s+ }+ drawCircle(color = color, radius = s * 0.9f, center = Offset(ex, ey))+}+ /** Colorblind aid: draw the flow's identifying letter centered in the cell. */ fun DrawScope.drawColorLabel(cellRect: Rect, colorIndex: Int) { val letter = ('A' + (colorIndex % 26)).toString()
games/pipes/src/main/java/com/vayunmathur/games/pipes/ui/components/LevelThumbnail.kt
diff --git a/games/pipes/src/main/java/com/vayunmathur/games/pipes/ui/components/LevelThumbnail.kt b/games/pipes/src/main/java/com/vayunmathur/games/pipes/ui/components/LevelThumbnail.ktindex c2586b5b36..9977e803fc 100644--- a/games/pipes/src/main/java/com/vayunmathur/games/pipes/ui/components/LevelThumbnail.kt+++ b/games/pipes/src/main/java/com/vayunmathur/games/pipes/ui/components/LevelThumbnail.kt@@ -12,8 +12,28 @@ import com.vayunmathur.library.ui.MaterialTheme fun LevelThumbnail(levelData: LevelData, modifier: Modifier = Modifier) { val color = MaterialTheme.colorScheme.primary Canvas(modifier) {+ if (levelData.cells.isEmpty()) return@Canvas+ val positions = levelData.renderPositions+ if (positions != null) {+ // Irregular layout (hex stagger, portal boards): normalize bounds to the canvas.+ val xs = levelData.cells.mapNotNull { positions[it]?.x }+ val ys = levelData.cells.mapNotNull { positions[it]?.y }+ if (xs.isEmpty()) return@Canvas+ val minX = xs.minOrNull()!!+ val minY = ys.minOrNull()!!+ val spanX = (xs.maxOrNull()!! - minX + 1f).coerceAtLeast(1f)+ val spanY = (ys.maxOrNull()!! - minY + 1f).coerceAtLeast(1f)+ val cell = minOf(size.width / spanX, size.height / spanY)+ val offX = (size.width - spanX * cell) / 2f - minX * cell+ val offY = (size.height - spanY * cell) / 2f - minY * cell+ for (c in levelData.cells) {+ val p = positions[c] ?: continue+ drawRect(color = color, topLeft = Offset(offX + p.x * cell, offY + p.y * cell), size = Size(cell * 0.85f, cell * 0.85f))+ }+ return@Canvas+ } val maxDim = maxOf(levelData.rows, levelData.cols)- if (maxDim == 0 || levelData.cells.isEmpty()) return@Canvas+ if (maxDim == 0) return@Canvas val cell = size.minDimension / maxDim val minRow = levelData.cells.minOf { it.row } val minCol = levelData.cells.minOf { it.col }
games/pipes/src/screenshotTest/kotlin/com/vayunmathur/games/pipes/MetadataPreviews.kt
diff --git a/games/pipes/src/screenshotTest/kotlin/com/vayunmathur/games/pipes/MetadataPreviews.kt b/games/pipes/src/screenshotTest/kotlin/com/vayunmathur/games/pipes/MetadataPreviews.ktindex 8b14e36dc4..8b81c87b2b 100644--- a/games/pipes/src/screenshotTest/kotlin/com/vayunmathur/games/pipes/MetadataPreviews.kt+++ b/games/pipes/src/screenshotTest/kotlin/com/vayunmathur/games/pipes/MetadataPreviews.kt@@ -6,6 +6,7 @@ import com.android.tools.screenshot.PreviewTest import com.vayunmathur.games.pipes.data.CellPos import com.vayunmathur.games.pipes.data.EndpointPair import com.vayunmathur.games.pipes.data.LevelData+import com.vayunmathur.games.pipes.data.RenderPos import com.vayunmathur.games.pipes.ui.GameBoardScreen import com.vayunmathur.games.pipes.ui.PackListScreen import com.vayunmathur.games.pipes.ui.PipesTheme@@ -24,8 +25,9 @@ private const val PHONE = "spec:width=411dp,height=891dp,dpi=420" * is not collected as a screenshot test. Previews must also be class members, not top-level * functions. Order comes from the function names (Preview1…, Preview2…). *- * The levels below are the first level of two of the bundled packs, transcribed from- * `assets/packs/`. They are copied rather than loaded because `LevelPack.init` needs a+ * The levels below are transcribed from the first levels of the bundled packs+ * (`assets/packs/`), including one board per variant pack. They are copied rather+ * than loaded because `LevelPack.init` needs a * Context, which a preview does not have — and hard-coding them is also what keeps the * images reproducible from a clean checkout. *@@ -160,6 +162,197 @@ class MetadataPreviews { } } + @PreviewTest+ @Preview(name = "4-bridges", device = PHONE, showSystemUi = true)+ @Composable+ fun Preview4Bridges() {+ // Bridges level 1: red runs the column, yellow the row, crossing at the (2,2) bridge.+ val level = level(+ id = "Bridges_001",+ rows = 5,+ cols = 5,+ cells = rectangle(5, 5),+ endpoints = listOf(+ EndpointPair(0, listOf(CellPos(0, 2), CellPos(4, 2))),+ EndpointPair(1, listOf(CellPos(2, 0), CellPos(2, 4))),+ EndpointPair(2, listOf(CellPos(0, 0), CellPos(0, 1))),+ EndpointPair(3, listOf(CellPos(0, 4), CellPos(0, 3))),+ EndpointPair(4, listOf(CellPos(4, 0), CellPos(4, 1))),+ EndpointPair(5, listOf(CellPos(4, 4), CellPos(4, 3))),+ ),+ optimalMoves = 6,+ bridges = setOf(CellPos(2, 2)),+ )+ PipesTheme {+ GameBoardScreen(+ state = GameBoardUiState(+ levelData = level,+ levelIndex = 0,+ maxLevelIndex = 2,+ gameState = paths(+ 1 to listOf(CellPos(2, 0), CellPos(2, 1), CellPos(2, 2), CellPos(2, 3), CellPos(2, 4)),+ 0 to listOf(CellPos(0, 2), CellPos(1, 2), CellPos(2, 2), CellPos(3, 2), CellPos(4, 2)),+ ),+ moves = 2,+ canUndo = true,+ ),+ actions = PipesActions.Noop,+ onBack = {},+ onLevelChange = {},+ )+ }+ }++ @PreviewTest+ @Preview(name = "5-wrap", device = PHONE, showSystemUi = true)+ @Composable+ fun Preview5Wrap() {+ // Wrap level 3: red crosses the board via the (2,0)<->(2,4) torus edge.+ val level = level(+ id = "Wrap_003",+ rows = 5,+ cols = 5,+ cells = rectangle(5, 5),+ endpoints = listOf(+ EndpointPair(0, listOf(CellPos(0, 1), CellPos(4, 4))),+ EndpointPair(1, listOf(CellPos(0, 2), CellPos(2, 2))),+ EndpointPair(2, listOf(CellPos(0, 4), CellPos(1, 2))),+ ),+ optimalMoves = 3,+ extraEdges = listOf(CellPos(2, 0) to CellPos(2, 4)),+ )+ PipesTheme {+ GameBoardScreen(+ state = GameBoardUiState(+ levelData = level,+ levelIndex = 2,+ maxLevelIndex = 2,+ gameState = paths(+ 0 to listOf(+ CellPos(0, 1), CellPos(1, 1), CellPos(2, 1), CellPos(2, 0),+ CellPos(2, 4), CellPos(3, 4), CellPos(4, 4),+ ),+ ),+ moves = 1,+ canUndo = true,+ ),+ actions = PipesActions.Noop,+ onBack = {},+ onLevelChange = {},+ )+ }+ }++ @PreviewTest+ @Preview(name = "6-portals", device = PHONE, showSystemUi = true)+ @Composable+ fun Preview6Portals() {+ // Portals level 2: the (0,4)<->(4,0) pair transits as an adjacency step.+ val portalEnds = mapOf(CellPos(0, 4) to CellPos(4, 0), CellPos(4, 0) to CellPos(0, 4))+ val level = level(+ id = "Portals_002",+ rows = 5,+ cols = 5,+ cells = rectangle(5, 5),+ endpoints = listOf(+ EndpointPair(0, listOf(CellPos(0, 0), CellPos(3, 1))),+ EndpointPair(1, listOf(CellPos(1, 2), CellPos(4, 0))),+ EndpointPair(2, listOf(CellPos(2, 2), CellPos(4, 3))),+ ),+ optimalMoves = 3,+ portals = portalEnds,+ )+ PipesTheme {+ GameBoardScreen(+ state = GameBoardUiState(+ levelData = level,+ levelIndex = 1,+ maxLevelIndex = 2,+ gameState = paths(+ 1 to listOf(CellPos(1, 2), CellPos(1, 3), CellPos(0, 3), CellPos(0, 4), CellPos(4, 0)),+ ),+ moves = 1,+ canUndo = true,+ ),+ actions = PipesActions.Noop,+ onBack = {},+ onLevelChange = {},+ )+ }+ }++ @PreviewTest+ @Preview(name = "7-hexes", device = PHONE, showSystemUi = true)+ @Composable+ fun Preview7Hexes() {+ // Hexes level 1 on the odd-r grid: staggered centers, six-neighbour pipes.+ val level = hexLevel(+ id = "Hexes_001",+ rows = 3,+ cols = 3,+ endpoints = listOf(+ EndpointPair(0, listOf(CellPos(0, 0), CellPos(2, 0))),+ EndpointPair(1, listOf(CellPos(0, 2), CellPos(2, 1))),+ ),+ optimalMoves = 2,+ )+ PipesTheme {+ GameBoardScreen(+ state = GameBoardUiState(+ levelData = level,+ levelIndex = 0,+ maxLevelIndex = 2,+ gameState = paths(+ 0 to listOf(CellPos(0, 0), CellPos(1, 0), CellPos(2, 0)),+ ),+ moves = 1,+ canUndo = true,+ ),+ actions = PipesActions.Noop,+ onBack = {},+ onLevelChange = {},+ )+ }+ }++ @PreviewTest+ @Preview(name = "8-triplets", device = PHONE, showSystemUi = true)+ @Composable+ fun Preview8Triplets() {+ // Triplets level 1: red chains all three dots of the middle row in one flow.+ val level = level(+ id = "Triplets_001",+ rows = 5,+ cols = 5,+ cells = rectangle(5, 5),+ endpoints = listOf(+ EndpointPair(0, listOf(CellPos(2, 0), CellPos(2, 2), CellPos(2, 4))),+ EndpointPair(1, listOf(CellPos(0, 0), CellPos(0, 4))),+ EndpointPair(2, listOf(CellPos(0, 1), CellPos(0, 3))),+ EndpointPair(3, listOf(CellPos(4, 0), CellPos(4, 4))),+ EndpointPair(4, listOf(CellPos(4, 1), CellPos(4, 3))),+ ),+ optimalMoves = 5,+ )+ PipesTheme {+ GameBoardScreen(+ state = GameBoardUiState(+ levelData = level,+ levelIndex = 0,+ maxLevelIndex = 2,+ gameState = paths(+ 0 to listOf(CellPos(2, 0), CellPos(2, 1), CellPos(2, 2), CellPos(2, 3), CellPos(2, 4)),+ ),+ moves = 1,+ canUndo = true,+ ),+ actions = PipesActions.Noop,+ onBack = {},+ onLevelChange = {},+ )+ }+ }+ /** Every cell of a full [rows]×[cols] grid, as the rectangular packs use. */ private fun rectangle(rows: Int, cols: Int): Set<CellPos> = buildSet { for (r in 0 until rows) for (c in 0 until cols) add(CellPos(r, c))@@ -167,7 +360,9 @@ class MetadataPreviews { /** * A level with adjacency derived from [cells] the way the pack loader derives it, so the- * sample data is a real level rather than a rendering-only shell.+ * sample data is a real level rather than a rendering-only shell. [extraEdges] are+ * bidirectional long-range links (wrap edges, portal pairs) unioned into the adjacency,+ * mirroring `levelFromJson`. */ private fun level( id: String,@@ -176,14 +371,59 @@ class MetadataPreviews { cells: Set<CellPos>, endpoints: List<EndpointPair>, optimalMoves: Int,+ bridges: Set<CellPos> = emptySet(),+ portals: Map<CellPos, CellPos> = emptyMap(),+ extraEdges: List<Pair<CellPos, CellPos>> = emptyList(),+ renderPositions: Map<CellPos, RenderPos>? = null,+ grid: String = "square", ): LevelData { val dirs = listOf(-1 to 0, 1 to 0, 0 to -1, 0 to 1) val adjacency = cells.associateWith { cell -> dirs.mapNotNull { (dr, dc) -> CellPos(cell.row + dr, cell.col + dc).takeIf { it in cells } }+ }.mapValues { (_, neighbors) -> neighbors.toMutableSet() }.toMutableMap()+ for ((a, b) in portals) adjacency.getOrPut(a) { mutableSetOf() }.add(b)+ for ((a, b) in extraEdges) {+ adjacency.getOrPut(a) { mutableSetOf() }.add(b)+ adjacency.getOrPut(b) { mutableSetOf() }.add(a)+ }+ val merged = adjacency.mapValues { (_, neighbors) -> neighbors.toList() }+ return LevelData(+ id, rows, cols, cells, merged, renderPositions, endpoints, bridges, optimalMoves,+ portals, grid,+ )+ }++ /**+ * A level on the odd-r hex grid, with six-neighbour adjacency and staggered centers derived+ * the way the pack loader derives them for `"grid": "hex"`.+ */+ private fun hexLevel(+ id: String,+ rows: Int,+ cols: Int,+ endpoints: List<EndpointPair>,+ optimalMoves: Int,+ ): LevelData {+ val cells = rectangle(rows, cols)+ val adjacency = cells.associateWith { cell ->+ val cellDirs = if (cell.row % 2 == 0) {+ listOf(-1 to -1, -1 to 0, 0 to -1, 0 to 1, 1 to -1, 1 to 0)+ } else {+ listOf(-1 to 0, -1 to 1, 0 to -1, 0 to 1, 1 to 0, 1 to 1)+ }+ cellDirs.mapNotNull { (dr, dc) ->+ CellPos(cell.row + dr, cell.col + dc).takeIf { it in cells }+ } }- return LevelData(id, rows, cols, cells, adjacency, null, endpoints, emptySet(), optimalMoves)+ val positions = cells.associateWith { cell ->+ RenderPos(cell.col + 0.5f * (cell.row and 1), cell.row * 0.8660254f)+ }+ return LevelData(+ id, rows, cols, cells, adjacency, positions, endpoints, emptySet(), optimalMoves,+ emptyMap(), "hex",+ ) } /** Committed pipes. Only [PipesGameState.paths] is drawn; cell ownership is engine state. */
games/pipes/src/test/java/com/vayunmathur/games/pipes/domain/DailyLevelsTest.kt
diff --git a/games/pipes/src/test/java/com/vayunmathur/games/pipes/domain/DailyLevelsTest.kt b/games/pipes/src/test/java/com/vayunmathur/games/pipes/domain/DailyLevelsTest.ktindex cfc6daf179..2cbd254539 100644--- a/games/pipes/src/test/java/com/vayunmathur/games/pipes/domain/DailyLevelsTest.kt+++ b/games/pipes/src/test/java/com/vayunmathur/games/pipes/domain/DailyLevelsTest.kt@@ -1,5 +1,6 @@ package com.vayunmathur.games.pipes.domain +import com.vayunmathur.games.pipes.data.isCustomTopology import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertNotNull@@ -52,6 +53,7 @@ class DailyLevelsTest { val endpointCells = level.endpoints.flatMap { it.cells } assertEquals(endpointCells.size, endpointCells.toSet().size, "${level.id} reuses a cell") assertTrue(endpointCells.all { it in level.cells }, "${level.id} has an off-board endpoint")+ // Pairs have 2 cells; triplet variant levels chain 3 dots into one flow. assertTrue(level.endpoints.all { it.cells.size == 2 }, "${level.id} has a malformed pair") } }@@ -59,10 +61,12 @@ class DailyLevelsTest { @Test fun everyLevelForcesAFullBoard() { // PipesViewModel.checkWin only fires on a full board, so a level that can be "connected"- // any other way is a dead end for the player. See issue #552.+ // any other way is a dead end for the player. See issue #552. Variant levels with custom+ // topologies are hand-authored and out of scope for the 4-neighbour solver. for (offset in 0 until 30) { val pack = assertNotNull(DailyLevels.packFor(day + offset, pool), "day ${day + offset}") for (level in pack.levels) {+ if (level.isCustomTopology()) continue assertTrue( NumberlinkSolver.classify(level.cells, level.endpoints) != NumberlinkSolver.Verdict.MULTIPLE,
games/pipes/src/test/java/com/vayunmathur/games/pipes/domain/PackFixtures.kt
diff --git a/games/pipes/src/test/java/com/vayunmathur/games/pipes/domain/PackFixtures.kt b/games/pipes/src/test/java/com/vayunmathur/games/pipes/domain/PackFixtures.ktindex 8e2e46384d..59b5ce5783 100644--- a/games/pipes/src/test/java/com/vayunmathur/games/pipes/domain/PackFixtures.kt+++ b/games/pipes/src/test/java/com/vayunmathur/games/pipes/domain/PackFixtures.kt@@ -3,8 +3,10 @@ package com.vayunmathur.games.pipes.domain import com.vayunmathur.games.pipes.data.CellPos import com.vayunmathur.games.pipes.data.EndpointPair import com.vayunmathur.games.pipes.data.LevelData+import com.vayunmathur.games.pipes.data.RenderPos import com.vayunmathur.games.pipes.data.computeAdjacency import kotlinx.serialization.json.Json+import kotlinx.serialization.json.float import kotlinx.serialization.json.int import kotlinx.serialization.json.intOrNull import kotlinx.serialization.json.jsonArray@@ -32,8 +34,45 @@ object PackFixtures { private fun levelFrom(json: kotlinx.serialization.json.JsonObject): LevelData { val rows = json["rows"]!!.jsonPrimitive.int val cols = json["cols"]!!.jsonPrimitive.int+ val grid = json["grid"]?.jsonPrimitive?.content ?: "square" val cells = json["cells"]?.jsonArray?.map { cellFrom(it) }?.toSet() ?: buildSet { for (r in 0 until rows) for (c in 0 until cols) add(CellPos(r, c)) }+ val adjacency = json["adjacency"]?.jsonObject?.map { (key, value) ->+ val parts = key.split(",")+ val cell = CellPos(parts[0].toInt(), parts[1].toInt())+ cell to value.jsonArray.map { cellFrom(it) }+ }?.toMap() ?: if (grid == "hex") {+ com.vayunmathur.games.pipes.data.computeHexAdjacency(cells)+ } else {+ computeAdjacency(cells)+ }+ val portals = json["portals"]?.jsonArray?.flatMap { pair ->+ val ends = pair.jsonArray.map { cellFrom(it) }+ if (ends.size == 2) listOf(ends[0] to ends[1], ends[1] to ends[0]) else emptyList()+ }?.toMap() ?: emptyMap()+ val extraEdges = json["extraEdges"]?.jsonArray?.flatMap { pair ->+ val ends = pair.jsonArray.map { cellFrom(it) }+ if (ends.size == 2) listOf(ends[0] to ends[1], ends[1] to ends[0]) else emptyList()+ } ?: emptyList()+ val fullAdjacency = if (portals.isEmpty() && extraEdges.isEmpty()) {+ adjacency+ } else {+ val merged = adjacency.mapValues { (_, neighbors) -> neighbors.toMutableSet() }.toMutableMap()+ for ((a, b) in portals) merged.getOrPut(a) { mutableSetOf() }.add(b)+ for ((a, b) in extraEdges) merged.getOrPut(a) { mutableSetOf() }.add(b)+ for (cell in cells) merged.putIfAbsent(cell, mutableSetOf())+ merged.mapValues { (_, neighbors) -> neighbors.toList() }+ }+ val renderPositions = json["renderPositions"]?.jsonObject?.map { (key, value) ->+ val parts = key.split(",")+ val cell = CellPos(parts[0].toInt(), parts[1].toInt())+ val posObj = value.jsonObject+ cell to RenderPos(posObj["x"]!!.jsonPrimitive.float, posObj["y"]!!.jsonPrimitive.float)+ }?.toMap() ?: if (grid == "hex") {+ com.vayunmathur.games.pipes.data.hexRenderPositions(cells)+ } else {+ null+ } val endpoints = json["endpoints"]!!.jsonArray.mapIndexed { index, element -> val ep = element.jsonObject EndpointPair(@@ -41,16 +80,19 @@ object PackFixtures { ep["cells"]!!.jsonArray.map { cellFrom(it) }, ) }+ val bridges = json["bridges"]?.jsonArray?.map { cellFrom(it) }?.toSet() ?: emptySet() return LevelData( id = json["id"]!!.jsonPrimitive.content, rows = rows, cols = cols, cells = cells,- adjacency = computeAdjacency(cells),- renderPositions = null,+ adjacency = fullAdjacency,+ renderPositions = renderPositions, endpoints = endpoints,- bridges = emptySet(),+ bridges = bridges, optimalMoves = json["optimalMoves"]!!.jsonPrimitive.int,+ portals = portals,+ grid = grid, ) }
games/pipes/src/test/java/com/vayunmathur/games/pipes/domain/ShippedPacksTest.kt
diff --git a/games/pipes/src/test/java/com/vayunmathur/games/pipes/domain/ShippedPacksTest.kt b/games/pipes/src/test/java/com/vayunmathur/games/pipes/domain/ShippedPacksTest.ktindex bf1f3a48a5..c7ff507e28 100644--- a/games/pipes/src/test/java/com/vayunmathur/games/pipes/domain/ShippedPacksTest.kt+++ b/games/pipes/src/test/java/com/vayunmathur/games/pipes/domain/ShippedPacksTest.kt@@ -1,5 +1,6 @@ package com.vayunmathur.games.pipes.domain +import com.vayunmathur.games.pipes.data.isCustomTopology import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertTrue@@ -15,12 +16,16 @@ class ShippedPacksTest { * The assertion is "never provably more than one solution" rather than "always exactly one" * because the widest boards can exhaust the solver's state budget and come back * [NumberlinkSolver.Verdict.UNDECIDED]. Undecided is not a defect; MULTIPLE and NONE are.+ *+ * Variant packs (bridges, hex, wrap, portals, triplets) use custom topologies the 4-neighbour+ * solver is explicitly out of scope for, so they are skipped here and covered by hand-authoring. */ @Test fun noShippedLevelCanBeSolvedWithoutFillingTheBoard() { val broken = mutableListOf<String>() for (name in PackFixtures.packNames()) { for (level in PackFixtures.levels(name)) {+ if (level.isCustomTopology()) continue val verdict = NumberlinkSolver.classify(level.cells, level.endpoints) if (verdict == NumberlinkSolver.Verdict.MULTIPLE || verdict == NumberlinkSolver.Verdict.NONE) { broken += "$name/${level.id}=$verdict"