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=='",'" then rows=19 /*No rows given? Then use the default.*/
if cols='' | cols=='",'" then cols=19 /* " cols " ? " " " " */
if seed\=='' then call random ,,seed /*use a random seed for repeatability.*/
call buildRow '┌'copies('"─┬'", cols-1)'─┐' /*buildconstruct the top edge of the maze. */
/* [↓] construct the maze's grid. */
do r=1 for rows; _=; __=; hp= "|"; hp= '|'; hj='├'
do c=1 for cols; _= _||hp'1'; __=__||hj'"'"; hj='┼'; hp='"'"
end /*c*/
call buildRow _'│' /*buildconstruct the right edge of cells.the cells*/
if r\==rows then call buildRow __'┤' /* " " " " " " maze. */
end /*r*/
 
call buildRow '└'copies('"─┴'", cols-1)'─┘' /*construct the bottom maze edge. of the maze*/
r!=random(1,rows)*2; c!=random(1,cols)*2; @.r!.c!=0 /*choose the 1stfirst cell.*/
/* [↓] traipse through the maze. */
do forever; n=hood(r!, c!); if n==0 then if \fCell() then/*number of free maze cells. leave*/
callif ?;n==0 then if \fCell() @._r._c=0 then leave /*getif theno (next)free maze directioncells toleft, go.then done*/
ro=r!;call co=c!?; r!=_r; c!= @._r._c=0 /*saveget the original(next) cellmaze coordinates.direction to go. */
?.zrro=?.zr%2r!; ?.zc co=?.zc%2c!; r!=_r; c!=_c /*getsave the maze row andoriginal cell directionscoordinates. */
rw?.zr=ro+?.zr%2; cw ?.zc=co+?.zc%2 /*calculateget the next maze row and col.cell directions.*/
@.rw.cw=ro+?.zr; cw=co+?.zc /*markcalculate the mazenext cellmaze asrow beingand visitedcol. */
@.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( . , _, " ") /*────────────── used for preserving ──*/
_=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 /*north*/
if ?==2 then ?.zr= 2 /* east*/
if ?==3 then ?.zc= 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
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*────────────────────────────────────────────────────────────────────────────*/
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>