Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Option Explicit
- Option Base 0
- Private Z() As Integer
- Private tmpCell(3) As Cell
- Private Cells As New Collection
- Public Sub Generate(ByVal Width As Integer, ByVal Height As Integer)
- Dim neighbors() As New Cell
- Dim current As New Cell
- Dim i As Integer
- Dim j As Integer
- Set tmpCell(0) = CreateCell(0, 1)
- Set tmpCell(1) = CreateCell(0, -1)
- Set tmpCell(2) = CreateCell(1, 0)
- Set tmpCell(3) = CreateCell(-1, 0)
- initStack
- current.x = 1
- current.y = 1
- Cells.Add current
- ReDim Z(Width - 1, Height - 1)
- Randomize
- For i = 0 To Width - 1
- For j = 0 To Height - 1
- Z(i, j) = 1
- Next
- Next i
- While Cells.Count > 0
- Z(current.x, current.y) = 0
- neighbors = GetValidNeighbors(current, Width, Height)
- 'REM neighbors(x) is never actually nothing, even if said to be so
- 'doing the "neighbors(x) is nothing" compaison magically creates a class
- If neighbors(0) Is Nothing And neighbors(1) Is Nothing And neighbors(2) Is Nothing And neighbors(3) Is Nothing Then
- Set current = Pop()
- Else
- Push current
- Set current = GetRnd(neighbors)
- End If
- Wend
- End Sub
- Private Function GetValidNeighbors(centerTile As Cell, ByVal Width As Integer, ByVal Height As Integer) As Cell()
- Dim Count As Integer
- Dim i As Integer
- Dim toCheck As New Cell
- Dim validNeighbors(3) As Cell
- For i = 0 To 3
- Set toCheck = New Cell
- toCheck.x = centerTile.x + tmpCell(i).x
- toCheck.y = centerTile.y + tmpCell(i).y
- 'Explicitely setting nothing also does not work
- Set validNeighbors(i) = Nothing
- If (toCheck.x Mod 2 = 1 Or toCheck.y Mod 2 = 1) And IsInside(toCheck, Width, Height) Then
- If Z(toCheck.x, toCheck.y) = 1 And HasThreeWallsIntact(toCheck, Width, Height) Then
- Set validNeighbors(Count) = New Cell
- validNeighbors(Count).x = toCheck.x
- validNeighbors(Count).y = toCheck.y
- Count = Count + 1
- End If
- End If
- Next
- GetValidNeighbors = validNeighbors
- End Function
- Private Function HasThreeWallsIntact(toCheck As Cell, ByVal w As Integer, ByVal h As Integer)
- Dim Count As Integer
- Dim i As Integer
- Dim neighborToCheck As New Cell
- Count = 0
- For i = 0 To 3
- Set neighborToCheck = New Cell
- neighborToCheck.x = toCheck.x + tmpCell(i).x
- neighborToCheck.y = toCheck.y + tmpCell(i).y
- If IsInside(neighborToCheck, w, h) Then
- If Z(neighborToCheck.x, neighborToCheck.y) = 0 Then
- Count = Count + 1
- End If
- End If
- Next
- HasThreeWallsIntact = Count
- End Function
- Private Function IsInside(c As Cell, ByVal w As Integer, ByVal h As Integer) As Boolean
- IsInside = (c.x >= 0 And c.y >= 0 And c.x < w And c.y < h)
- End Function
- ''CELL''
- Private Function CreateCell(x As Integer, y As Integer)
- Dim a As New Cell
- a.x = x
- a.y = y
- Set CreateCell = a
- End Function
- ''STACK''
- Private Sub initStack()
- Set Cells = New Collection
- End Sub
- Private Sub Push(newItem As Variant)
- With Cells
- .Add newItem
- End With
- End Sub
- Public Function Pop() As Variant
- With Cells
- If .Count > 0 Then
- Set Pop = .Item(.Count)
- .Remove .Count
- End If
- End With
- End Function
- ''MISC''
- Private Function IsVarArrayEmpty(anArray As Variant)
- Dim i As Integer
- On Error Resume Next
- i = UBound(anArray, 1)
- If Err.Number = 0 And i <> -1 Then
- IsVarArrayEmpty = False
- Else
- IsVarArrayEmpty = True
- End If
- End Function
- Private Function GetRnd(arr)
- Dim a
- Set a = Nothing
- While a Is Nothing
- Set a = arr(Rnd * UBound(arr))
- Wend
- Set GetRnd = a
- End Function
- Public Sub Test()
- Call Generate(10, 10)
- End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement