-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMechanics.bas
More file actions
292 lines (249 loc) · 10.6 KB
/
Mechanics.bas
File metadata and controls
292 lines (249 loc) · 10.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
Attribute VB_Name = "Mechanics"
Option Explicit
Public Enum Directions
Up
Down
Left
Right
End Enum
Function EmptyCellCount(gameCells() As Integer) As Integer
Dim empties As Collection
Set empties = GetEmptyCells(gameCells)
EmptyCellCount = empties.Count
End Function
Sub RandomlyPlace2Or4(gameCells() As Integer)
'' WARNING this will modify the gameCells argument passed in (by ref).
If EmptyCellCount(gameCells) = 0 Then
Call addLog("OH NO! You can't place a new tile if there are no empty spots!")
Exit Sub
End If
Dim randomTileValue As Integer
' this will produce a 2 or a 4
randomTileValue = (Int(Rnd * 2) + 1) * 2
Dim empties As Collection
Set empties = GetEmptyCells(gameCells)
Dim randomEmptySlot As Integer
randomEmptySlot = (Int(Rnd * empties.Count) + 1)
gameCells(empties.Item(randomEmptySlot).x, empties.Item(randomEmptySlot).y) = randomTileValue
End Sub
Function GetEmptyCells(gameCells() As Integer) As Collection
Dim empties As Collection
Set empties = New Collection
Dim t As Tile
Dim x As Integer, y As Integer
For y = 0 To UBound(gameCells, 2)
For x = 0 To UBound(gameCells, 1)
If gameCells(x, y) = 0 Then
Set t = New Tile
t.x = x
t.y = y
empties.Add t
End If
Next x
Next y
Set GetEmptyCells = empties
End Function
Function ApplyGravity(gameCells() As Integer, dx As Integer, dy As Integer, stepX As Integer, stepY As Integer, startX As Integer, startY As Integer, endX As Integer, endY As Integer) As Collection
Dim x As Integer, y As Integer
Dim needToContinueLoop As Boolean
Dim a As AnimationStep
Dim animationSteps As Collection
Set animationSteps = New Collection
Dim gravityStartX As Integer
Dim gravityStartY As Integer
Dim gravityEndX As Integer
Dim gravityEndY As Integer
Dim walk As Integer
Dim fullCellFound As Boolean
Do
needToContinueLoop = False
For y = startY To endY Step stepY ' each row
'start at the given cell:
For x = startX To endX Step stepX
If gameCells(x, y) = 0 Then
gravityEndX = x
gravityEndY = y
fullCellFound = False
If x + dx >= 0 And y + dy >= 0 And x + dx <= UBound(gameCells) And y + dy <= UBound(gameCells) Then
If dx = 0 Then
For walk = y + dy To endY Step stepY
' if we've walked to a cell that's full, record its position, jump out of the foor loop
If gameCells(x, walk) > 0 Then
' record it
gravityStartX = x
gravityStartY = walk
fullCellFound = True
Exit For
End If
Next walk
End If
If dy = 0 Then
For walk = x + dx To endX Step stepX
' if we've walked to a cell that's full, record its position, jump out of the foor loop
If gameCells(walk, y) > 0 Then
' record it
gravityStartX = walk
gravityStartY = y
fullCellFound = True
Exit For
End If
Next walk
End If
If fullCellFound Then
gameCells(gravityEndX, gravityEndY) = gameCells(gravityStartX, gravityStartY)
gameCells(gravityStartX, gravityStartY) = 0
needToContinueLoop = True
' Add to the list of proposed moves that goes back to the form
Set a = New AnimationStep
a.startX = gravityStartX
a.startY = gravityStartY
a.endX = gravityEndX
a.endY = gravityEndY
a.cellValue = gameCells(gravityEndX, gravityEndY)
animationSteps.Add a
End If
End If
End If
' if we're not empty we're done, skip ahead
Next x
Next y
Loop While needToContinueLoop
Set ApplyGravity = animationSteps
End Function
Function ApplyMerges(gameCells() As Integer, dx As Integer, dy As Integer, _
stepX As Integer, stepY As Integer, _
startX As Integer, startY As Integer, _
endX As Integer, endY As Integer) As Collection
Dim x As Integer
Dim y As Integer
Dim a As AnimationStep
Dim animationSteps As Collection
Set animationSteps = New Collection
For y = startY To endY Step stepY ' each row
For x = startX To endX Step stepX
If Not gameCells(x, y) = 0 Then
If x + dx >= 0 And y + dy >= 0 And x + dx <= UBound(gameCells) And y + dy <= UBound(gameCells) Then
' look at our neighbour - if they're the same as us, merge.
If gameCells(x + dx, y + dy) = gameCells(x, y) Then
'double ourselves and blank them!
gameCells(x, y) = gameCells(x, y) * 2
gameCells(x + dx, y + dy) = 0
' apply gravity on the remainder of this row
Set animationSteps = appendCollection(animationSteps, ApplyGravity(gameCells, dx, dy, stepX, stepY, x + dx, y + dy, endX, y + dy))
Set a = New AnimationStep
a.startX = x + dx
a.startY = y + dy
a.endX = x
a.endY = y
a.amIaMerge = True
a.cellValue = gameCells(x, y)
animationSteps.Add a
End If
End If
End If
' if the cell is empty, we're done, because that means the rest of the row is empty too (gravity)
Next x
Next y
Set ApplyMerges = animationSteps
End Function
Function appendCollection(a As Collection, b As Collection) As Collection
Dim newCollection As Collection
Set newCollection = a
Dim thing As Object
For Each thing In b
newCollection.Add thing
Next thing
Set appendCollection = newCollection
End Function
Function NeighbouringTwins(gameCells() As Integer) As Boolean
Dim areThereNeighbours As Boolean
areThereNeighbours = False
Dim x As Integer, y As Integer
For y = 0 To UBound(gameCells)
For x = 0 To UBound(gameCells)
If x + 1 <= UBound(gameCells) Then
If gameCells(x, y) = gameCells(x + 1, y) Then
areThereNeighbours = True
End If
End If
If y + 1 <= UBound(gameCells) Then
If gameCells(x, y) = gameCells(x, y + 1) Then
areThereNeighbours = True
End If
End If
Next x
Next y
NeighbouringTwins = areThereNeighbours
End Function
Function GameStep(gameCells() As Integer, direction As Directions) As Collection
Dim dx As Integer, dy As Integer
Dim stepX As Integer, stepY As Integer
Dim startX As Integer, startY As Integer
Dim endX As Integer, endY As Integer
Select Case direction
Case Directions.Right
dx = -1
dy = 0
stepX = -1
stepY = 1
startX = UBound(gameCells)
startY = 0
endX = 0
endY = UBound(gameCells)
Case Directions.Left
dx = 1
dy = 0
stepX = 1
stepY = 1
startX = 0
startY = 0
endX = UBound(gameCells)
endY = UBound(gameCells)
Case Directions.Up
dx = 0
dy = 1
stepX = 1
stepY = 1
startX = 0
startY = 0
endX = UBound(gameCells)
endY = UBound(gameCells)
Case Directions.Down
dx = 0
dy = -1
stepX = -1
stepY = -1
startX = UBound(gameCells)
startY = UBound(gameCells)
endX = 0
endY = 0
Case Else
Call addLog("exiting because of unimplemented direction")
Exit Function
End Select
If EmptyCellCount(gameCells) = 0 Then
Call addLog("GameStep(): no empty cells! Check if there are legal moves left.")
If NeighbouringTwins(gameCells) Then
Call addLog("GameStep(): there are, however, valid moves left.")
Else
Call addLog("GameStep(): there are no valid moves left. Game over!")
Set GameStep = New Collection
Exit Function
End If
End If
Dim animationSteps As Collection
Dim didGravityMove As Boolean, didMergeMove As Boolean
Set animationSteps = ApplyGravity(gameCells, dx, dy, stepX, stepY, startX, startY, endX, endY)
didGravityMove = animationSteps.Count > 0
Dim mergeAnimationSteps As Collection
Set mergeAnimationSteps = ApplyMerges(gameCells, dx, dy, stepX, stepY, startX, startY, endX, endY)
didMergeMove = mergeAnimationSteps.Count > 0
' we want to do more moves below, potentially, so let's save these:
Set mergeAnimationSteps = appendCollection(mergeAnimationSteps, animationSteps)
Set animationSteps = New Collection
If didMergeMove Or didGravityMove Or frmMain.mnuCheatAlwaysGive.Checked Then
Set animationSteps = ApplyGravity(gameCells, dx, dy, stepX, stepY, startX, startY, endX, endY)
Call RandomlyPlace2Or4(gameCells)
End If
Set GameStep = appendCollection(animationSteps, mergeAnimationSteps)
End Function