为什么TicTacToe minimax无法正常工作

问题描述

很久以前,我做了这个tictactoe,并决定今天添加AI选项。但是,对我而言,没有明显的原因,即使我从正常运行的javascript代码进行了转换,它也不起作用。

消除了对问题功能的不满

    def minimax(self,is_max):
        if self.won():
            return 1 if self.winner() == 'O' else -1

        if self.draw():
            return 0

        if is_max:
            best_score = float('-inf')
            for i in range(9):
                if self.valid_move(i):
                    self.board[i] = 'O'
                    score = self.minimax(False)
                    self.board[i] = ' '
                    best_score = max(best_score,score)
            return best_score
        else:
            best_score = float('inf')
            for i in range(9):
                if self.valid_move(i):
                    self.board[i] = 'X'
                    score = self.minimax(True)
                    self.board[i] = ' '
                    best_score = min(best_score,score)
            return best_score

    def best_move(self):
        best_score = float('-inf')
        move = 0
        for i in range(9):
            if self.valid_move(i):
                self.board[i] = 'O'
                score = self.minimax(False)
                self.board[i] = ' '

                if score > best_score:
                    best_score = score
                    move = i

        return move

    def move_ai(self):
        self.board[self.best_move()] = self.current_player()
        self.current_move += 1
        self.show_board()


解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)