CS50-Tictactoe中的Minimax代码有什么问题?

时间:2020-05-27 01:27:41

标签: python cs50

像操作(板)或结果(板,动作),函数的功能是好的,有效的工作,basedes我的极小()方法,完美的作品时,我选择“O”作为球员,但是当选择“X” AI Minimax系统变得太奇怪了: *对不起,英语基本,但我是智利人(来自洛杉矶) 这是我的代码:

def minimax(board):
    """
    Returns the optimal action for the current player on the board.
    """
    # Defining AI
    current_player = player(board)
    # Randomizing first move
    if board == initial_state():
        a = random.randint(0, 2)
        b = random.randint(0, 2)
        return [a, b]

    # if the game is ended none action is valid
    if terminal(board):
        return None

    # Storing all possible actions
    Actions = actions(board)

    # If one action returns a terminal board then realize that
    for action in Actions:
         if terminal(result(board, action)):
             return action

    # Randomizing first move
    if board == initial_state():
        a = random.randint(0, 2)
        b = random.randint(0, 2)
        return [a, b]

    # X maximize the utility
    if current_player == X:
        v = -math.inf
        for action in Actions:
            k = min_value(result(board, action))
            if k > v:
                v = k
                move = action

    # O minimize the utility
    if current_player == O:
        v = math.inf
        for action in Actions:
            k = max_value(result(board, action))
            if k < v:
                v = k
                move= action

    return move


def max_value(board):
    if terminal(board):
        return utility(board)
    v = -math.inf
    for action in actions(board):
        v = max(v, min_value(result(board, action)))
    return v


def min_value(board):
    if terminal(board):
        return utility(board)
    v = math.inf
    for action in actions(board):
        v = min(v, max_value(result(board, action)))
    return v

0 个答案:

没有答案