Anonymous user
Maze generation: Difference between revisions
m
→{{header|REXX}}: changed/added comments and whitespace, changed indentations.
m (→{{header|REXX}}: changed/added comments and whitespace, changed indentations.) |
|||
Line 4,923:
In order to preserve the aspect ratio (for most display terminals), several '''changestr''' invocations and
<br>some other instructions were added to increase the horizontal dimension (cell size).
<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 _'│' /*construct the right edge of the cells
if r\==rows then call buildRow __'┤' /* " " " " " " maze.
end /*r*/
call buildRow '└'copies(
r!=random(1,rows)*2; c!=random(1,cols)*2; @.r!.c!=0
/* [↓] traipse through the maze. */
do forever; n=hood(r!,c!)
if n==0 then if \fCell() then leave
@.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*/
@.r=_ /*save the row in @.*/
end /*r*/
do #=1 for height; _=@.# /*display the maze to the terminal. */
call makeNice /*make some cell corners look prettier.*/
_=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 /*#*/
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)▼
/*──────────────────────────────────────────────────────────────────────────────────────*/
?: do forever; ?.=0;
if ?==
if ?==
_r=r!+?.zr; _c=c!+?.zc; if @._r._c==1 then return
end /*forever*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
buildRow:
do c=1 for width;
/*──────────────────────────────────────────────────────────────────────────────────────*/
fCell: do
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.*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
▲hood: parse arg rh,ch; return @(rh+2,ch) + @(rh-2,ch) + @(rh,ch-2) + @(rh,ch+2)
makeNice: width=length(_); old=#-1; new=#+1; old_=@.old; new_=@.new▼
▲makeNice: width=length(_); old=#-1; new=#+1; old_=@.old; new_=@.new
if
/* [↓] handle the top row of the grid.*/
▲ if right(_,2)=='.┤' then _=translate(_, '|', "┤")
do k=1 for width while #==1;
do k=1 for width while #==height; z=substr(_,k,1) /*maze bot row.*/
if z\=='┴' then iterate
if substr(old_, k, 1)=='\' then _=overlay(
end /*k*/
/* [↓] handle the mid
do k=3 to width-2 by 2 while #//2; z=substr(_,k,1) /*maze mid rows*/
if z\=='┼' then iterate
le=substr(_,k-1,1)
ri=substr(_,k+1,1)
up=substr(old_,k,1)
dw=substr(new_,k,1)
select
when le== . & ri== . & up=='│' & dw==
when le=='~' & ri==
when le=='~' & ri==
when le=='~' & ri==
when le=='~' & ri== . & up=='\' & dw==
when le== . & ri==
when le== . & ri== . & up=='│' & dw==
when le== . & ri== . & up=='\' & dw==
when le== . & ri==
when le== . & ri==
when le=='~' & ri== . & up=='\' & dw==
when le=='~' & ri== . & up=='│' & dw==
when le=='~' & ri== . & up=='│' & dw==
when le== . & ri==
otherwise nop
end /*select*/
end /*k*/
return</lang>
Some older REXXes don't have a '''changestr''' BIF, so one is included here ──► [[CHANGESTR.REX]].
<br><br>
|