Maze generation: Difference between revisions

→‎version 1: changed order of subroutines, added whitespace and comments. -- ~~~~
(→‎{{header|Julia}}: better code for the printer.)
(→‎version 1: changed order of subroutines, added whitespace and comments. -- ~~~~)
Line 3,708:
=={{header|REXX}}==
===version 1===
In order to preserve the aspect ratio (for most display terminals), several   '''changestr'''   instructions and
<br>some other instructions were added to increase the horizontal dimension (columns).
<lang rexx>/*REXX program to generate generates and displaydisplays a (rectangular) solveable maze. */
height=0; @.=0 /*default for all cells visited.*/
parse arg rows cols seed . /*allow user to specify maze size*/
Line 3,716:
if cols='' | cols==',' then cols=19 /*No cols given? Use the default*/
if seed\=='' then call random ,,seed /*use a seed for repeatability. */
call buildRow '┌'copies('─┬',cols-1)'─┐' /*build the top edge of maze.*/
/*(below) build the maze's grid & pop.*/
do r=1 for rows; _=; __=; hp= '|'; hj='├'
do c=1 for cols; _= _||hp'1'; __=__||hj'─'; hj='┼'; hp='│'
end /*c*/
call buildRow _'│' /*build right edge of cells.*/
if r\==rows then call buildRow __'┤' /* " " " " maze.*/
end /*r*/
 
call buildRow '└'copies('─┴',cols-1)'─┘' /*build the bottom maze edge.*/
r!=random(1,rows)*2; c!=random(1,cols)*2; @.r!.c!=0 /*choose 1st cell*/
end /* [↓] traipse through the maze.*/
 
do forever; n=hood(r!,c!); if n==0 then if \fcell() then leave
call ?; @._r._c=0 /*get the (next) direction to go.*/
ro=r!; co=c!; r!=_r; c!=_c /*save original cell coordinates.*/
?.zr=?.zr%2; ?.zc=?.zc%2 /*get the row and cell directions*/
rw=ro+?.zr; cw=co+?.zc /*calculate the next row and col.*/
@.rw.cw='·' /*mark the cell as being visited.*/
@.rw.cw='·'
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(_,'-','fa'x) /*translate to minus*/
_=translate(_,'\','fa'x) /*trans to backslash*/
Line 3,747:
end /*r*/
exit /*stick a fork in it, we're done.*/
/*──────────────────────────────────FCELL subroutine────────────────────*/
fcell: do r=1 for rows; r2=r+r
do c=1 for cols; c2=c+c
if hood(r2,c2)==1 then do; r!=r2; c!=c2; @.r!.c!=0; return 1
end
end /*c*/
end /*r*/
return 0
/*──────────────────────────────────@ subroutine────────────────────────*/
@: parse arg _r,_c; return @._r._c /*a fast way to reference a cell.*/
/*──────────────────────────────────? subroutine────────────────────────*/
?: do forever; ?.=0; ?=random(1,4)
Line 3,765 ⟶ 3,757:
_r=r!+?.zr; _c=c!+?.zc; if @._r._c==1 then return
end /*forever*/
/*──────────────────────────────────HOOD subroutine─────────────────────*/
hood: parse arg rh,ch; return @(rh+2,ch)+@(rh-2,ch)+@(rh,ch-2)+@(rh,ch+2)
/*──────────────────────────────────BUILDROW subroutine─────────────────*/
buildRow: parse arg z; height=height+1; width=length(z)
do c=1 for width; @.height.c=substr(z,c,1); end; return</lang>
/*──────────────────────────────────FCELL subroutine────────────────────*/
fcell: do r=1 for rows; r2=r+r
do c=1 for cols; c2=c+c
if hood(r2,c2)==1 then do; r!=r2; c!=c2; @.r!.c!=0; return 1;end
end /*c*/
end /*r*/
return 0
/*──────────────────────────────────HOOD subroutine─────────────────────*/
hood: parse arg rh,ch; return @(rh+2,ch)+@(rh-2,ch)+@(rh,ch-2)+@(rh,ch+2)</lang>
Some older REXXes don't have a '''changestr''' bif, so one is included here ──► [[CHANGESTR.REX]].
<br><br>
Line 3,796 ⟶ 3,795:
└───┴───┴───┴───┴───┴───┴───┴───┴───┴───┘
</pre>
 
===version 2===
<lang rexx>/* REXX ***************************************************************