-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgameState.py
306 lines (238 loc) · 10.9 KB
/
gameState.py
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# Represents a Gem on the board
class Gem:
def __init__(self, color, status, point):
self.color = color
self.status = status
self.point = point
def __repr__(self):
return 'Gem(\'%s\', \'%s\', %s)' % (self.color, self.status, self.point)
# Represents the state of the game, including the board, score, and level
class GameState:
def __init__(self, boardDim):
self.gameOver = False
self.boardDim = boardDim
self.board = Board(boardDim)
self.level = 0
self.score = 0
self.percentComplete = 0
# Updates the board to reflect the given move and adds to the total score
def makeMove(self, move):
score, numMatches, matches = self.board.makeMove(move)
self.score += score
# Returns the current level
def getLevel(self):
return self.level
# Returns the total score
def getScore(self):
return self.score
# Returns the percent complete as a decimal
def getCompletion(self):
return self.percentComplete
# Represents the game board as a list of lists containing Gem objects
class Board:
def __init__(self, boardDim):
self.boardDim = boardDim
self.board = [[None for x in range(self.boardDim.x)] for y in range(self.boardDim.y)]
self.matchTypes = {'5 consecutive': (self.getMatch5, 500),
'L or T': (self.getMatchLT, 150),
'4 consecutive': (self.getMatch4, 100),
'3 consecutive': (self.getMatch3, 50)}
# Updates the board to reflect the given move, including removing gems
# and making them 'fall' into empty spaces. New unknown gems are
# represented by None.
# Returns the number of points gained
def makeMove(self, move):
self.swapGems(move)
matches = []
points = 1
totalPoints = 0
numMatches = -1
while points > 0:
numMatches += 1
points, matchList = self.removeMatches()
matches.extend(matchList)
self.dropGems()
totalPoints += points
return totalPoints, numMatches, matches
# returns True if move would cause a new match, False otherwise
def moveMakesMatch(self, move):
self.swapGems(move)
madeMatch = self.containsMatch([move.point1, move.point2])
self.swapGems(move)
return madeMatch
# Updates the board to represent just the swapping of two Gems without
# removing any matches
def swapGems(self, move):
pt1, pt2 = move.pointTuple()
if self.isOnBoard(pt1) and self.isOnBoard(pt2):
gem1 = self.board[pt1.y][pt1.x]
gem2 = self.board[pt2.y][pt2.x]
if gem1 != None:
gem1.point = pt2
if gem2 != None:
gem2.point = pt1
self.board[pt2.y][pt2.x] = gem1
self.board[pt1.y][pt1.x] = gem2
# Causes all the gems to obey gravity
def dropGems(self):
for x in range(self.boardDim.x):
for y in range(1, self.boardDim.y):
if self.board[y][x] == None:
for newY in range(y - 1, -1, -1):
gem = self.board[newY][x]
if gem != None:
gem.point = Point(x, newY + 1)
self.board[newY + 1][x] = gem
self.board[0][x] = None
# Returns True if there are matches on the board containing one of the Points in points, False otherwise
def containsMatch(self, points):
for matchName, (matchFunc, matchPoints) in self.matchTypes.items():
for x in range(self.boardDim.x):
for y in range(self.boardDim.y):
gem = self.board[y][x]
if gem != None:
match = matchFunc(gem)
if match != None:
for matchedGem in match.matchedGems:
for point in points:
if point.x == matchedGem.point.x and point.y == matchedGem.point.y:
return True
return False
# Removes any matches, placing special Gems in case of L, T, or 4- or
# 5-in-a-row
# Returns the number of points gained
def removeMatches(self):
matches = []
totalPoints = 0
for matchName, (matchFunc, matchPoints) in self.matchTypes.items():
#print 'Searching for matches of type: %s' % matchName
for x in range(self.boardDim.x):
for y in range(self.boardDim.y):
gem = self.board[y][x]
if gem != None:
match = matchFunc(gem)
if match != None:
matches.append(match)
# Remove all Gems that were contained in the Match
for gem in match.matchedGems:
self.board[gem.point.y][gem.point.x] = None
replaceGem = match.replaceGem
if replaceGem != None:
pt = replaceGem.point
self.board[pt.y][pt.x] = match.replaceGem
totalPoints += matchPoints
return totalPoints, matches
# Returns a Match if gem is the topleft-most gem in a 5-in-a-row match
def getMatch5(self, gem):
return self.getMatchX(gem, 5)
# Returns a Match if gem is the topleft-most gem in an "L" or "T" match
def getMatchLT(self, gem):
dirs = (Point(1,0),Point(0,1))
for direction in dirs:
matchedGems = self.getMatchedGemsXDir(gem, 3, direction)
if matchedGems != None:
dir2 = dirs[direction == dirs[0]] # dir2 is the item in dirs that is not direction
for matchedGem in matchedGems:
matchedGems2 = self.getMatchedGemsXDir(matchedGem, 3, dir2)
if matchedGems2 != None:
# Return a Match corresponding to the found match
matchedGems2.remove(matchedGem)
matchedGems.extend(matchedGems2)
return Match(matchedGems, None)
# Returns a Match if gem is the topleft-most gem in a 4-in-a-row match
def getMatch4(self, gem):
return self.getMatchX(gem, 4)
# Returns a Match if gem is the topleft-most gem in a 3-in-a-row match
def getMatch3(self, gem):
return self.getMatchX(gem, 3)
# Returns a Match if gem is the topleft-most gem in a sequence of x
# same-colored gems, or None otherwise
def getMatchX(self, gem, x):
dirs = (Point(1,0), Point(0,1))
for direction in dirs:
matchedGems = self.getMatchedGemsXDir(gem, x, direction)
if matchedGems != None:
return Match(matchedGems, None)
return None
# Returns a list of gems if gem is the first in a sequence of x same-colored
# gems in direction
def getMatchedGemsXDir(self, gem, x, direction):
matchedGems = [gem]
for i in range(1, x):
newPt = direction * i + gem.point
if self.isOnBoard(newPt):
newGem = self.board[newPt.y][newPt.x]
if newGem == None or newGem.color != gem.color:
return None
#if gem.color == 'purple' and gem.point.x == 1 and gem.point.y == 2 and x == 3:
# print direction, newPt, i, x, newGem
matchedGems.append(newGem)
else:
return None
return matchedGems
# Returns True if the given point is on the board and False otherwise
def isOnBoard(self, point):
return point.x >= 0 and point.y >= 0 and point.x < self.boardDim.x and point.y < self.boardDim.y
def __repr__(self):
rep = str(self.boardDim) + '\n'
for row in self.board:
rep += str(row) + '\n'
return rep
# Represents a Gem match
class Match:
def __init__(self, matchedGems, replaceGem):
self.matchedGems = matchedGems
self.replaceGem = replaceGem
def __repr__(self):
return 'Match(%s, %s)' % (self.matchedGems, self.replaceGem)
# Represents a 2D point
class Point:
def __init__(self, x=0, y=0):
self.x = x
self.y = y
def toTuple(self):
return (self.x, self.y)
def __add__(self, other):
return Point(self.x + other.x, self.y + other.y)
def __sub__(self, other):
return Point(self.x - other.x, self.y - other.y)
# Multiplication by a constant
def __mul__(self, other):
return Point(self.x * other, self.y * other)
# Division by a constant
def __div__(self, other):
return Point(self.x / other, self.y / other)
def __repr__(self):
return 'Point(%i, %i)' % (self.x, self.y)
# Represents an RGB value as a 3D vector
class RGB:
def __init__(self, r=0, g=0, b=0):
self.r = r
self.g = g
self.b = b
def toTuple(self):
return (self.r, self.g, self.b)
def __add__(self, other):
return RGB(self.r + other.r, self.g + other.g, self.b + other.b)
def __sub__(self, other):
return RGB(self.r - other.r, self.g - other.g, self.b - other.b)
# Multiplication by a constant
def __mul__(self, other):
return RGB(self.r * other, self.g * other, self.b * other)
# Division by a constant
def __div__(self, other):
return RGB(self.r / other, self.g / other, self.b / other)
def __repr__(self):
return 'RGB(%i, %i, %i)' % (self.r, self.g, self.b)
def distSquared(self, other):
return (self.r-other.r)**2+(self.g-other.g)**2+(self.b-other.b)**2
# Represents a move the player can make
class Move:
def __init__(self, point1, point2, score):
self.point1 = point1
self.point2 = point2
self.score = score
def pointTuple(self):
return (self.point1, self.point2)
def __repr__(self):
return 'Move(%s, %s, %i)' % (self.point1, self.point2, self.score)