Microsoft Small Basic

Program Listing: NRW874-0
'Maze Maker
'Robin Andrews'
'
'set graphics window
GraphicsWindow.Show()
screenWidth = Desktop.Width
screenHeight = Desktop.Height
rightMargin = 15
bottomMargin = 75
GraphicsWindow.Width = screenWidth - rightMargin
GraphicsWindow.Height = screenHeight - bottomMargin
GraphicsWindow.Top = 0
GraphicsWindow.Left = 0
background = "cyan"
GraphicsWindow.BackgroundColor = background
GraphicsWindow.PenWidth = 1
'initialise cell array and other variables
rows = 21
columns = 21
size = (GraphicsWindow.Height - 100)/rows
tableX = (GraphicsWindow.Width - columns*size)/2
tableY = (GraphicsWindow.Height - rows*size)/2
For r = 1 to rows
For c = 1 to columns
cell[r][c]["xPos"] = (tableX + (c - 1) * size)
cell[r][c]["yPos"] = (tableY + (r - 1) * size)
'If Math.Remainder(r, 2) = 0 And Math.Remainder(c, 2) = 0 Then
'cell[r][c]["path"] = "true"
'Else
cell[r][c]["path"] = "false"
'EndIf
EndFor
EndFor
neighbors_c[0] = -1 ' W
neighbors_r[0] = 0
neighbors_c[1] = 0 ' S
neighbors_r[1] = 1
neighbors_c[2] = 1 ' E
neighbors_r[2] = 0
neighbors_c[3] = 0 ' N
neighbors_r[3] = -1
'draw initial grid
drawGrid()
' DFS (Depth-First Search) algorithm
' create a CellStack (LIFO) to hold a list of cell locations
' set totalCells = number of cells in grid
rows2 = (rows - 1) / 2
cols2 = (columns - 1) / 2
totalCells = rows2 * cols2
' choose a cell at random and call it CurrentCell (c and r)
c = Math.GetRandomNumber(cols2) * 2
r = Math.GetRandomNumber(rows2) * 2
cell[r][c]["path"] = "true"
drawCell()
visitedCells = 1
'
While visitedCells < totalCells
' find all neighbors of CurrentCell with all walls intact
nWalls = 0
For i = 0 To 3
nc = c + neighbors_c[i] * 2
nr = r + neighbors_r[i] * 2
If 1 < nc And nc < columns And 1 < nr And nr < rows And cell[nr][nc]["path"] = "false" Then
nWalls = nWalls + 1
walls[nWalls] = i
EndIf
EndFor
'if one or more found
If nWalls >= 1 Then
' choose one at random
i = walls[Math.GetRandomNumber(nWalls)]
' push CurrentCell location on the CellStack
Stack.PushValue("col", c)
Stack.PushValue("row", r)
' knock down the wall between it and CurrentCell
c = c + neighbors_c[i]
r = r + neighbors_r[i]
cell[r][c]["path"] = "true"
drawCell() ' knock down the wall
' make the new cell CurrentCell
c = c + neighbors_c[i]
r = r + neighbors_r[i]
cell[r][c]["path"] = "true"
drawCell() ' knock down the cell
' add 1 to VisitedCells
visitedCells = visitedCells + 1
Else
' pop the most recent cell entry off the CellStack
' make it CurrentCell
c = Stack.PopValue("col")
r = Stack.PopValue("row")
EndIf
Program.Delay(100)
EndWhile

'detect mouse events to create path
GraphicsWindow.MouseDown = mouseDownEvent

'mouseDownEvent subroutine
Sub mouseDownEvent
x = GraphicsWindow.MouseX - tableX
y = GraphicsWindow.mouseY - tableY
If Mouse.IsLeftButtonDown Then
c = Math.Floor(x / size) + 1
r = Math.Floor(y / size) + 1
If 1 <= r And r <= rows And 1 <= c And c <= columns Then
If cell[r][c]["path"] = "true" Then
cell[r][c]["path"] = "false"
ElseIf cell [r][c]["path"] = "false" Then
cell[r][c]["path"] = "true"
EndIf
drawCell()
EndIf
EndIf
EndSub

'drawGrid subroutine
Sub drawGrid
For r = 1 To rows
For c = 1 To columns
drawCell()
EndFor
Endfor
EndSub

'drawCell subroutine
Sub drawCell
' param r - row
' param c - column
If cell[r][c]["path"] = "false" then
GraphicsWindow.BrushColor = "blue"
Else
GraphicsWindow.BrushColor = "white"
Endif
GraphicsWindow.FillRectangle(cell[r][c]["xPos"], cell[r][c]["yPos"], size, size)
GraphicsWindow.DrawRectangle(cell[r][c]["xPos"], cell[r][c]["yPos"], size, size)
EndSub