Advertisement
AyrA

Maze module

May 12th, 2014
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Option Explicit
  2. Option Base 0
  3.  
  4. Private Z() As Integer
  5. Private tmpCell(3) As Cell
  6. Private Cells As New Collection
  7.  
  8. Public Sub Generate(ByVal Width As Integer, ByVal Height As Integer)
  9.     Dim neighbors() As New Cell
  10.     Dim current As New Cell
  11.     Dim i As Integer
  12.     Dim j As Integer
  13.    
  14.    
  15.     Set tmpCell(0) = CreateCell(0, 1)
  16.     Set tmpCell(1) = CreateCell(0, -1)
  17.     Set tmpCell(2) = CreateCell(1, 0)
  18.     Set tmpCell(3) = CreateCell(-1, 0)
  19.  
  20.    
  21.    
  22.     initStack
  23.    
  24.    
  25.     current.x = 1
  26.     current.y = 1
  27.    
  28.     Cells.Add current
  29.    
  30.     ReDim Z(Width - 1, Height - 1)
  31.    
  32.     Randomize
  33.    
  34.     For i = 0 To Width - 1
  35.         For j = 0 To Height - 1
  36.             Z(i, j) = 1
  37.         Next
  38.     Next i
  39.    
  40.     While Cells.Count > 0
  41.         Z(current.x, current.y) = 0
  42.         neighbors = GetValidNeighbors(current, Width, Height)
  43.         'REM neighbors(x) is never actually nothing, even if said to be so
  44.        'doing the "neighbors(x) is nothing" compaison magically creates a class
  45.        If neighbors(0) Is Nothing And neighbors(1) Is Nothing And neighbors(2) Is Nothing And neighbors(3) Is Nothing Then
  46.             Set current = Pop()
  47.         Else
  48.             Push current
  49.             Set current = GetRnd(neighbors)
  50.         End If
  51.     Wend
  52. End Sub
  53.  
  54. Private Function GetValidNeighbors(centerTile As Cell, ByVal Width As Integer, ByVal Height As Integer) As Cell()
  55.     Dim Count As Integer
  56.     Dim i As Integer
  57.     Dim toCheck As New Cell
  58.     Dim validNeighbors(3) As Cell
  59.    
  60.     For i = 0 To 3
  61.         Set toCheck = New Cell
  62.         toCheck.x = centerTile.x + tmpCell(i).x
  63.         toCheck.y = centerTile.y + tmpCell(i).y
  64.        
  65.         'Explicitely setting nothing also does not work
  66.        Set validNeighbors(i) = Nothing
  67.        
  68.         If (toCheck.x Mod 2 = 1 Or toCheck.y Mod 2 = 1) And IsInside(toCheck, Width, Height) Then
  69.             If Z(toCheck.x, toCheck.y) = 1 And HasThreeWallsIntact(toCheck, Width, Height) Then
  70.                 Set validNeighbors(Count) = New Cell
  71.                 validNeighbors(Count).x = toCheck.x
  72.                 validNeighbors(Count).y = toCheck.y
  73.                 Count = Count + 1
  74.             End If
  75.         End If
  76.     Next
  77.    
  78.     GetValidNeighbors = validNeighbors
  79. End Function
  80.  
  81. Private Function HasThreeWallsIntact(toCheck As Cell, ByVal w As Integer, ByVal h As Integer)
  82.     Dim Count As Integer
  83.     Dim i As Integer
  84.     Dim neighborToCheck As New Cell
  85.    
  86.     Count = 0
  87.    
  88.     For i = 0 To 3
  89.         Set neighborToCheck = New Cell
  90.         neighborToCheck.x = toCheck.x + tmpCell(i).x
  91.         neighborToCheck.y = toCheck.y + tmpCell(i).y
  92.        
  93.         If IsInside(neighborToCheck, w, h) Then
  94.             If Z(neighborToCheck.x, neighborToCheck.y) = 0 Then
  95.                 Count = Count + 1
  96.             End If
  97.         End If
  98.     Next
  99.     HasThreeWallsIntact = Count
  100. End Function
  101.  
  102. Private Function IsInside(c As Cell, ByVal w As Integer, ByVal h As Integer) As Boolean
  103.     IsInside = (c.x >= 0 And c.y >= 0 And c.x < w And c.y < h)
  104. End Function
  105.  
  106. ''CELL''
  107. Private Function CreateCell(x As Integer, y As Integer)
  108.     Dim a As New Cell
  109.     a.x = x
  110.     a.y = y
  111.     Set CreateCell = a
  112. End Function
  113.  
  114. ''STACK''
  115. Private Sub initStack()
  116.     Set Cells = New Collection
  117. End Sub
  118.  
  119. Private Sub Push(newItem As Variant)
  120.     With Cells
  121.         .Add newItem
  122.     End With
  123. End Sub
  124.  
  125. Public Function Pop() As Variant
  126.     With Cells
  127.         If .Count > 0 Then
  128.             Set Pop = .Item(.Count)
  129.             .Remove .Count
  130.         End If
  131.     End With
  132. End Function
  133.  
  134. ''MISC''
  135. Private Function IsVarArrayEmpty(anArray As Variant)
  136.     Dim i As Integer
  137.    
  138.     On Error Resume Next
  139.     i = UBound(anArray, 1)
  140.     If Err.Number = 0 And i <> -1 Then
  141.         IsVarArrayEmpty = False
  142.     Else
  143.         IsVarArrayEmpty = True
  144.     End If
  145.  
  146. End Function
  147.  
  148. Private Function GetRnd(arr)
  149.     Dim a
  150.     Set a = Nothing
  151.    
  152.     While a Is Nothing
  153.         Set a = arr(Rnd * UBound(arr))
  154.     Wend
  155.    
  156.     Set GetRnd = a
  157. End Function
  158.  
  159. Public Sub Test()
  160.     Call Generate(10, 10)
  161. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement