我的程序使用极大极小算法来选择对井字游戏中的人类玩家最不利的移动。(这是有效的(
之后,我尝试实现阿尔法-贝塔修剪,以优化算法所需的时间,从而获得最佳移动。(这不起作用(
我知道为什么会引发错误,但我不明白为什么错误是由"alpha"one_answers"beta"引发的,而不是由"board"、"minX"或"minY"引发的。
我认为我声明的变量都应该是全局变量,是吗?
错误:UnboundLocalError:在分配之前引用了本地变量"beta">
#Global Variables
values = [0,1,2]
minX = 0
minY = 0
alpha = -999
beta = 999
global alpha
global beta
board = [["-" for x in range(3)] for y in range(3)]
#ausgabe
def ausgabe():
for zeile in range(3):
for spalte in range(3):
print("|"+str(board[zeile][spalte]),end="")
print("|")
#auswertung
#liefert 0, wenn O gewonnen hat
#liefert 2, wenn X gewonnen hat
#liefert 1, wenn es unentschieden ausgeht
#liefert -1 wenn noch nicht klar
def auswertung():
for i in ("X", "O"):
for x in range(3):
if board[x][0] == i and board[x][1] == i and board[x][2] == i:
return i
for y in range(3):
if board[0][y] == i and board[1][y] == i and board[2][y] == i:
return i
if board[0][0] == i and board[1][1] == i and board[2][2] == i:
return i
if board[0][2] == i and board[1][1] == i and board[2][0] == i:
return i
for zeile in range(3):
for spalte in range(3):
if board[zeile][spalte] == "-":
return -1
return 1
#max
def max():
temp = auswertung()
if temp != -1:
if temp == "X":
return 2
elif temp == "O":
return 0
else:
return temp
maximalwert = -999
for x in range(3):
for y in range(3):
if board[x][y] == "-":
board[x][y] = "X"
temp = alpha
alpha = beta
beta = temp
if alpha < beta:
break
wert = min()
board[x][y] = "-"
if wert > maximalwert:
maximalwert = wert
return maximalwert
#min
def min():
temp = auswertung()
if temp != -1:
if temp == "X":
return 2
elif temp == "O":
return 0
else:
return temp
minimalwert = 999
for x in range(3):
for y in range(3):
if board[x][y] == "-":
board[x][y] = "O"
temp = beta
beta = alpha
alpha = temp
if alpha > beta:
break
wert = max()
board[x][y] = "-"
if wert < minimalwert:
minimalwert = wert
return minimalwert
#wo befindet sich das min
def minWo():
temp = auswertung()
if temp != -1:
if temp == "X":
return 2
elif temp == "O":
return 0
else:
return temp
global minX
global minY
minimalwert = 999
for x in range(3):
for y in range(3):
if board[x][y] == "-":
board[x][y] = "O"
temp = beta
beta = alpha
alpha = temp
if alpha > beta:
break
wert = max()
board[x][y] = "-"
if wert < minimalwert:
minX = x
minY = y
minimalwert = wert
return minimalwert
def user_input():
while True:
try:
number1 = int(input("number1: "))
number2 = int(input("number2: "))
if number1 in values and number2 in values:
if board[number1][number2] == "-":
return number1, number2
else:
print("Invalid Input!ttry again")
except ValueError:
print("Invalid Input!ttry again")
def game_still_going():
winner = auswertung()
if winner == "X" or winner == "O":
print("Player "+winner+" won the game")
return False
elif winner == 1:
print("It's a draw")
return False
return True
def play_game():
while game_still_going():
number1,number2 = user_input()
board[number1][number2] = "X"
minWo()
board[minX][minY] = "O"
ausgabe()
play_game()
您应该将alpha各自的beta设置为-999和999,而不是maximurt,这应该有效。看看minimax的伪代码。