Jump to content

Maze generation: Difference between revisions

m
→‎{{header|Uiua}}: slightly nicer algorithm
(Added Uiua solution)
m (→‎{{header|Uiua}}: slightly nicer algorithm)
Line 9,679:
Vis ← 2
 
BreakNS ← ⍜⊡(0◌)⊂Ver⊂⊃(/↥≡(⊡0)↥≡⊡0|⊡0_1)
# Both are ([here, next], maze) -> (maze')
BreakEW ← ⍜⊡(0◌)⊂Hor⊂⊃(⊡0_0|/↥≡(⊡1)↥≡⊡1)
BreakNS ← ⍜⊡(0◌)⊂Ver⊂⊃(/↥≡(⊡0)|⊡0_1)
# Both are ([here, next], maze) -> (maze')
BreakEW ← ⍜⊡(0◌)⊂Hor⊂⊃(⊡0_0|/↥≡(⊡1))
BreakWall ⟨◌|Walk ⊡1 ⟜⍜⊡(0◌)⟨BreakNS|BreakEW⟩=°⊟≡⊢.)⟩
 
Neighbours ← +⊙¤[[¯1 0] [1 0] [0 1] [0 ¯1]] # Gives N4
Shuffle ← ⊏⍏[⍥⚂]⧻.
IsVisited ← ¬⊡⊂Vis
MarkAsVisited ⟜(⍜(⊡|1◌)⊂Vis)
OnlyInBounds ← ▽≡IsVisited ⊙¤,, # (finds the boundary 1's)
 
# (here, maze) -> (maze')
Walk ← |2 (
MarkAsVisited
# Mark as visited.
# Find neighbours. (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)
 
# 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⟩=°⊟≡⊢.)⟩
)
)
 
117

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.