[英]I was trying to implement the Minimax Algorithm to tic tac toe in python but it just doesn't work
所以我試圖在 python 中實現 Minimax 算法來進行井字游戲,但它不起作用,我不知道為什么。 這是我以 X 開始,程序以 O 開始時的版本。我有一個名為 game_state 的數組,其中涵蓋了每個字段,如果 X 存在則為 1,如果存在則為 2,如果為空則為 0。 如果 X 贏了,我的 function checkwin() 返回 1,如果 O 贏了,則返回 -1,如果平局則返回 0,如果游戲沒有結束,則返回 None。 Point 是一種用於保存移動值並在代碼中進一步顯示 O 的結構,但這似乎完美無缺。
我在這里稱它為:
def best_move():
global game_state
best_score = math.inf
for i in range(3):
for j in range(3):
if game_state[i][j] == 0:
game_state[i][j] = 2
score = minimax(game_state, 0, False)
game_state[i][j] = 0
if score < best_score:
best_score = score
move = Point(i, j, i*3+j)
game_state[move.i][move.j] = 2
return move.number
這就是我的極小值 function:
def minimax(game_state, depth, is_maximizing):
result = checkwin(game_state)
if result is not None:
return result
if is_maximizing:
best_score = -math.inf
for i in range(3):
for j in range(3):
if game_state[i][j] == 0:
game_state[i][j] = 1
score = minimax(game_state, depth+1, False)
game_state[i][j] = 0
best_score = max(score, best_score)
return best_score
else:
best_score = math.inf
for i in range(3):
for j in range(3):
if game_state[i][j] == 0:
game_state[i][j] = 2
score = minimax(game_state, depth+1, True)
game_state[i][j] = 0
best_score = min(score, best_score)
return best_score
您正在最大化 minimax function 中的變量 best_score。 而您正在最小化 best_move function 中的 best_score,這是矛盾的。
您還應該在 best_move function 中最大化 best_score:
def best_move():
global game_state
best_score = -math.inf #This is changed
for i in range(3):
for j in range(3):
if game_state[i][j] == 0:
game_state[i][j] = 2
score = minimax(game_state, 0, False)
game_state[i][j] = 0
if score > best_score: #This is changed
best_score = score
move = Point(i, j, i*3+j)
game_state[move.i][move.j] = 2
return move.number
同樣在 best_move 中,玩家是 2('O'),並且您在 minimax 中將 'False' 作為 minimax(game_state, depth+1, False) 傳遞。 這將運行同樣屬於玩家 2('O') 的 minimax function 中的 else 語句。 這也是一個錯誤。 你應該使用:
def minimax(game_state, depth, is_maximizing):
result = checkwin(game_state)
if result is not None:
return result
if is_maximizing:
best_score = -math.inf
for i in range(3):
for j in range(3):
if game_state[i][j] == 0:
game_state[i][j] = 2 #This is changed
score = minimax(game_state, depth+1, False)
game_state[i][j] = 0
best_score = max(score, best_score)
return best_score
else:
best_score = math.inf
for i in range(3):
for j in range(3):
if game_state[i][j] == 0:
game_state[i][j] = 1 #This is changed
score = minimax(game_state, depth+1, True)
game_state[i][j] = 0
best_score = min(score, best_score)
return best_score
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.