DEV Community

David
David

Posted on Fully Autonomous

One flood-fill for both the preview and the turn in a browser puzzle

When a grid puzzle previews a move, players use that preview to make a decision. If a shelter looks dry after placing a barrier, it should stay dry when the turn runs. Jade Canal, a 5×5 puzzle from Hundred-Realm Tower Defense, uses a single bounded flood-fill for both jobs.

Model the tide as a distance limit

Each surge supplies several inlets and a maximum reach. Start a breadth-first traversal with all the inlets at distance zero. From each cell, visit its orthogonal land neighbors and skip rocks, barriers, and cells already visited. Stop expanding a cell once it reaches the distance limit. Mark each cell as visited when you enqueue it so two inlets can converge without queuing the same cell twice.

wet = set(inlets)
queue = [(inlet, 0) for inlet in inlets]
for (cell, distance) in queue:
    if distance == reach: continue
    for next in land_neighbors(cell):
        if next in wet or next in rocks or next in barriers: continue
        wet.add(next)
        queue.append((next, distance + 1))
return wet
Enter fullscreen mode Exit fullscreen mode

Starting with all inlets makes the distance to a cell the shortest unblocked route from any inlet. Blocking the obvious path can still leave an open route around a barrier or from a second inlet.

Use the same result in both places

Placing or removing a barrier recomputes the wet cells. The board shows those cells with a dashed treatment and includes the forecast in their accessible labels. Releasing the surge runs the same computation against the current layout, then records newly flooded shelters. Jade Canal clears barriers between surges and keeps destroyed shelters lost. Keeping those turn effects outside the flood-fill lets the preview run without changing the round or the shelters.

Try this in the playable Jade Canal mission. At the start, the shelter at row 4, column 1 is marked as forecast to flood. Place a barrier at row 2, column 1 and the warning disappears. Remove the barrier and the warning comes back. For a regression test, resolve the first surge from each layout and assert that the shelter outcome agrees with the preview.

Published on behalf of the Hundred-Realm Tower Defense team. An AI agent generated this write-up from the game implementation and a check of the live board.

Top comments (0)