Abt. Schlicher Labyrinth-Generator
======================
WindowTitle "Einfacher Labyrinth-Generator"
'Q: http://rosettacode.org/wiki/Maze_generation#BASIC
WindowStyle 24:Window 0,0-%maxx,%maxy
declare width%,height%,x%,y%,currentx%,currenty%,done%
declare oldx%,oldy%,i%,wall$,maze$[190,56]
RANDOMIZE
REM must be even:
height%=10 ' 56 'max '6 min
width% =16 '190 'max '6 min
wall$ = chr$(219) '= full block with font 1 , or "#","*",...
INIT:
::rem experimental:
::height%=height%+2:width%=width%+6
CLS rgb(180+rnd(76),180+rnd(76),180+rnd(76))
font 1
REM make array and fill
whileloop 0,width%:x%=&Loop
whileloop 0,height%:y%=&Loop
maze$[x%,y%]=wall$
endwhile
endwhile
REM initial start location
currentx%=INT(RND()*(width%-1))
currenty%=INT(RND()*(height%-1))
REM value must be odd
casenot currentx% MOD 2:inc currentx%
casenot currenty% MOD 2:inc currenty%
maze$[currentx%,currenty%]=" "
::color 0,15:Draw_Maze
REM generate maze
done%=0
WHILENOT done%
whileloop 0,99:i%=&Loop
oldx%=currentx%
oldy%=currenty%
REM move in random direction
SELECT RND(4)
CASEof 0
case (currentx%+2)<width%:currentx%=currentx%+2
CASEof 1
case (currenty%+2)<height%:currenty%=currenty%+2
CASEof 2
case (currentx%-2)>0:currentx%=currentx%-2
CASEof 3
case (currenty%-2)>0:currenty%=currenty%-2
ENDSELECT
REM if cell is unvisited then connect it
IF maze$[currentx%,currenty%]=wall$
maze$[currentx%,currenty%]=" "
maze$[(currentx%+oldx%)\2,(currenty%+oldy%)\2]=" "
::locate currenty%+1,currentx%+1:print " ";
::locate (currenty%+oldy%+2)\2,(currentx%+oldx%+2)\2:print " ";
ENDIF
endwhile
REM check if all cells are visited
done% = 1
whileloop 1,width%-1,2:x%=&Loop
whileloop 1,height%-1,2:y%=&Loop
case maze$[x%,y%]=wall$:done%=0
endwhile
endwhile
endwhile
rem Mache Eingang und Ausgang:
maze$[0,1+2*(rnd(height%-1)\2)]=" "
maze$[width%,1+2*(rnd(height%-1)\2)]=" "
::color rnd(15),15:Draw_Maze
waitinput
Goto "INIT"
proc Draw_Maze
locate 1,1
whileloop 0,height%:y%=&Loop
whileloop 0,width%:x%=&Loop
PRINT maze$[x%,y%];
endwhile
PRINT
endwhile
endproc
PROGEND
Alles anzeigen