29 lines
891 B
Text
29 lines
891 B
Text
# Build and solve a maze.
|
|
W ← 16
|
|
H ← 8
|
|
|
|
GetWall ← -⊃⋅∘∘⊡1⊃⋅∘∘÷2⊸/-
|
|
# ([here, next], maze) -> (maze')
|
|
BreakWall ← ⍜(⊡|⋅@ )GetWall
|
|
|
|
N₄ ← +⊙¤[¯2_0 2_0 0_2 0_¯2]
|
|
Shuffle ← ⊏⍏⍥⚂⊸⧻
|
|
# (pos maze) -> T if it's already seen (or out of bounds).
|
|
IsVisited ← ≠@\s⬚@\s⊡
|
|
MarkAsVisited ← ⟜(⍜⊡⋅@\s)
|
|
# (here, maze) -> (maze')
|
|
Walk ← |2 (
|
|
MarkAsVisited
|
|
# (here, maze) -> ([[here, next] x(up to)4], maze)
|
|
≡⊟¤⟜(Shuffle N₄)
|
|
# Update maze for each in turn. For each, if it
|
|
# still isn't visited, break the wall, recurse into it.
|
|
∧(⨬(◌|Walk⊡1⟜BreakWall)IsVisited◌°⊟⊃⊙∘⊙∘)
|
|
)
|
|
|
|
# Generate a maze.
|
|
Maze ← (
|
|
↘¯1♭₂↯(+1H)⊟⊂/⊂↯W"+-" @+⊂/⊂↯W"|." @| # Build a filled maze.
|
|
Walk 1_1 # Walk around eating dots and breaking walls.
|
|
)
|
|
Maze
|