Python 如何将alpha-beta修剪添加到minimax tic-tac-toe游戏中?

Python 如何将alpha-beta修剪添加到minimax tic-tac-toe游戏中?,python,algorithm,artificial-intelligence,minimax,alpha-beta-pruning,Python,Algorithm,Artificial Intelligence,Minimax,Alpha Beta Pruning,我正在用minimax算法制作一个无与伦比的tic-tac-toe程序。程序控件必须遍历树中的大量(超过200k)节点才能选择位置(显然,它会随着每次移动而减少)。但是,我想特别使用alpha-beta修剪使程序更快。我在互联网上查看了一些方法,但在我看来,我的代码似乎与这些方法不兼容 任何关于如何做到这一点的想法都会对我非常有用。这个计划是为了我今晚需要提交的作业,所以如果有人能帮我解决这个问题,那就太好了 下面是我为该程序编写的代码(在Python 3.6中) 提前谢谢 import sys

我正在用minimax算法制作一个无与伦比的tic-tac-toe程序。程序控件必须遍历树中的大量(超过200k)节点才能选择位置(显然,它会随着每次移动而减少)。但是,我想特别使用alpha-beta修剪使程序更快。我在互联网上查看了一些方法,但在我看来,我的代码似乎与这些方法不兼容

任何关于如何做到这一点的想法都会对我非常有用。这个计划是为了我今晚需要提交的作业,所以如果有人能帮我解决这个问题,那就太好了

下面是我为该程序编写的代码(在Python 3.6中)

提前谢谢

import sys
move = -1
n = 0
nodes = 0
def evaluateBoard(board):
    global n
    #Checking for rows
    cnt = 0
    for i in range(n):
        res = 0
        for j in range(n):
           res += board[cnt * n + j] 
        cnt += 1
        if res == n:
            return 1
        elif res == -n:
            return -1

    #Checking for columns
    for i in range(n):
        res = 0
        for j in range(n):
            res += board[i + n * j]
        if res == n:
            return 1
        elif res == -n:
            return -1

    #Checking for diagonals
    res = res2 = 0
    for i in range(n):
        res += board[i * (n + 1)]   
        res2 += board[(i + 1) * (n - 1)]
    if n in [res, res2]:
        return 1
    elif -n in [res, res2]:
        return -1

    return 0

def checkNonTerminal(board):
   for pos in board:
       if pos == 0:
           return 1
   return 0

def getScore(board, depth):
    if evaluateBoard(board) == 1:
        return 10 - depth
    elif evaluateBoard(board) == -1:
        return depth - 10
    else:
        return 0

def minimax(board, turn, depth, alpha, beta):
    global nodes
    if evaluateBoard(board) == 0 and checkNonTerminal(board) == 0:
        return getScore(board, depth)
    global move
    moves = list()
    scores = list()

    for square, pos in enumerate(board):
        if pos == 0:
            nodes += 1
            #print(board)
            new_board = board.copy()
            new_board[square] = turn
            moves.append(square)
            #print("Moves:", moves, "depth:", depth, "turn:", turn, checkNonTerminal(new_board) == 0)
            if evaluateBoard(new_board) in [1, -1] or checkNonTerminal(new_board) == 0:
                move = square
                return getScore(new_board, depth)
            scores.append(minimax(new_board, turn * -1, depth + 1, alpha, beta))
#    print("moves",moves)
#    print("scores", scores) 

    if turn == 1:
        move = moves[scores.index(max(scores))]
        return max(scores)
    elif turn == -1:
        move = moves[scores.index(min(scores))]
        return min(scores)

def displayBoard(board):
    global n
    for i in range(n):
        for j in range(n):
            if board[n*i+j] == 1:
                print("x",end=" ")
            elif board[n*i+j] == -1:
                print("o", end=" ")
            else:
                print(".", end = " ")
        print()
    print()

def main():      
    global n 
    global move
    n = 3
    first_turn = input("Would you like to go first (Y/N)?: ")
    if first_turn in ['Y', 'y']:
        first_turn = -1
        cnt = 1
    else:
        first_turn = 1
        cnt = 2
    board = [0] * 9
    displayBoard(board)
    while evaluateBoard(board) == 0 and checkNonTerminal(board) == 1:

        if cnt % 2 == 0:
            minimax(board, 1, 0)
            #print(score)
            board[move] = 1
            displayBoard(board)
        else:
            choice = eval(input("Enter your choice (1-9): "))
            if board[choice - 1] != 0:
                print("No cheating!")
                sys.exit([0])
            else:
                board[choice - 1] = -1
                displayBoard(board)
        cnt += 1

    if evaluateBoard(board) == 1:
        print("You lose!")
    elif evaluateBoard(board) == -1:
        print("You win!")
    else:
        print("It's a draw!")

    print(nodes,"nodes")

main()

在输入大小变得更易于管理之前,采用启发式方法可能更有意义,例如5!而不是9!。我认为搜索360000个可能性是非常低效的,因为你可以在开始时选择已知的最佳移动,然后从那里使用mini-max,从而有效地获得相同的结果。利用这个来了解alpha-beta算法。您的函数调用看起来很相似。这是我能找到的最好的伪代码,它甚至帮助了我。