set display mode 1280, 1024, 32
`Declare some types
type mazeCell
   N
   S
   E
   W
endtype
`define a coord
type Coord
   x as integer
   y as integer
endtype
 
`How many cells do we want?
#constant M_WIDTH 16
#constant M_HEIGHT 16
 
`What is the size of the "alley"
global MAP_SCALE#
MAP_SCALE# = 24.0
 
`Create a maze array of cells
dim Maze(M_WIDTH, M_HEIGHT) as mazeCell
 
`Generate the maze
GenerateMaze()
 
 
paste image 1, 0, 0
wait key
end
 
 
 
 
 
`*******************************
`**                           **
`**      MAZE GENERATION      **
`**                           **
`*******************************
function GenerateMaze()
   `Create a maze full of walls (like a grid
   for i = 1 to M_WIDTH
      for j = 1 to M_HEIGHT
         Maze(i,j).N = 1
         Maze(i,j).S = 1
         Maze(i,j).E = 1
         Maze(i,j).W = 1
      next j
   next i
 
   `Create a stack of walls to be knocked down to create a path
   dim CellStack() as Coord : empty array CellStack() : array index to stack CellStack()
   TotalCells = M_WIDTH * M_HEIGHT
   `Visited cells = 1 as we're about to pick our starting cell
   VisitedCells = 1
   currentCell as Coord
   currentCell.x = rnd(M_WIDTH-2)+1
   currentCell.y = rnd(M_HEIGHT-2)+1
   Dim Available(4)
 
   `Loop over every single cell until we've visited them all
   while VisitedCells < TotalCells
      `Clear available walls
      Available(0) = 0
      Available(1) = 0
      Available(2) = 0
      Available(3) = 0
 
 
      ` 0 = S
      ` 1 = N
      ` 2 = W
      ` 3 = E
      `Count how many walls are available and work out which ones are availble.
      `A cell in question IS available if it has all 4 walls in tact... This stops a cell becoming 2-sided...
      found = 0
      if currentCell.y > 1
         tX = currentCell.x
         tY = currentCell.y-1
         if Maze(tx,ty).N = 1 AND Maze(tx,ty).S = 1 AND Maze(tx,ty).E = 1 AND Maze(tx,ty).W = 1
            inc found
            Available(0) = 1
         endif
      endif
      if currentCell.y < M_HEIGHT
         tX = currentCell.x
         tY = currentCell.y+1
         if Maze(tx,ty).N = 1 AND Maze(tx,ty).S = 1 AND Maze(tx,ty).E = 1 AND Maze(tx,ty).W = 1
            inc found
            Available(1) = 1
         endif
      endif
      if currentCell.x > 1
         tX = currentCell.x-1
         tY = currentCell.y
         if Maze(tx,ty).N = 1 AND Maze(tx,ty).S = 1 AND Maze(tx,ty).E = 1 AND Maze(tx,ty).W = 1
            inc found
            Available(2) = 1
         endif
      endif
      if currentCell.x < M_WIDTH
         tX = currentCell.x+1
         tY = currentCell.y
         if Maze(tx,ty).N = 1 AND Maze(tx,ty).S = 1 AND Maze(tx,ty).E = 1 AND Maze(tx,ty).W = 1
            inc found
            Available(3) = 1
         endif
      endif
 
      `if we foudn a wall to knock down, pick on...
      if found > 0
         `Pick a random wall - if its not available (worked out above) then pick another
         choice = rnd(3)
         while Available(choice) = 0
            choice = rnd(3)
         endwhile
 
         `Depending on selection, knock down that wall and att the current cell to the stack for backtrackign purposes
         select choice
            `North
            case 0
               Maze(currentCell.x, currentCell.y).N = 0
               Maze(currentCell.x, currentCell.y-1).S = 0
               add to stack CellStack()
               CellStack() = currentCell
               dec currentCell.y
            endcase
 
            `South
            case 1
               Maze(currentCell.x, currentCell.y).S = 0
               Maze(currentCell.x, currentCell.y+1).N = 0
               add to stack CellStack()
               CellStack() = currentCell
               inc currentCell.y
            endcase
 
            `West
            case 2
               Maze(currentCell.x, currentCell.y).W = 0
               Maze(currentCell.x-1, currentCell.y).E = 0
               add to stack CellStack()
               CellStack() = currentCell
               dec currentCell.x
            endcase
 
            `East
            case 3
               Maze(currentCell.x, currentCell.y).E = 0
               Maze(currentCell.x+1, currentCell.y).W = 0
               add to stack CellStack()
               CellStack() = currentCell
               inc currentCell.x
            endcase
         endselect
         `We just visited a new cell!!
         inc VisitedCells
      else
         `In this case, there is nowhere to go - so lets set the current cell to the last one...
         currentCell = CellStack()
         remove from stack CellStack()
      endif
   endwhile
 
   `Create a 2D Map image
   cls
 
   IMGX# = M_WIDTH  * MAP_SCALE#
   IMGY# = M_HEIGHT * MAP_SCALE#
 
   SCALEX# = (IMGX#-1) / M_WIDTH
   SCALEY# = (IMGY#-1) / M_HEIGHT
 
   for i = 1 to M_WIDTH
      for j = 1 to M_HEIGHT
         x1 = (i-1) *  SCALEX#
         x2 =  i    *  SCALEX#
         y1 = (j-1) * -SCALEY# + (M_HEIGHT * MAP_SCALE#)
         y2 =  j    * -SCALEY# + (M_HEIGHT * MAP_SCALE#)
 
         if Maze(i,j).N = 1 then line x1, y1, x2, y1
         if Maze(i,j).S = 1 then line x1, y2, x2, y2
         if Maze(i,j).E = 1 then line x2, y1, x2, y2
         if Maze(i,j).W = 1 then line x1, y1, x1, y2
      next j
   next i
 
   get image 1, 0, 1, IMGX#, IMGY#+1, 1
endfunction