Public
Sub
createMaze(
ByVal
m
As
Integer
,
randomNumber
Random)
'create new maze
ReDim
Grid.Cells(m - 1)
For
c
= 0
To
m - 1
Grid.Cells(c)(m - 1)
r
Grid.Cells(c)(r) =
New
Grid.cellDetails
Next
Grid.Cells.GetUpperBound(0)
Grid.Cells(0).GetUpperBound(0)
Grid.Cells(c)(r).visited =
False
Grid.Cells(c)(r).NorthWall =
True
Grid.Cells(c)(r).SouthWall =
Grid.Cells(c)(r).WestWall =
Grid.Cells(c)(r).EastWall =
'Stop
Dim
maze
List(Of Point)
unvisited
= m ^ 2
Grid.startPoint =
Point(randomNumber.
(0, m), m - 1)
Grid.Cells(Grid.startPoint.X)(m - 1).SouthWall =
maze.Add(
Point(Grid.startPoint.X, m - 1))
unvisited -= 1
While
unvisited > 0
p
Point = maze(randomNumber.
(0, maze.Count))
choice
If
p.X > 0
And
p.X < m - 1
Then
'c
p.Y > 0
p.Y < m - 1
'r
choice.AddRange(
Point() {
Point(p.X - 1, p.Y),
Point(p.X, p.Y - 1),
Point(p.X + 1, p.Y),
Point(p.X, p.Y + 1)})
'l,t,r,b
ElseIf
p.Y = 0
'l,r,b
p.Y = m - 1
Point(p.X + 1, p.Y)})
'l,t,r
End
p.X = 0
't,r,b
'r,b
't,r
p.X = m - 1
'l,t,b
'l,b
Point(p.X, p.Y - 1)})
'l,t
choice.RemoveAll(
Function
(pt) Grid.Cells(pt.X)(pt.Y).visited)
choice.Count = 0
Continue
p2
Point = choice(randomNumber.
(0, choice.Count))
p.X = p2.X
p2.Y < p.Y
Grid.Cells(p.X)(p.Y).NorthWall
Grid.Cells(p.X)(p.Y).NorthWall =
Grid.Cells(p2.X)(p2.Y).SouthWall =
Grid.Cells(p2.X)(p2.Y).visited =
Point(p2.X, p2.Y))
Else
p2.Y > p.Y
Grid.Cells(p.X)(p.Y).SouthWall
Grid.Cells(p.X)(p.Y).SouthWall =
Grid.Cells(p2.X)(p2.Y).NorthWall =
maze.Add(p2)
p.X > p2.X
p2.Y = p.Y
Grid.Cells(p.X)(p.Y).WestWall
Grid.Cells(p.X)(p.Y).WestWall =
Grid.Cells(p2.X)(p2.Y).EastWall =
p.X < p2.X
Grid.Cells(p.X)(p.Y).EastWall
Grid.Cells(p.X)(p.Y).EastWall =
Grid.Cells(p2.X)(p2.Y).WestWall =
solveMaze(
xPos
yPos
alreadySearched(,)
Boolean
solution
List(Of Point))
correctPath
=
'should the computer check this cell
shouldCheck
'Check for out of boundaries
xPos >= m
OrElse
xPos < 0
yPos >= m
yPos < 0
shouldCheck =
'Check if at finish
Point(xPos, yPos) = Grid.endPoint
correctPath =
'Check if previously searched
alreadySearched(xPos, yPos)
'Search the cell
'mark cell as searched
alreadySearched(xPos, yPos) =
'Check right cell
correctPath = correctPath
Or
(Grid.Cells(xPos)(yPos).EastWall =
, solveMaze(m, xPos + 1, yPos, alreadySearched, solution),
)
'Check down cell
(Grid.Cells(xPos)(yPos).SouthWall =
, solveMaze(m, xPos, yPos + 1, alreadySearched, solution),
'check left cell
(Grid.Cells(xPos)(yPos).WestWall =
, solveMaze(m, xPos - 1, yPos, alreadySearched, solution),
'check up cell
(Grid.Cells(xPos)(yPos).NorthWall =
, solveMaze(m, xPos, yPos - 1, alreadySearched, solution),
'add cell to solution path
solution.Add(
Point(xPos, yPos))
Return