我的 Minimax 算法出了什么问题?

What is going wrong with my Minimax Algorithm?

我尝试使用 a tutorial

制作一个 MiniMax AI

AI 不起作用,每次都在底部行上构建,报告列索引 0, 1, 2, 3, 4, 5, 6,然后是下一行,依此类推。与我的和 the tutorials working version was the depth of the minimax algorithm as it goes along. Where this was the depth of the working one as it recurred, and this 相比,我在代码输出中发现的唯一不同之处在于我的代码输出的深度(它比这更长,但我的控制台没有保留所有内容)。我已经尝试了很多方法来使其正常工作,包括将棋盘系统重新设计为列表中的多个列表以及许多其他操作,例如删除部分代码和重新设计分数,但它似乎没有改变。

我的极小极大函数是:

def minimax(board, depth, alpha, beta, maximizingPlayer):
    print (depth)
    validLocations = getValidLocations(board)
    isTerminal = isTerminalNode(board)
    if depth == 0 or isTerminal:
        if isTerminal:
            if checkWin(board, computerDisc):
                return (None, math.inf)
            elif checkWin(board, playerDisc):
                return (None, -math.inf)
            else: # Game is over, no more spaces
                return (None, 0)
        else: # Depth is zero
            # print ("THIS IS DEPTH 0")
            return (None, scorePos(board, computerDisc))

    if maximizingPlayer:
        value = -math.inf
        column = random.choice(validLocations)
        for c in validLocations:
            boardCopy = copy.deepcopy(board)
            dropPiece(boardCopy, c, computerDisc)
            newScore = minimax(boardCopy, depth-1, alpha, beta, False)[1]
            if newScore > value:
                value = newScore
                column = c
            alpha = max(alpha, value)
            if alpha >= beta:
                break
        return (column, value)

    else: # Minimizing player
        value = math.inf
        column = random.choice(validLocations)
        for c in validLocations:
            boardCopy = copy.deepcopy(board)
            dropPiece(boardCopy, c, playerDisc)
            newScore = minimax(boardCopy, depth-1, alpha, beta, True)[1]
            if newScore < value:
                value = newScore
                column = c
            beta = min(beta, value)
            if alpha >= beta:
                break

        return (column, value)

教程功能是:

def minimax(board, depth, alpha, beta, maximizingPlayer):
valid_locations = get_valid_locations(board)
is_terminal = is_terminal_node(board)
if depth == 0 or is_terminal:
    if is_terminal:
        if winning_move(board, AI_PIECE):
            return (None, 100000000000000)
        elif winning_move(board, PLAYER_PIECE):
            return (None, -10000000000000)
        else: # Game is over, no more valid moves
            return (None, 0)
    else: # Depth is zero
        return (None, score_position(board, AI_PIECE))
if maximizingPlayer:
    value = -math.inf
    column = random.choice(valid_locations)
    for col in valid_locations:
        row = get_next_open_row(board, col)
        b_copy = board.copy()
        drop_piece(b_copy, row, col, AI_PIECE)
        new_score = minimax(b_copy, depth-1, alpha, beta, False)[1]
        if new_score > value:
            value = new_score
            column = col
        alpha = max(alpha, value)
        if alpha >= beta:
            break
    return column, value

else: # Minimizing player
    value = math.inf
    column = random.choice(valid_locations)
    for col in valid_locations:
        row = get_next_open_row(board, col)
        b_copy = board.copy()
        drop_piece(b_copy, row, col, PLAYER_PIECE)
        new_score = minimax(b_copy, depth-1, alpha, beta, True)[1]
        if new_score < value:
            value = new_score
            column = col
        beta = min(beta, value)
        if alpha >= beta:
            break
    return column, value

当我的不起作用时,我试图让它尽可能接近教程功能,但它似乎仍然不起作用。我需要做什么才能让它起作用?

完整节目: 我的:https://repl.it/@MyloBishop/Connect-4 教程:https://github.com/KeithGalli/Connect4-Python/blob/master/connect4_with_ai.py

棋盘作为参数 node 传递给您的 minimax 函数,但在函数内部您使用 board

参考程序: def minimax(棋盘、深度、alpha、beta、maximizingPlayer):

您的程序: def minimax(node, depth, alpha, beta, maximizingPlayer):

因此,您的 递归 minimax 函数未按预期工作。

编辑: 要修复此问题,请将 minimax 中的 board 替换为 node(不要将函数定义中的 node 更改为棋盘)

编辑 2: 还要检查函数 scorePos - 你有一个硬编码的 computerDisc 而不是使用函数参数。

编辑 3: 此外,其他辅助函数如 isBoardFull() 和 isSpaceFree() 应该在棋盘副本上运行,而不是全局变量。