Maze generation: Difference between revisions

m
(Added POV-Ray solution)
 
(5 intermediate revisions by 3 users not shown)
Line 25:
V hor = [[‘+--’] * w [+] [String(‘+’)]] * (h + 1)
 
F walk(Int x, Int y) -> NVoid
@vis[y][x] = 1
V d = [(x - 1, y), (x, y + 1), (x + 1, y), (x, y - 1)]
Line 7,279:
 
=={{header|POV-Ray}}==
This POV-Ray solution uses an iterative rather than recursive approach because POV-Ray has a small stack for recursion (about 100 levels) and so the program would crash on even quite small mazes. With the iterative approach it works on very large mazes.
<syntaxhighlight lang="pov">
#version 3.7;
Line 7,289 ⟶ 7,290:
#declare Cols = 17;
 
#declare Seed = seed(2); // A seed willproduces alwaysa produce the samefixed sequence of pseudorandom numbers
 
#declare Wall = prism {
0, 0.8, 7,
<0, -0.5>, <0.05, -0.45>, <0.05, 0.45>, <0, 0.5>, <-0.05, 0.45>, <-0.05, -0.45>, <0, -0.5>
<-0.05, 0.45>, <-0.05, -0.45>, <0, -0.5>
texture {
pigment {
Line 7,348 ⟶ 7,350:
#local Col = floor(rand(Seed)*(Cols-1) + 0.5); // Random start column
#local Top = -1;
#local Max = -1;
Push(Stack, Top, Row, Col, Row, Col)
 
Line 7,377 ⟶ 7,378:
Push(Stack, Top, Row, Col, Row, Col-1)
#end
#end
 
#if (Top > Max)
#declare Max = Top;
#end
 
Line 7,441 ⟶ 7,438:
 
light_source {
<-0.1*Cols, Rows, 0.5*Rows>, colour rgb <1, 1, 1>
area_light
x, y, 3, 3
Line 7,467 ⟶ 7,464:
</syntaxhighlight>
{{out}}
[[File:Povray maze solution 640px.png|640px|frame|none|alt=POV-Ray maze with red brick walls|POV-Ray solution with 15 rows and 17 columns]]
 
=={{header|Processing}}==
Line 10,065 ⟶ 10,062:
 
=={{header|Uiua}}==
{{works with|Uiua|0.1012.30-dev.1}}
{{trans|Python}}
Inspired by the Python algorithm, so will produce identicalvery similar output.
 
NOTE: this code is extended in [https://rosettacode.org/wiki/Maze_solving#Uiua the Maze Solving page] so please update
that page if you change this code.
<syntaxhighlight lang="Uiua">
# Build and solve a maze.
H ← 8
# Experimental!
W ← 18
VerW010
HorH16
Vis ← 2
 
GetWall ← -:⊡1:÷2/-.
BreakNS ← ⊂Ver⊂⊃(/↥≡⊡0|⊡0_1)
BreakEW ← ⊂Hor⊂⊃(⊡0_0|/↥≡⊡1)
# ([here, next], maze) -> (maze')
BreakWall ← ⍜⊡(0◌)⟨BreakNS|BreakEW⟩=°⊟≡⊢.⋅@ )GetWall
 
NeighboursNfour ← +⊙¤[12_0 0]2_0 [1 0] [0 1] [00_2 0_¯1]2] # Gives N4
Shuffle ← ⊏⍏[⍥⚂]⧻.
# (pos maze) -> T if it's already a star.
IsVisited ← ¬⊡⊂Vis≠@.⊡
MarkAsVisited ← ⟜(⍜(⊡|1◌)⊂Vis⍜⊡⋅@.)
OnlyInBounds ← ▽≡IsVisited ⊙¤,, # (finds the boundary 1's)
# (pos) -> (bool)
 
InBounds ← ▽⊸≡(↧⊃(/↧≥1_1|/↧< +1 × 2 H_W))
# (here, maze) -> (maze')
Walk ← |2 (
MarkAsVisited
# (here, maze) -> ([[here, next] x(up to)4], maze)
≡⊟¤⟜(Shuffle OnlyInBoundsInBounds NeighboursNfour)
 
# Update maze for each in turn. For each, if it
# still isn't visited, break the wall, recurse into it.
∧(⟨◌⨬(◌|Walk ⊡1 ⟜BreakWall⟩IsVisited◌Walk⊡1⟜BreakWall)IsVisited◌°⊟,,)
)
 
# Generate a maze.
⊂↯H⊂↯W0 1↯+1W1 # vis (added 1's help bounds checks)
HMaze8(
⊂↯H⊂↯W1 1↯+1W 0 # ver
↘¯1☇1↯(+1H)⊟⊂/⊂↯W"+-" @+⊂/⊂↯W"| " @| # Build a filled maze.
↯+1H⊂↯W1 0 # hor
Walk 1_1 # Walk around breaking walls.
⊂⊟
)
# Stack: ([hor, ver, vis])
Maze
Walk [0 0]
 
PP! ← ≡/⊂∵^! # Pretty print using switch.
≡(&p$"_\n_")⊃(PP!⟨"+ "|"+--"⟩⊡Ver|PP!⟨" "|"| "⟩⊡Hor)
 
</syntaxhighlight>
{{out}}
<pre>
╭─
╷ "+-+-+-+-+-+-+-+-+-+-+"
"|.|.|. .|. . . . . .|"
"+ + + + + +-+-+ +-+ +"
"|.|. .|. . .|. .|. .|"
"+ + +-+-+-+-+ +-+ +-+"
"|.|.|. . . .|.|. . .|"
"+ + + +-+-+ + +-+-+ +"
"|.|.|. .|. .|. .|. .|"
"+ +-+-+ + +-+-+ + +-+"
"|. . .|.|. . . .|. .|"
"+-+-+ + +-+-+-+-+-+ +"
"|. . . .|. . . . . .|"
"+-+-+-+-+-+-+-+-+-+-+"
</pre>
 
=={{header|Wren}}==
159

edits