How to resolve the algorithm Maze generation step by step in the Red programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Maze generation step by step in the Red programming language
Table of Contents
Problem Statement
Generate and show a maze, using the simple Depth-first search algorithm.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Maze generation step by step in the Red programming language
Source code in the red programming language
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? p: pos - 1x0] [keep p]
if all [pos/x < (size/x - 1) not visited? p: pos + 1x0] [keep p]
if all [pos/y > 0 not visited? p: pos - 0x1] [keep p]
if all [pos/y < (size/y - 1) not visited? p: pos + 0x1] [keep p]
]
pick nnbs random length? nnbs
]
expand: function [pos][
insert visited pos
either npos: newneighbour pos [
insert exploring npos
do select [
0x-1 [o: offsetof npos walls/:o/y: 0]
1x0 [o: offsetof pos walls/:o/x: 0]
0x1 [o: offsetof pos walls/:o/y: 0]
-1x0 [o: offsetof npos walls/:o/x: 0]
] npos - pos
][
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 rejoin [pick " _" 1 + p/y pick " |" 1 + p/x]
]
print ""
]
You may also check:How to resolve the algorithm McNuggets problem step by step in the Ring programming language
You may also check:How to resolve the algorithm Leap year step by step in the min programming language
You may also check:How to resolve the algorithm Variadic function step by step in the Clojure programming language
You may also check:How to resolve the algorithm Sum and product of an array step by step in the PowerShell programming language
You may also check:How to resolve the algorithm Write language name in 3D ASCII step by step in the Tcl programming language