井字游戏的极小极大算法仅在下一个可用方块中播放

问题描述 投票:0回答:1

我正在尝试学习极小极大算法。我的极小极大算法不起作用,计算机只是在下一个可用的方块中打“O”。我的整个源代码如下(抱歉文字墙)。 bestScore 变量保持其最初设置的值(1000 或 -1000,具体取决于它是最大化还是最小化),并且它永远不会返回基本情况之一。不知道从这里去哪里

from json.encoder import INFINITY
from itertools import combinations

from pygame.examples import grid


class GameState():
    def __init__(self):
        self.grid = [["-","-","-"],
                     ["-","-","-"],
                     ["-","-","-"]]

        self.MagicSquare = [[4, 9, 2],
                            [3, 5, 7],
                            [8, 1, 6]]

        self.XtoMove = True
        self.gameOver = False
        self.Xsum = []
        self.Osum = []
        self.Xwins = False
        self.Owins = False
        self.draw = False

    def makeMove(self, row, col):
        totalX = 0
        totalO = 0


        if self.grid[row][col] == "-":

            if self.XtoMove == True:

                self.grid[row][col] = 'X'
                self.bestMove()
                #self.XtoMove = False
                self.Xsum.append(self.MagicSquare[row][col])

                for combo in combinations(self.Xsum, 3):
                    if sum(combo) == 15:
                        self.gameOver = True
                        self.Xwins = True

            if len(self.Xsum) + len(self.Osum) == 9:
                self.gameOver = True
                self.draw = True



    def checkDraw(self):
        if len(self.Xsum) + len(self.Osum) == 9:
            self.gameOver = True
            self.draw = True
            return True

    def checkWinner(self, player):

        if player == 'X':
            sums = self.Xsum
        else:
            sums = self.Osum

        for combo in combinations(sums, 3):
            if sum(combo) == 15:
                return True
        return False


    def minimax(self, grid, depth, isMaximising):
        if self.checkWinner("O"):
            return 1
        elif self.checkWinner("X"):
            return -1
        elif self.checkDraw():
            return 0

        if isMaximising:
            bestScore = -1000
            for r in range(3):
                for c in range(3):
                    if grid[r][c] == "-":
                        grid[r][c] = 'O'
                        score = self.minimax(grid, depth + 1, False)
                        grid[r][c] = "-"
                        bestScore = max(score, bestScore)

            return bestScore

        else:
            bestScore = 1000
            for r in range(3):
                for c in range(3):
                    if grid[r][c] == "-":
                        grid[r][c] = 'X'
                        score = self.minimax(grid, depth + 1, True)
                        grid[r][c] = "-"
                        bestScore = min(score, bestScore)
            return bestScore

    def bestMove(self):
        bestScore = -10000
        move = (-1, -1)
        for r in range(3):
            for c in range(3):
                if self.grid[r][c] == "-":
                    self.grid[r][c] = 'O'
                    score = self.minimax(self.grid, 0, False)
                    self.grid[r][c] = "-"
                    if score > bestScore:
                        bestScore = score
                        move = (r, c)

        if move != (-1,-1):
            self.grid[move[0]][move[1]] = 'O'
            self.Osum.append(self.MagicSquare[move[0]][move[1]])

            for combo in combinations(self.Osum, 3):
                if sum(combo) == 15:
                    self.gameOver = True
                    self.Owins = True

        if len(self.Xsum) + len(self.Osum) == 9 and not self.Owins:
            self.gameOver = True
            self.draw = True
python minimax
1个回答
0
投票

minimax
函数使用参数中的网格,但
bestMove
使用
self.grid
。我认为正是这种缺乏同步性可能导致了您的问题。我认为
self.grid
没有被更新,而只是参数
grid
然后没有跟进。

我希望这有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.