def move_mm(board, player, trials=None): """ Make a move on the board. """ score = board.check_win() if score is not None: return SCORES[score], (-1, -1) else: moves = board.get_empty_squares() compare = -2 best_move = (-1, -1) best_score = -2 for amove in moves: new_board = board.clone() new_board.move(amove[0], amove[1], player) result = move_mm(new_board, provided.switch_player(player)) value = result[0] * SCORES[player] if value > 0: return result[0], amove else: if value > compare: compare = value best_score = result[0] best_move = amove return best_score, best_move
def mc_trial(board, player): """ Takes a current board and the next player to move and plays a complete game. The input board is converted into the complete game. """ while not is_over(board): make_move(board, player) player = provided.switch_player(player)
def mc_trial(board, player): """ The function should play a game starting with the given player by making random moves, alternating between players. The function should return when the game is over. """ while True: # Pick a random empty square. row, col = random.choice(board.get_empty_squares()) # Mark the square with the current player. board.move(row, col, player) # Check if the player has won. If he did we break here. if board.check_win(): break # Change player. player = provided.switch_player(player)