我正在尝试创建一个Tic-Tac-Toe游戏,允许用户对抗该程序 . 该程序将使用Minimax算法修剪决策树并输出最佳移动 . 下面我将代码发布到我正在开发Game Ai的类 . 函数“create_tree”返回从根节点开始的所有可能移动的决策树以及状态将发生的深度 . 我试图通过一个应用minimax修剪方法的模块来传递它,以找到应该进行的下一个最佳移动但是它不起作用,因为当我尝试评估游戏状态时,IDLE会抛出错误:

Traceback(最近一次调用最后一次):文件“C:\ Users \ Faisal \ Documents \ Python \ Comp4 project \ class_test3.py”,第87行,打印(node.minimax())文件“C:\ Users \ Faisal \ Documents \ Python \ Comp4 project \ class_test3.py“,第56行,在minimax中如果self.check_if_won(node [each])== 1:TypeError:list indices必须是整数,而不是list

我的代码如下:

Board = ['X', 'X', 3, 'O', 5, 'X', 7, 'O', 9]
whose_move = 1

depth = 0 
class Node():
    def __init__(self, Board, whose_move):
        self.Board = Board
        self.whose_move = whose_move
        self.win_comb = ((0,1,2), (3,4,5), (6,7,8), (6,3,0), (7,4,1), (8,5,2), (6,4,2), (8,4,0))
        self.piece = 'X'
        self.depth = depth

   def create_tree(self,piece=None,Board=None):
        if Board is None:
            Board = self.Board
            depth = self.depth
        if piece is None:
            piece = self.piece
        child_nodes = []
        for index, square in enumerate(Board):
            if square == index + 1:  # if square not taken
                new_board = Board[:]  # make a shallow copy
                new_board[index] = piece
                child_nodes.append(new_board)
                if self.check_if_won(new_board) == -1:   # removed self in arguments
                    if piece == 'X':
                        child_nodes.append(self.create_tree('O',new_board))
                    else:
                        child_nodes.append(self.create_tree('X',new_board))
        return [child_nodes]


    def check_if_won(self,Board=None):
        for each in self.win_comb:
            if (Board[each[0]] == Board[each[1]] and Board[each[1]]== Board[each[2]]):
                return 1
        for index,square in enumerate(Board):
            if square == index + 1:
                return -1
        return 0

    def who_won(Board=None):
        for each in self.win_comb:
            if(Board[each[0]]) == 'X':
               return 1
            else:
               return 0

    def minimax(self,node=None,player=None):
        best= None
        if node == None:
            node = self.create_tree()
        if player == None:
            player = 'X'
        for each in node:
                if self.check_if_won(node[each])== 1:
                    if self.who_won(node[each]) == 1:
                        return 1
                    elif self.who_won(node[each]) == 0:
                       return -1
                elif self.check_if_won(node[each]) == 0:
                       return 0
        for move in node:
            if player == 'X':
                val = self.minimax(node,'O')
            else:
                val = self.minimax(node,'X')
            if player == 'X':
                if val > best:
                    best = val
            else:
                if val<best:
                    best = val
        return best






node = Node(Board, whose_move)

n = node.create_tree()

print(n)

print(node.minimax())

当我运行程序时,我的create_tree()模块生成以下输出

[[['X', 'X', 'X', 'O', 5, 'X', 7, 'O', 9], ['X', 'X', 3, 'O', 'X', 'X', 7, 'O', 9], [[['X', 'X', 'O', 'O', 'X', 'X', 7, 'O', 9], [[['X', 'X', 'O', 'O', 'X', 'X', 'X', 'O', 9], [[['X', 'X', 'O', 'O', 'X', 'X', 'X', 'O', 'O']]], ['X', 'X', 'O', 'O', 'X', 'X', 7, 'O', 'X']]], ['X', 'X', 3, 'O', 'X', 'X', 'O', 'O', 9], [[['X', 'X', 'X', 'O', 'X', 'X', 'O', 'O', 9], ['X', 'X', 3, 'O', 'X', 'X', 'O', 'O', 'X']]], ['X', 'X', 3, 'O', 'X', 'X', 7, 'O', 'O'], [[['X', 'X', 'X', 'O', 'X', 'X', 7, 'O', 'O'], ['X', 'X', 3, 'O', 'X', 'X', 'X', 'O', 'O'], [[['X', 'X', 'O', 'O', 'X', 'X', 'X', 'O', 'O']]]]]]], ['X', 'X', 3, 'O', 5, 'X', 'X', 'O', 9], [[['X', 'X', 'O', 'O', 5, 'X', 'X', 'O', 9], [[['X', 'X', 'O', 'O', 'X', 'X', 'X', 'O', 9], [[['X', 'X', 'O', 'O', 'X', 'X', 'X', 'O', 'O']]], ['X', 'X', 'O', 'O', 5, 'X', 'X', 'O', 'X'], [[['X', 'X', 'O', 'O', 'O', 'X', 'X', 'O', 'X']]]]], ['X', 'X', 3, 'O', 'O', 'X', 'X', 'O', 9], [[['X', 'X', 'X', 'O', 'O', 'X', 'X', 'O', 9], ['X', 'X', 3, 'O', 'O', 'X', 'X', 'O', 'X'], [[['X', 'X', 'O', 'O', 'O', 'X', 'X', 'O', 'X']]]]]...

因此,可以理解错误产生的原因,但我不知道如何以系统的顺序提取树的不同深度的每个游戏状态 .