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

Function getComputerMove

src/gamesbyexample/pygame_games/flippy.py:486–508  ·  view source on GitHub ↗
(board, computerTile)

Source from the content-addressed store, hash-verified

484
485
486def getComputerMove(board, computerTile):
487 # Given a board and the computer's tile, determine where to
488 # move and return that move as a [x, y] list.
489 possibleMoves = getValidMoves(board, computerTile)
490
491 # randomize the order of the possible moves
492 random.shuffle(possibleMoves)
493
494 # always go for a corner if available.
495 for x, y in possibleMoves:
496 if isOnCorner(x, y):
497 return [x, y]
498
499 # Go through all possible moves and remember the best scoring move
500 bestScore = -1
501 for x, y in possibleMoves:
502 dupeBoard = copy.deepcopy(board)
503 makeMove(dupeBoard, computerTile, x, y)
504 score = getScoreOfBoard(dupeBoard)[computerTile]
505 if score > bestScore:
506 bestMove = [x, y]
507 bestScore = score
508 return bestMove
509
510
511def checkForQuit():

Callers 1

runGameFunction · 0.70

Calls 4

getValidMovesFunction · 0.70
isOnCornerFunction · 0.70
makeMoveFunction · 0.70
getScoreOfBoardFunction · 0.70

Tested by

no test coverage detected