MCPcopy Create free account
hub / github.com/asweigart/PythonStdioGames / canMakeMove

Function canMakeMove

src/gamesbyexample/pygame_games/gemgem.py:273–316  ·  view source on GitHub ↗
(board)

Source from the content-addressed store, hash-verified

271
272
273def canMakeMove(board):
274 # Return True if the board is in a state where a matching
275 # move can be made on it. Otherwise return False.
276
277 # The patterns in oneOffPatterns represent gems that are configured
278 # in a way where it only takes one move to make a triplet.
279 oneOffPatterns = (((0,1), (1,0), (2,0)),
280 ((0,1), (1,1), (2,0)),
281 ((0,0), (1,1), (2,0)),
282 ((0,1), (1,0), (2,1)),
283 ((0,0), (1,0), (2,1)),
284 ((0,0), (1,1), (2,1)),
285 ((0,0), (0,2), (0,3)),
286 ((0,0), (0,1), (0,3)))
287
288 # The x and y variables iterate over each space on the board.
289 # If we use + to represent the currently iterated space on the
290 # board, then this pattern: ((0,1), (1,0), (2,0))refers to identical
291 # gems being set up like this:
292 #
293 # +A
294 # B
295 # C
296 #
297 # That is, gem A is offset from the + by (0,1), gem B is offset
298 # by (1,0), and gem C is offset by (2,0). In this case, gem A can
299 # be swapped to the left to form a vertical three-in-a-row triplet.
300 #
301 # There are eight possible ways for the gems to be one move
302 # away from forming a triple, hence oneOffPattern has 8 patterns.
303
304 for x in range(BOARDWIDTH):
305 for y in range(BOARDHEIGHT):
306 for pat in oneOffPatterns:
307 # check each possible pattern of "match in next move" to
308 # see if a possible move can be made.
309 if (getGemAt(board, x+pat[0][0], y+pat[0][1]) == \
310 getGemAt(board, x+pat[1][0], y+pat[1][1]) == \
311 getGemAt(board, x+pat[2][0], y+pat[2][1]) != None) or \
312 (getGemAt(board, x+pat[0][1], y+pat[0][0]) == \
313 getGemAt(board, x+pat[1][1], y+pat[1][0]) == \
314 getGemAt(board, x+pat[2][1], y+pat[2][0]) != None):
315 return True # return True the first time you find a pattern
316 return False
317
318
319def drawMovingGem(gem, progress):

Callers 1

runGameFunction · 0.85

Calls 1

getGemAtFunction · 0.85

Tested by

no test coverage detected