RosettaCodeData/Task/Maze-generation/M2000-Interpreter/maze-generation-2.m2000
2023-07-01 13:44:08 -04:00

70 lines
2.2 KiB
Text

Module Maze2 {
\\ depth-first search
Profiler
Form 80,50
let w=60, h=40
Double
\\center proportional text double size
Report 2, Format$("Maze {0}x{1}",w,h)
Normal
Refresh
Set Fast !
Dim maze$(1 to w+1, 1 to h+1)="#"
Include=Lambda w,h (a,b) ->a>=1 and a<=w and b>=1 and b<=h
Flush ' empty stack
if random(1,2)=1 then {
entry=(if(random(1,2)=1->2, w),Random(1, h/2)*2)
} else {
entry=(random(1,w/2)*2,If(Random(1,2)=1->2,h))
}
maze$(entry#val(0), entry#val(1))=" "
forchoose=(,)
Push Entry
do {
do {
NewForChoose(!entry)
status=len(forchoose)
if status>0 then {
status--
forchoose=forchoose#val(random(0,status))
Push forchoose
OpenDoor(!Entry, !forchoose)
Rem : ShowMaze()
} else exit
entry=forchoose
} Always
if empty then exit
Read entry
} Always
ShowMaze()
Print timecount/1000
Sub NewForChoose(x,y)
Local x1=x-2, x2=x+2, y1=y-2, y2=y+2, arr=(,)
Stack New {
if include(x1,y) then if Maze$(x1,y)<>" " Then push (x1, y)
if include(x2,y) then if Maze$(x2,y)<>" " Then push (x2, y)
if include(x,y1) then if Maze$(x,y1)<>" " Then push (x, y1)
if include(x,y2) then if Maze$(x,y2)<>" " Then push (x, y2)
forchoose= Array([])
}
End Sub
Sub OpenDoor(x1,y1, x2,y2)
Local i
if x1=x2 then {
y1+=y2<=>y1
for i=y1 to y2 {maze$(x1, i)=" " }
} Else {
x1+=x2<=>x1
for i=x1 to x2 {maze$(i, y1)=" "}
}
End Sub
Sub ShowMaze()
Refresh 5000
cls ,4 ' split screen - preserve lines form 0 to 3
Local i, j
For j=1 to h+1 { Print @(10) : for i=1 to w+1 {Print maze$(i,j);}:Print}
Print
Refresh 100
End Sub
}
Maze2