def score_moves(self, board): '''Score next moves for a board''' scores = {} depth = 9 - len(board.next_moves()) for pos in board.next_moves(): next_board = Board(board.state()) next_board.move(pos) if next_board.winner() == board.X_WINS: scores[pos] = 10 - depth elif next_board.winner() == board.O_WINS: scores[pos] = -10 + depth elif next_board.winner() == board.TIE: scores[pos] = 0 else: score, _ = self.best_moves(next_board) scores[pos] = score return scores
def play_game(player1, player2): '''Play a game with two robots''' board = Board() for turn in range(9): player = player2 if (turn % 2) else player1 board.move(player.next_move(board)) if board.winner(): return board