๐งต Wave Function Collapse โ 2D Tile Solver
Superpositions ยท entropy-first collapse ยท constraint propagation ยท backtracking on contradiction
Superpositions ยท entropy-first collapse ยท constraint propagation ยท backtracking on contradiction
A plain-canvas 2D implementation of the Wave Function Collapse algorithm: real superpositions, entropy-guided cell selection, weighted collapse, stack-based constraint propagation, and genuine backtracking when a contradiction is hit.
Every cell starts holding every tile that could ever occupy it. The uncollapsed cell with the fewest remaining candidates (lowest entropy) is chosen next; one candidate is picked at random, weighted by how common that tile should be. Propagation then removes any neighbouring tile whose facing socket no longer matches, rippling outward until the grid stabilises โ or a cell is left with zero options, which triggers a real backtrack: the last collapse is undone, that choice is forbidden, and the search resumes.
Pick Roads or Pipes, set a grid size and speed, then press New Map. Step advances one collapse at a time; Auto runs continuously. Click any uncollapsed cell to force its collapse by hand and watch the constraint wave spread from it.
Unlike a purely visual demo, this solver actually rewinds its own history: each collapse is snapshotted before it happens, so a dead-end can be undone exactly, the offending tile excluded, and generation retried from that point rather than silently forcing a fallback.