Skip to content
Adrien Hubert

Wave function collapse, on a pipe tileset.

Every cell in this grid starts holding all sixteen pipe tiles at once. The solver finds the cell with the fewest options left, picks one tile for it at random, and then pushes that decision outward: any neighbour whose remaining tiles no longer line up gets pruned, which can prune its neighbours in turn. Repeat until every cell holds exactly one tile and the pipes join up edge to edge with no loose ends.

Collapsing

The tileset

There are exactly sixteen tiles, one for each way the four edges of a square can be either open or closed: the blank tile, four dead ends, two straights, four elbows, four tees, and the full cross. Two tiles may sit next to each other only when the edge they share agrees. A tile open on its right side demands a neighbour open on its left, and a closed edge demands a closed edge back. That single rule is the whole grammar of the network.

Each cell keeps its options as a sixteen-bit mask, one bit per tile. "Entropy" here is just the number of bits still set. Collapsing a cell clears the mask down to a single bit. Propagation walks a stack of changed cells, recomputing for each neighbour the union of edges its surviving tiles can still present, and intersecting that with the neighbour's mask.

When it fails

Sometimes propagation empties a cell's mask: no tile can satisfy all of its neighbours at once. That is a contradiction, and this solver handles it the blunt way Maxim Gumin's original does. It throws the whole grid away and starts over. The counter in the status line shows how many restarts the current network needed. For a tileset this permissive most attempts succeed on the first try, so the number usually stays low.

Source

The algorithm is Maxim Gumin's Wave Function Collapse, released in 2016 (github.com/mxgmn/WaveFunctionCollapse). This is the "simple tiled" model with adjacency taken from matching edges, the same framing Isaac Karth and Adam Smith use in their 2017 analysis "WaveFunctionCollapse is Constraint Solving in the Wild". The pipe tileset and the sixteen-bit-mask implementation here are mine.