Maze generation: Difference between revisions

m
→‎simpler version of above: added/changed comments and whitespace, used a template for the output section.
m (→‎simpler version of above: added wording to the REXX section header.)
m (→‎simpler version of above: added/changed comments and whitespace, used a template for the output section.)
Line 5,726:
height=0; @.=0 /*default for all cells visited. */
parse arg rows cols seed . /*allow user to specify the maze size. */
if rows='' | rows=="," then rows=19 19 /*No rows given? Then use the default.*/
if cols='' | cols=="," then cols=19 19 /* " cols " ? " " " " */
if seed\=='' then call random ,,seed /*use a random seed for repeatability.*/
call buildRow '┌'copies("─┬", cols-1)'─┐' /*construct the top edge of the maze.*/
/* [↓] construct the maze's grid. */
do r=1 for rows; _=; __=; hp= "|"; hj= '├'
do c=1 for cols; _= _ || hp'1'; __= __ || hj"─"; hj= '┼'; hp= "│"
end /*c*/
call buildRow _'│' /*construct the right edge of the cells*/
Line 5,739:
 
call buildRow '└'copies("─┴", cols-1)'─┘' /*construct the bottom edge of the maze*/
r!= random(1, rows)*2; c!= random(1, cols)*2; @.r!.c!=0 0 /*choose the first1st cell.*/
/* [↓] traipse through the maze. */
do forever; n= hood(r!, c!) /*number of free maze cells. */
if n==0 then if \fCell() then leave /*if no free maze cells left, then done*/
call ?; @._r._c=0 0 /*get the (next) maze direction to go. */
ro= r!; co= c!; r!= _r; c!=_c _c /*save the original cell coordinates. */
?.zr= ?.zr % 2; ?.zc= ?.zc %2 2 /*get the maze row and cell directions.*/
rw= ro + ?.zr; cw= co + ?.zc /*calculate the next maze row and col. */
@.rw.cw=. /*mark the maze cell as being visited. */
end /*forever*/
Line 5,752:
do r=1 for height; _= /*display the maze. */
do c=1 for cols*2 + 1; _=_ || @.r.c; end /*c*/
if \(r//2) then _= translate(_, '\', .) /*trans to backslash*/
_=changestr(1 , _, 111) /*──────these four ────────────────────*/
_=changestr(0 , _, 000) /*───────── statements are ────────────*/
Line 5,761:
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
@: parse arg _r,_c; return @._r._c /*a fast way to reference a maze cell. */
hood: parse arg rh,ch; return @(rh+2,ch) + @(rh-2,ch) + @(rh,ch-2) + @(rh,ch+2)</lang>
/*──────────────────────────────────────────────────────────────────────────────────────*/
?: do forever; ?.= 0; ?= random(1, 4); if ?==1 then ?.zc= -2 /*north*/
if ?==2 then ?.zr= 2 2 /* east*/
if ?==3 then ?.zc= 2 2 /*south*/
if ?==4 then ?.zr= -2 /* west*/
_r= r! + ?.zr; _c= c! + ?.zc; if @._r._c==1 then return
end /*forever*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
buildRow: parse arg z; height= height + 1; width= length(z)
do c=1 for width; @.height.c= substr(z, c, 1); end; return
/*──────────────────────────────────────────────────────────────────────────────────────*/
fCell: do r=1 for rows; rr= r + r
do c=1 for cols; cc= c + c
if hood(rr,cc)==1 then do; r!=rr; c!=cc; @.r!.c!=0; return 1; end
end /*c*/
end /*r*/ /* [↑] r! and c! are used by invoker.*/
return 0</lang>
'''{{out|output''' |text=&nbsp; when using the input: &nbsp; &nbsp; <tt> 10 &nbsp;10 </tt>}}
/*──────────────────────────────────────────────────────────────────────────────────────*/
hood: parse arg rh,ch; return @(rh+2,ch) + @(rh-2,ch) + @(rh,ch-2) + @(rh,ch+2)</lang>
'''output''' &nbsp; when using the input: &nbsp; <tt> 10 &nbsp;10 </tt>
<pre>
┌───┬───┬───┬───┬───┬───┬───┬───┬───┬───┐