Maze generation: Difference between revisions

Content added Content deleted
(Added Uiua solution)
m (→‎{{header|Uiua}}: slightly nicer algorithm)
Line 9,679: Line 9,679:
Vis ← 2
Vis ← 2


BreakNS ← ⊂Ver⊂⊃(/↥≡⊡0|⊡0_1)
# Both are ([here, next], maze) -> (maze')
BreakEW ← ⊂Hor⊂⊃(⊡0_0|/↥≡⊡1)
BreakNS ← ⍜⊡(0◌)⊂Ver⊂⊃(/↥≡(⊡0)|⊡0_1)
# ([here, next], maze) -> (maze')
BreakEW ← ⍜⊡(0◌)⊂Hor⊂⊃(⊡0_0|/↥≡(⊡1))
BreakWall ⍜⊡(0◌)⟨BreakNS|BreakEW⟩=°⊟≡⊢.


Neighbours ← +⊙¤[[¯1 0] [1 0] [0 1] [0 ¯1]] # Gives N4
Neighbours ← +⊙¤[[¯1 0] [1 0] [0 1] [0 ¯1]] # Gives N4
Shuffle ← ⊏⍏[⍥⚂]⧻.
Shuffle ← ⊏⍏[⍥⚂]⧻.
IsVisited ← ¬⊡⊂Vis
IsVisited ← ¬⊡⊂Vis
MarkAsVisited ⟜(⍜(⊡|1◌)⊂Vis)
OnlyInBounds ← ▽≡IsVisited ⊙¤,, # (finds the boundary 1's)
OnlyInBounds ← ▽≡IsVisited ⊙¤,, # (finds the boundary 1's)


# (here, maze) -> (maze')
# (here, maze) -> (maze')
Walk ← |2 (
Walk ← |2 (
MarkAsVisited
# Mark as visited.
# (here, maze) -> ([[here, next] x(up to)4], maze)
⟜(⍜(⊡|1◌)⊂Vis)
# Find neighbours. (here, maze) -> ([[here, next] x(up to)4], maze)
≡⊟¤⟜(Shuffle OnlyInBounds Neighbours)
≡⊟¤⟜(Shuffle OnlyInBounds Neighbours)


# Update maze for each in turn.
# Update maze for each in turn. For each, if it
# still isn't visited, break the wall, recurse into it.
∧(
∧(⟨◌|Walk ⊡1 ⟜BreakWall⟩IsVisited◌°⊟,,)
# Is it visited? (check again due to recursion)
IsVisited◌°⊟,,
# Yes: Break NS/EW wall based on x == next x, recurse.
⟨◌|Walk ⊡1 ⟜(⟨BreakNS|BreakEW⟩=°⊟≡⊢.)⟩
)
)
)