RetroCoders Community

FreeBasic Programming => FreeBasic Tips & Tricks => Topic started by: mysoft on Sep 28, 2023, 03:50 PM

Title: Recursive Random Maze
Post by: mysoft on Sep 28, 2023, 03:50 PM
another version of a random maze, this one made with a recursive function.

(https://i.imgur.com/jTqK5qo.png)

const MapWid=80,MapHei=25
'static shared as long MapWid,MapHei
'MapWid = loword(width) : MapHei = hiword(width)
redim shared as byte Mapa(MapHei,MapWid)

screenres MapWid*8,MapHei*14
width MapWid,MapHei
locate ,,0

sub LabirintoRecursivo( iLin as long , iCol as long ) 
   
  static as long iDirTab(3)={-1,1, 0,0} 
  dim as long iDir=int(rnd*4)
  for iDir = iDir to iDir+3
   
    static as long iDirLin,iDirCol,iNewLin,iNewCol
    iDirLin = iDirTab(iDir and 3) : iDirCol = iDirTab((iDir+2) and 3)
   
    iNewLin = iLin+iDirLin : iNewCol = iCol+iDirCol   
    if iNewLin < 2 or iNewLin >= ((MapHei-1) or 1) then continue for
    if iNewCol < 2 or iNewCol >= ((MapWid-1) or 1) then continue for
    if Mapa(iNewLin,iNewCol)<>0 then continue for
    if Mapa(iNewLin+iDirLin,iNewCol+iDirCol)<>0 then continue for
   
    Mapa(iNewLin,iNewCol)=1 : locate iNewLin,iNewCol : print chr(177);                   
    iNewLin += iDirLin : iNewCol += iDirCol
    Mapa(iNewLin,iNewCol)=1 : locate iNewLin,iNewCol : print chr(177);                   
   
    sleep 10 : LabirintoRecursivo(iNewLin,iNewCol)         
       
  next iDir 
 
end sub

do 
  color , 0 : cls : color 1+rnd*14,1+rnd*14
  clear Mapa(0,0),0,(MapWid+1)*(MapHei+1)
  LabirintoRecursivo(2,2) : sleep
loop
Title: Re: Recursive Random Maze
Post by: johnno56 on Sep 29, 2023, 12:02 AM
Nicely done! I even liked the change of colours when a key is pressed...  Cool...