Private Sub CreateMaze()
Dim intX As Integer
Dim intY As Integer
Dim intX2 As Integer
Dim intY2 As Integer
Dim intTmpX As Integer
Dim intTmpY As Integer
Dim intDirn As Integer
Dim intTmpDirn As Integer
Dim intVertsX(sizeX, sizeY) As Integer
Dim intVertsY(sizeX, sizeY) As Integer
'clear maze
For intX = 1 To sizeX - 1
For intY = 1 To sizeY - 1
intMz(intX, intY) = 0
Next
Next
'make border
For intX = 0 To sizeX
intMz(intX, 0) = 1
intMz(intX, sizeY) = 1
Next
For intY = 0 To sizeY
intMz(0, intY) = 1
intMz(sizeX, intY) = 1
Next
'generate vertices
For intX = 2 To sizeX - 2 Step 2
For intY = 2 To sizeY - 2 Step 2
intVertsX(intX, intY) = intX
intVertsY(intX, intY) = intY
Next
Next
'mix up the order to draw verticies
For intX = 2 To sizeX - 2 Step 2
For intY = 2 To sizeY - 2 Step 2
'Let's say that sizeX and sizeY are both 60, then is
'(sizeX / 2 - 1) = 29 and (sizeY / 2 - 1) = 29 as well.
' Int(Rnd * 29) returns a value between 0 and 28. (Int(Rnd * 29) + 1)
'returns a value between 1 and 29. 1 * 2 = 2 and 29 * 2 = 58. So the
'entire function (Int(Rnd * 29) + 1) * 2 returns a value between 2
'and 58.
intX2 = (Int(Rnd * (sizeX / 2 - 1)) + 1) * 2
intY2 = (Int(Rnd * (sizeY / 2 - 1)) + 1) * 2
'To swap two variables you save the first (A) then you replace (A) with
'the other variable (B) and then you replace (B) with the saved (A)-
'variable
intTmpX = intVertsX(intX, intY)
intTmpY = intVertsY(intX, intY)
intVertsX(intX, intY) = intVertsX(intX2, intY2)
intVertsY(intX, intY) = intVertsY(intX2, intY2)
intVertsX(intX2, intY2) = intTmpX
intVertsY(intX2, intY2) = intTmpY
Next
Next
'Now draw walls.
'Draw a wall from each of the verticies in a random direction until another wall
'is reached.
' For the best looking maze we don't want any walls going all the way across
'the maze, so if we're close to an edge, we make it more likely for the wall to
'go to that closest wall.
For intX = 2 To sizeX - 2 Step 2
For intY = 2 To sizeY - 2 Step 2
intTmpX = intVertsX(intX, intY)
intTmpY = intVertsY(intX, intY)
If intMz(intTmpX, intTmpY) = 0 Then
' Top Left ----------
If intX < sizeX / 3 And intY < sizeY / 3 Then
intTmpDirn = Int(Rnd * 11)
If intTmpDirn <= 4 Then
intDirn = 0
ElseIf intTmpDirn >= 5 And intTmpDirn <= 8 Then
intDirn = 3
ElseIf intTmpDirn = 9 Then
intDirn = 2
Else
intDirn = 1
End If
' Left --------------
ElseIf intX < sizeX / 3 And intY >= sizeY / 3 And intY <= sizeY / 3 Then
intTmpDirn = Int(Rnd * 8)
If intTmpDirn <= 4 Then
intDirn = 0
ElseIf intTmpDirn = 5 Then
intDirn = 1
ElseIf intTmpDirn = 6 Then
intDirn = 2
Else
intDirn = 3
End If ' = 40 if sizeY = 60
' Bottom Left ----------- /------^------\
ElseIf intX < sizeX / 3 And intY > sizeY * (2 / 3) Then
intTmpDirn = Int(Rnd * 11)
If intTmpDirn <= 4 Then
intDirn = 0
ElseIf intTmpDirn >= 5 And intTmpDirn <= 8 Then
intDirn = 2
ElseIf intTmpDirn = 9 Then
intDirn = 1
Else
intDirn = 3
End If
' Bottom ------------
ElseIf intX >= sizeX / 3 And intX <= sizeX * (2 / 3) And intY > sizeY * (2 / 3) Then
intTmpDirn = Int(Rnd * 8)
If intTmpDirn <= 4 Then
intDirn = 2
ElseIf intTmpDirn = 5 Then
intDirn = 0
ElseIf intTmpDirn = 6 Then
intDirn = 1
Else
intDirn = 3
End If
' Bottom Right ---------
ElseIf intX > sizeX * (2 / 3) And intY > sizeY * (2 / 3) Then
intTmpDirn = Int(Rnd * 11)
If intTmpDirn <= 4 Then
intDirn = 1
ElseIf intTmpDirn >= 5 And intTmpDirn <= 8 Then
intDirn = 2
ElseIf intTmpDirn = 9 Then
intDirn = 0
Else
intDirn = 3
End If
' Right ----------
ElseIf intX > sizeX * (2 / 3) And intY >= sizeY / 3 And intY <= sizeY * (2 / 3) Then
intTmpDirn = Int(Rnd * 8)
If intTmpDirn <= 4 Then
intDirn = 1
ElseIf intTmpDirn = 5 Then
intDirn = 0
ElseIf intTmpDirn = 6 Then
intDirn = 2
Else
intDirn = 3
End If
' Top Right ----------
ElseIf intX > sizeX * (2 / 3) And intY < sizeY / 3 Then
intTmpDirn = Int(Rnd * 11)
If intTmpDirn <= 4 Then
intDirn = 1
ElseIf intTmpDirn >= 5 And intTmpDirn <= 8 Then
intDirn = 3
ElseIf intTmpDirn = 9 Then
intDirn = 0
Else
intDirn = 2
End If
' Top -------------
ElseIf intX >= sizeX / 3 And intX <= sizeX * (2 / 3) And intY < sizeY / 3 Then
intTmpDirn = Int(Rnd * 8)
If intTmpDirn <= 4 Then
intDirn = 3
ElseIf intTmpDirn = 5 Then
intDirn = 0
ElseIf intTmpDirn = 6 Then
intDirn = 1
Else
intDirn = 2
End If
' Middle -----------
Else
intDirn = Int(Rnd * 4)
End If
Do
intMz(intTmpX, intTmpY) = 1
Select Case intDirn
Case 0: intTmpX = intTmpX - 1
Case 1: intTmpX = intTmpX + 1
Case 2: intTmpY = intTmpY + 1
Case Else: intTmpY = intTmpY - 1
End Select
Loop Until intMz(intTmpX, intTmpY) = 1
End If
Next
Next
End Sub