python - Minimax 算法 tic tac toe 不工作

标签 python algorithm artificial-intelligence minimax

我的 minimax 算法 tic tac toe AI 代码似乎无法正常工作,我也不知道为什么。回溯方面似乎有问题,如果移动导致损失则返回负值;它不区分防御 Action 与进攻 Action 。

它没有选择将 X 放在位置 6 以阻止对手连续到达 3,而是将它放在另一个牌上

board = [
        "X", "X", "O",
        "O", "O", "X",
        "-", "-", "-",
        ]

opp = "O"
plyr = "X"

def getOpenPos(board):
    openPos = []
    for index, state in enumerate(board):
        if state == "-":
            openPos.append(index)
    return openPos

def winning(board, plyr):
    if ((board[0] == plyr and board[1] == plyr and board[2] == plyr) or 
        (board[3] == plyr and board[4] == plyr and board[5] == plyr) or 
        (board[6] == plyr and board[7] == plyr and board[8] == plyr) or
        (board[0] == plyr and board[4] == plyr and board[8] == plyr) or
        (board[1] == plyr and board[4] == plyr and board[7] == plyr) or
        (board[2] == plyr and board[4] == plyr and board[6] == plyr) or
        (board[0] == plyr and board[3] == plyr and board[6] == plyr) or
        (board[2] == plyr and board[5] == plyr and board[8] == plyr)):
        return True
    else:
        return False 

def minimax(board, turn, FIRST):
    possibleMoves = getOpenPos(board)
    #check if won
    if (winning(board, opp)):
        return -10
    elif (winning(board, plyr)):
        return 10

    scores = []

    #new board created for recursion, and whoevers turn it is
    for move in possibleMoves:
        newBoard = board
        newBoard[move] = turn


        if (turn == plyr):
            scores.append( [move,minimax(newBoard, opp, False)] )
        elif (turn == opp):
            scores.append( [move, minimax(newBoard, plyr, False)] )

    #collapse recursion by merging all scores to find optimal position
    #see if there is a negative value (loss) and if there is its a -10
    if not FIRST:
        bestScore = 0
        for possibleScore in scores:
            move = possibleScore[0]
            score = possibleScore[1]
            if score == -10:
                return -10
            else:
                if score > bestScore:
                    bestScore = score
        return bestScore

    else:
        bestMove, bestScore = 0, 0
        for possibleScore in scores:
            move = possibleScore[0]
            score = possibleScore[1]
            if score > bestScore:
                bestMove = move
                bestScore = score

        #returns best position
        return bestMove



print(minimax(board, plyr, True))

最佳答案

我发现您的代码存在两个问题。如果你修复它们,在这种情况下你至少会得到 6

第一个问题是 newBoard = board 这行实际上并没有复制列表,它只是复制了引用。这可以通过将其更改为 newBoard = board[:] 来解决。

第二个问题是 bestScore 的起始值实际上并未超出预期范围,因此您不会每次都获得 bestIndex 的值。我将 bestMove, bestScore = 0, 0 更改为 bestMove, bestScore = 0, -11,它似乎对我有用。

关于python - Minimax 算法 tic tac toe 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50192878/

相关文章:

algorithm - 推荐关联项目的推荐算法

python - NetworkX 访问具有多个节点属性的节点

python - docker -py : How to bind an IP address to a container

algorithm - Big-O 时间复杂度,嵌套 for 和 while 循环

python - Raspberry Pi Google 助理问题

c++ - 从异或神经网络到图像识别

python - 计算单独的相关性,按列值分组

python - 为什么PIL与Pytorch经常使用?

algorithm - 一个人对一堆纸牌进行物理分类的最佳策略是什么?

algorithm - 如何找到图像中最密集的区域?