Maze generation: Difference between revisions

Content added Content deleted
m (→‎simpler version of above: used a smaller font size for the output section showing the maze.)
Line 6,714: Line 6,714:
ok
ok
</pre>
</pre>

=={{header|Red}}==
<lang Red>Red ["Maze generation"]

size: as-pair to-integer ask "Maze width: " to-integer ask "Maze height: "
random/seed now/time
offsetof: function [pos] [pos/y * size/x + pos/x + 1]
visited?: function [pos] [find visited pos]

newneighbour: function [pos][
nnbs: collect [
if all [pos/x > 0 not visited? p1: pos - 1x0] [keep p1]
if all [pos/x < (size/x - 1) not visited? p2: pos + 1x0] [keep p2]
if all [pos/y > 0 not visited? p3: pos - 0x1] [keep p3]
if all [pos/y < (size/y - 1) not visited? p4: pos + 0x1] [keep p4]
]
pick nnbs random length? nnbs
]
expand: function [pos][
insert visited pos
either npos: newneighbour pos [
insert exploring npos
switch npos - pos [
case 0x-1 [o: offsetof npos walls/:o/y: 0]
case 1x0 [o: offsetof pos walls/:o/x: 0]
case 0x1 [o: offsetof pos walls/:o/y: 0]
case -1x0 [o: offsetof npos walls/:o/x: 0]
]
][
remove exploring
]
]
visited: []
walls: append/dup [] 1x1 size/x * size/y
exploring: reduce [random size - 1x1]

until [
expand exploring/1
empty? exploring
]

print append/dup "" " _" size/x
repeat j size/y [
prin "|"
repeat i size/x [
p: pick walls (j - 1 * size/x + i)
prin either 1 = p/y ["_"][" "]
prin either 1 = p/x ["|"][" "]
]
print ""
]
halt</lang>
{{out}}
<pre>Maze width: 15
Maze height: 15
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _
| _ _ | _ _ _ _| _ _ |
|_ _ |_ _| | _ _ |_ | |_ _|
| | |_ |_ _ _ _| | _|_ _ |
| |_|_ _ |_ | | | _ _| _|
| _ _|_ _| |_ _| | _ _| |
| | |_ _ _ _| | _ _| | _ _|
|_ _ _ | | |_| | | _|_ _ |
|_ _ | | | |_ _ _| | | _ | |
| _ _| | |_ _ _ |_ _| | | |
| _ | |_ | |_ |_ _ _| | |
| | |_ _|_ _ _ |_ |_ _| _| |
| | _ |_ _| | |_ | |
| |_|_ |_ |_| _|_|_|_ _ _| |
|_ _ | | |_ |_ _ _ _ _ | |
|_ _ _ _|_ _ _ _ _ _ _ _|_ _ _|
(halted)</pre>


=={{header|REXX}}==
=={{header|REXX}}==