Modern Apps git

← Modern-Apps.git

games/unblockjam: solver rejects blocks hanging off the board

Commit
b6905028e2525b189dca5a21b5deb5241ad64a4a
Author
Vayun Mathur <[email protected]>
When
2026-11-30 23:57 UTC (5 days ago)
Parents
8ef6cca
Board.of documents 'null when the board is malformed' but
validated only emptiness, the main block, and count/extent
caps — never that blocks are positive-sized, fully on the
board, or non-overlapping, although fill()/isBlocked() index
a flat BooleanArray assuming exactly that. A block hanging
off the bottom threw ArrayIndexOutOfBoundsException inside
the solver; one hanging off the right edge silently aliased
into the next row's cells, so the solver explored a different
board than declared and optimalMoves could report a wrong
count.

Board.of now additionally requires sane board extents, every
block positive-sized with a non-negative position fully
inside the board, and no pairwise overlap — all returning
null like the existing malformed cases. This also protects
the daily generator, which treats explore()==null as
'try another layout' and would otherwise crash or accept a
layout from a row-wrapped board. All 850 shipped levels, plus
the 250 originals, daily generation, move legality, win
detection, undo and restore were audited and cleared.
Verified: :games:unblockjam:testDevUnitTest 93 tests and
lint pass.

Diff

games/unblockjam/src/main/java/com/vayunmathur/games/unblockjam/domain/RushHourSolver.kt
diff --git a/games/unblockjam/src/main/java/com/vayunmathur/games/unblockjam/domain/RushHourSolver.kt b/games/unblockjam/src/main/java/com/vayunmathur/games/unblockjam/domain/RushHourSolver.ktindex a617080bc6..7639fb166f 100644--- a/games/unblockjam/src/main/java/com/vayunmathur/games/unblockjam/domain/RushHourSolver.kt+++ b/games/unblockjam/src/main/java/com/vayunmathur/games/unblockjam/domain/RushHourSolver.kt@@ -212,7 +212,33 @@ object RushHourSolver {                 if (main.position.y != level.exit.y) return null                 // Bit-packing bound. The shipped pack tops out at 15 blocks on a 6x6.                 if (blocks.size > MAX_BLOCKS) return null-                if (level.dimension.width > MAX_EXTENT || level.dimension.height > MAX_EXTENT) return null+                if (level.dimension.width !in 1..MAX_EXTENT) return null+                if (level.dimension.height !in 1..MAX_EXTENT) return null+                // The board arrays below are indexed by cell, assuming every block is a+                // positive-sized rectangle lying wholly on the board; a block hanging off+                // an edge either crashes that indexing or silently wraps into the next+                // row. Overlapping blocks are not a legal board either.+                for (block in blocks) {+                    val w = block.dimension.width+                    val h = block.dimension.height+                    if (w < 1 || h < 1 || w > level.dimension.width || h > level.dimension.height) {+                        return null+                    }+                    if (block.position.x < 0 || block.position.y < 0) return null+                    if (block.position.x > level.dimension.width - w) return null+                    if (block.position.y > level.dimension.height - h) return null+                }+                for (i in blocks.indices) {+                    for (j in i + 1 until blocks.size) {+                        val a = blocks[i]+                        val b = blocks[j]+                        val overlap = a.position.x < b.position.x + b.dimension.width &&+                            a.position.x + a.dimension.width > b.position.x &&+                            a.position.y < b.position.y + b.dimension.height &&+                            a.position.y + a.dimension.height > b.position.y+                        if (overlap) return null+                    }+                }                  val n = blocks.size                 val horizontal = BooleanArray(n) {
games/unblockjam/src/test/java/com/vayunmathur/games/unblockjam/domain/RushHourSolverEdgeTest.kt
diff --git a/games/unblockjam/src/test/java/com/vayunmathur/games/unblockjam/domain/RushHourSolverEdgeTest.kt b/games/unblockjam/src/test/java/com/vayunmathur/games/unblockjam/domain/RushHourSolverEdgeTest.ktindex cca157ec7d..c0e36239ac 100644--- a/games/unblockjam/src/test/java/com/vayunmathur/games/unblockjam/domain/RushHourSolverEdgeTest.kt+++ b/games/unblockjam/src/test/java/com/vayunmathur/games/unblockjam/domain/RushHourSolverEdgeTest.kt@@ -48,6 +48,32 @@ class RushHourSolverEdgeTest {         )     } +    @Test+    fun offBoardOverlappingAndDegenerateBlocksAreMalformed() {+        val main = hBlock(0, 2)+        // Hangs off the bottom: occupies (0,5) and (0,6), off the 6x6 board. This used to+        // crash the cell indexing in Board.fill instead of reporting a malformed board.+        assertNull(RushHourSolver.explore(level(listOf(main, vBlock(0, 5)))))+        assertNull(RushHourSolver.optimalMoves(level(listOf(main, vBlock(0, 5)))))+        // Hangs off the right edge: occupies (5,0) and (6,0). The (6,0) cell used to alias+        // (0,1) in the occupied array, so the solver silently solved a different board.+        assertNull(RushHourSolver.explore(level(listOf(main, hBlock(5, 0)))))+        // Negative position, degenerate size, and overlapping blocks are malformed too.+        assertNull(RushHourSolver.explore(level(listOf(main, vBlock(-1, 0)))))+        assertNull(+            RushHourSolver.explore(+                level(listOf(main, Block(Coord(3, 0), Dimension(0, 2), false)))+            )+        )+        assertNull(RushHourSolver.explore(level(listOf(main, hBlock(1, 2)))))+        // A zero-sized board extent is malformed as well.+        assertNull(+            RushHourSolver.explore(+                level(listOf(hBlock(0, 0)), dimension = Dimension(0, 6), exit = Coord(0, 0))+            )+        )+    }+     @Test     fun openBoardWinsInOneAndExplorationRebuildsLevels() {         val lvl = level(listOf(hBlock(0, 2), vBlock(4, 0)))