Anonymous user
Maze generation: Difference between revisions
m
→simpler version of above: added/changed whitespace and comments, changed some indentations.
m (→{{header|REXX}}: changed/added comments and whitespace, changed indentations.) |
m (→simpler version of above: added/changed whitespace and comments, changed some indentations.) |
||
Line 5,082:
The above REXX version had a quite of bit of code to "dress up" the maze presentation,
<br>so a slimmed down version was included here.
<lang rexx>/*REXX program generates and displays a rectangular solvable maze (of any size). */
height=0; @.=0 /*default for all cells visited. */
parse arg rows cols seed . /*allow user to specify the maze size. */
if rows='' | rows==
if cols='' | cols==
if seed\=='' then call random ,,seed /*use a random seed for repeatability.*/
call buildRow '┌'copies(
/* [↓] construct the maze's grid. */
do r=1 for rows; _=; __=; hp= "|";
do c=1 for cols; _= _||hp'1'; __=__||hj
end /*c*/
call buildRow _'│' /*
if r\==rows then call buildRow __'┤' /* " " " " " " maze.
end /*r*/
call buildRow '└'copies(
r!=random(1,rows)*2;
/* [↓] traipse through the maze. */
do forever; n=hood(r!, c!)
@.rw.cw=. /*mark the maze cell as being visited. */
end /*forever*/
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 ────────────*/
_=changestr(
_=changestr('─', _, "───") /*────────────────── the aspect ratio. */
say translate(_, '│', "|\10") /*make it presentable for the screen. */
end /*r*/
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. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
?: do forever; ?.=0; ?=random(1,4); if ?==1 then ?.zc=-2
if ?==2 then ?.zr= 2
if ?==3 then ?.zc= 2
if ?==4 then ?.zr=-2
_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;
end /*c*/
end /*r*/
return 0
/*──────────────────────────────────────────────────────────────────────────────────────*/
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>
|