Maze generation: Difference between revisions

m
→‎{{header|REXX}}: added/changed comments and whitespace.
(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\==, 'W') then call random ,,seed /*use a random seed for repeatability. */
heightht=0; @.=0 @.=0 /*default for all cells/*HT= # visited.rows in grid; @.: default cell*/
call buildRowmakeRow '┌'copies('~┬', cols - 1)'~┐' /*construct 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 buildRowmakeRow _'│' /*construct the right edge of the cells. */
if r\==rows then call buildRowmakeRow __'┤' /* " " " " " " maze. " maze.*/
height= 0; @.=end 0 /*r*/ /*default for[↑] all construct cellsthe maze's visitedgrid. */
end /*r*/
 
call buildRowmakeRow '└'copies("~┴", cols - 1)'~┘' /*construct the bottom edge of the maze*/
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 heightht; _= /*display the maze. */_=
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 heightht; _= @.# /*display the maze to the terminal. */
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 /*a fast way to reference a maze cell. */
hoodmakeRow: parse arg rh,chz; ht= return @(rhht+2,1; ch) do +c=1 @for length(rh-2, chz) + ; @.ht.c=substr(rhz, ch-2c,1); +end; @(rh, ch+2)return
hood: parse arg rh,ch; return @(rh+2,ch) + @(rh-2,ch) + @(rh,ch-2) + @(rh,ch+2)
/*──────────────────────────────────────────────────────────────────────────────────────*/
?: 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*/ /* [↑] r! and c! are used by invoker.*/
end /*r*/; return /* [↑] r! and c! are used by invoker.*/0
return 0
/*──────────────────────────────────────────────────────────────────────────────────────*/
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) /*maze top row.*/
if z\=='┬' then iterate
if substr(new_, k, 1)=='\' then _= overlay("═", _, k)
end /*k*/
 
do k=1 for width while #==heightht; z= substr(_, k, 1) /*maze bot row.*/
if z\=='┴' then iterate
if substr(old_, k, 1)=='\' then _= overlay("═", _, k)
end /*k*/
 
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=="│" then _= overlay('|', _, k)
Line 5,743 ⟶ 5,740:
otherwise nop
end /*select*/
end /*k*/; return</lang>
return</lang>
Some older REXXes don't have a &nbsp; '''changestr''' &nbsp; BIF, so one is included here &nbsp; ──► &nbsp; [[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\==, 'W') then call random ,,seed /*use a random seed for repeatability.*/
ht=0; @.=0 /*HT= # rows in grid; @.: default cell*/
call buildRow '┌'copies("─┬", cols-1)'─┐' /*construct the top edge of the maze.*/
call makeRow '┌'copies("─┬", cols-1)'─┐' /* [↓] construct the maze's grid.top edge of the maze.*/
 
do r=1 for rows; _=; __=; hp= "|"; hj= '├'
do c r=1 for colsrows; _=; _ || hp'1' __=; __hp= __ "|| hj"─"; hj= '┼'; hphj= "│"'├'
do r do c=1 for rowscols; _=; _ || __=hp'1'; __= __ || hp= hj"|"; hj= '┼'; hjhp= '├'"│"
end /*c*/
end end /*rc*/
call buildRow _'│' /*construct the right edge of the cells*/
if r\==rows then call buildRow __'┤' /* " call makeRow _'│' " " /*construct the right " " edge "of the maze.cells*/
if r\==rows then call makeRow __'┤' /* " " " " " " maze.*/
end /*r*/
end /*r*/ call buildRow _'│' /* [↑] construct the rightmaze's grid. edge of the cells*/
 
call buildRowmakeRow '└'copies("─┴", cols-1)'─┘' /*construct the bottom edge of the maze*/
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 if \fCell() then leave /*if no free maze cells left, then done*/
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 heightht; _= /*display the maze. */_=
do c=1 for cols*2 + 1; _= _ || @.r.c; end /*c*/
if \(r//2) then _= translate(_, '\', .) /*translate a period to a /*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. */
hoodmakeRow: parse arg rh,chz; ht= ht+1; return do @(rh+2,ch)c=1 +for @length(rh-2,chz) +; @.ht.c=substr(rhz,c,ch-21); +end; @(rh,ch+2)return
hood: parse arg rh,ch; return @(rh+2,ch) + @(rh-2,ch) + @(rh,ch-2) + @(rh,ch+2)
/*──────────────────────────────────────────────────────────────────────────────────────*/
?: 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; do r=1 for rows; rr= r + r
do c=1 for cols; 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! and c! are used by invoker.*/
end /*r*/; return /* [↑] r! and c! are used by invoker.*0</lang>
return 0</lang>
{{out|output|text=&nbsp; when using input: &nbsp; &nbsp; <tt> 10 &nbsp;10 </tt>}}
<pre>