Maze generation: Difference between revisions
Content added Content deleted
(added Ol) |
m (→{{header|REXX}}: added/changed comments and whitespace.) |
||
Line 5,645:
<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==',' then rows= 19 /*No rows given? Then use the default.*/
if cols='' | cols==',' then cols= 19 /* " cols " ? " " " " */
if datatype(seed
call
/* [↓] construct the maze's grid. */
do r=1 for rows; _=;
do c=1 for cols; _= _ || hp'1';
end /*c*/
call
if r\==rows then call
end /*r*/▼
call
r!= random(1, rows) *2; c!= random(1, cols) *2; @.r!.c!= 0 /*choose 1st cell.*/
/* [↓] traipse through the maze. */
Line 5,669:
@.rw.cw= . /*mark the maze cell as being visited. */
end /*forever*/
/* [↓] display maze to the terminal. */
do r=1 for
do c=1 for cols*2 + 1; _= _ || @.r.c; end /*c*/
if \(r//2) then _= translate(_, '\', .) /*trans to backslash*/
Line 5,676:
end /*r*/
do #=1 for
call makeNice /*prettify cell corners and dead─ends. */
_= changestr(1 ,_, 111 ) /*──────these four ────────────────────*/
Line 5,686:
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
@: parse arg _r,_c; return @._r._c
hood: parse arg rh,ch; return @(rh+2,ch) + @(rh-2,ch) + @(rh,ch-2) + @(rh,ch+2)
/*──────────────────────────────────────────────────────────────────────────────────────*/
?: do forever; ?.= 0;
if ?==2 then ?.zr= 2
if ?==3 then ?.zc= 2
if ?==4 then ?.zr= -2
_r= r! + ?.zr;
end /*forever*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
fCell: do r=1 for rows;
do c=1 for cols;
if hood(rr,
end /*c*/ /* [↑] r! and c! are used by invoker.*/
end /*r*/; return
/*──────────────────────────────────────────────────────────────────────────────────────*/
makeNice: width= length(_); old= # - 1; new= # + 1; old_= @.old; new_= @.new
Line 5,710 ⟶ 5,707:
if right(_,2)=='.┤' then _= translate(_, "|", '┤')
do k=1 for width while #==1; z= substr(_, k, 1)
if z\=='┬' then iterate
if substr(new_, k, 1)=='\' then _= overlay("═", _, k)
end /*k*/
do k=1 for width while #==
if z\=='┴' then iterate
if substr(old_, k, 1)=='\' then _= overlay("═", _, k)
end /*k*/
do k=3 to width-2 by 2 while #//2;
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=="│" then _= overlay('|', _, k)
Line 5,743 ⟶ 5,740:
otherwise nop
end /*select*/
end
Some older REXXes don't have a '''changestr''' BIF, so one is included here ──► [[CHANGESTR.REX]].
Line 5,802 ⟶ 5,798:
<br>was included here for easier reading and understanding of the program's logic.
<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 datatype(seed
ht=0; @.=0 /*HT= # rows in grid; @.: default cell*/
call makeRow '┌'copies("─┬",
do r=1 for rows; _=; __=; hp= "|"; hj= '├'▼
do
call buildRow _'│' /*construct the right edge of the cells*/▼
if r\==rows then call makeRow __'┤' /* " " " " " " maze.*/
call
r!= random(1, rows)*2; c!= random(1, cols)*2; @.r!.c!= 0 /*choose the 1st cell.*/
/* [↓] traipse through the maze. */
do forever; n= hood(r!, c!) /*number of free maze cells. */
if n==0 then
call ?; @._r._c= 0 /*get the (next) maze direction to go. */
ro= r!; co= c!; r!= _r; c!= _c /*save the original cell coordinates. */
Line 5,827 ⟶ 5,823:
@.rw.cw=. /*mark the maze cell as being visited. */
end /*forever*/
/* [↓] display maze to the terminal. */
do r=1 for
do c=1 for cols*2 + 1; _= _ || @.r.c; end /*c*/
if \(r//2) then _= translate(_, '\',
_= changestr( 1
_= changestr( 0
_= changestr( .
_= changestr('─', _ , "───"
say translate( _ , '│' , "|\10")
end /*r*/
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
@:
hood: parse arg rh,ch; return @(rh+2,ch) + @(rh-2,ch) + @(rh,ch-2) + @(rh,ch+2)
/*──────────────────────────────────────────────────────────────────────────────────────*/
?:
if ?==2 then ?.zr= 2 /* east*/
if ?==3 then ?.zc= 2 /*south*/
if ?==4 then ?.zr= -2 /* west*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
fCell: do r=1 for rows;
do c=1 for cols;
end /*c*/
{{out|output|text= when using input: <tt> 10 10 </tt>}}
<pre>
|