Anonymous user
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=
if cols='' | cols=="," then cols=
if seed\=='' then call random ,,seed
call buildRow '┌'copies("─┬", cols-1)'─┐'
/* [↓] construct the maze's grid. */
do r=1 for rows; _=; __=; hp= "|"; hj= '├'
do c=1 for cols; _= _ || hp'1'; __= __ || hj"─";
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!=
/* [↓] traipse through the maze. */
do forever; n= hood(r!, c!)
if n==0 then if \fCell() then leave /*if no free maze cells left, then done*/
call ?; @._r._c=
ro= r!; co= c!; r!= _r;
?.zr= ?.zr % 2;
rw= ro + ?.zr;
@.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(_, '\', .)
_=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;
/*──────────────────────────────────────────────────────────────────────────────────────*/
?:
if ?==2 then ?.zr=
if ?==3 then ?.zc=
if ?==4 then ?.zr= -2
/*──────────────────────────────────────────────────────────────────────────────────────*/
buildRow: parse arg z; height= height + 1; width= length(z)
/*──────────────────────────────────────────────────────────────────────────────────────*/
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>
▲hood: parse arg rh,ch; return @(rh+2,ch) + @(rh-2,ch) + @(rh,ch-2) + @(rh,ch+2)</lang>
▲'''output''' when using the input: <tt> 10 10 </tt>
<pre>
┌───┬───┬───┬───┬───┬───┬───┬───┬───┬───┐
|