我正在制作一个井字游戏。这个游戏有两种模式。一种是双人模式,另一种是与AI对战的模式。双人模式运行得很好,但AI模式却无法正常工作。AI模式会在选中特定复选框时启用,并且其中一个回合由AI执行。我使用了极小化极大算法来实现AI的执行。由于这是我第一次使用GUI(Tkinter)制作游戏,我不明白代码哪里出了问题。请有人给我一些关于哪里出错的见解,或者请帮我修正代码。提前感谢。
from tkinter import *from copy import deepcopyimport randomgame = Tk()game.title("TIC TAC TOE")game.geometry("450x555")#game.configure(bg = '#b3b3b3')with_AI = Label(text = "Want to play with AI:", font = ("Helvatica", 15), fg = "#000000")with_AI.grid(row = 0, column = 0, columnspan = 2, sticky = 'e')yes_AI = IntVar()Checkbutton(game, variable = yes_AI).grid(row = 0, column = 2, sticky = 'w')textPlay = Label(text = "Start the Game!!!", font = ("Helvatica", 15), fg = "red")textPlay.grid(row = 1, column = 0, columnspan = 3)temp = random.choice([0, 1])if temp == 0: player = 'O'else: player = 'X'stop_game = Falsedef minimax(states, depth, isMax, playerAI, scores): result = checkWinner1() if result != None: return scores[result] if isMax: bestVal = -1000 for i in range(3): for j in range(3): if states[i][j] == 0: states[i][j] = playerAI moveVal = minimax(states, depth + 1, False, playerAI, scores) states[i][j] = 0 bestVal = max(moveVal, bestVal) return bestVal else: bestVal = 1000 for i in range(3): for j in range(3): if states[i][j] == 0: if playerAI == 'X': states[i][j] = 'O' else: states[i][j] = 'X' moveVal = minimax(states, depth + 1, True, playerAI, scores) states[i][j] = 0 bestVal = min(moveVal, bestVal) return bestValdef bestMove(states, playerAI, scores): checkWinner1() bestVal = -1000 for i in range(3): for j in range(3): if states[i][j] == 0: moveVal = playerAI moveVal = minimax(states, 0, False, playerAI, scores) states[i][j] = 0 if moveVal > bestVal: bestRow = i bestColumn = j bestVal = moveVal return bestRow, bestColumndef callback(r, c): global player global textPlay global states if player == 'X' and states[r][c] == 0 and stop_game == False: board[r][c].configure(text = 'X', fg = '#f64c72') states[r][c] = 'X' player = 'O' textPlay.config(text = "O's turn") if yes_AI.get() == 1: scores = {'X':-1, 'O':1, 'tie':0} bestRow, bestColumn = bestMove(states, player, scores) board[bestRow, bestColumn].configure(text = 'O', fg = '#f64c72') states[bestRow, bestColumn] = 'O' textPlay.config(text = "X's turn") if player == 'O' and states[r][c] == 0 and stop_game == False: board[r][c].configure(text = 'O', fg = '#f64c72') states[r][c] = 'O' player = 'X' textPlay.config(text = "X's turn") if yes_AI.get() == 1: scores = {'X':1, 'O':-1, 'tie':0} bestRow, bestColumn = bestMove(states, player, scores) board[bestRow, bestColumn].configure(text = 'X', fg = '#f64c72') states[bestRow, bestColumn] = 'X' textPlay.config(text = "O's turn") checkWinner()def checkWinner1(): global stop_game global states win = None win_color = '#85d139' for i in range(3): if states[i][0] == states[i][1] == states[i][2] != 0: if states[i][0] == 'X': win = 'X' else: win = 'O' for i in range(3): if states[0][i] == states[1][i] == states[2][i] != 0: if states[0][i] == 'X': win = 'X' else: win = 'O' if states[0][0] == states[1][1] == states[2][2] != 0: if states[1][1] == 'X': win = 'X' else: win = 'O' if states[2][0] == states[1][1] == states[0][2] != 0: if states[1][1] == 'X': win = 'X' else: win = 'O' temp1 = 0 for i in range(3): for j in range(3): if states[i][j] == 0: temp1 = 1 if temp1 == 0: textPlay.configure(text = "It's a tie! Click on 'Reset' to play again.") win = 'tie' return win
回答:
在callback函数中,在第二个’if条件’内,你应该添加:
player = 'X'
在下一个条件中添加player = ‘O’,以便切换玩家。