在这些日子里,我一直在努力使用minimax算法,我可以说我终于理解了它(感谢stackoverflow中的另一篇文章)。因此,我打开了我的编辑器,我试图将它变成一个非常简单的(请不要责怪我代码:P)tic tac toe,只是试一试。一切正常,但计算机移动功能总是检索我-1。我不是要你给我代码,只是为什么这样做。我已多次搜索代码,但一无所获。代码可能与我在网上找到的另一个代码非常相似。这是我的代码:
# COMPUTER AI
def computer_move():
best_move = minimax_recurse(game_board,active_player, 0)
print "The best move is ", best_move
make_move(game_board,best_move, active_player)
print "COMPUTER MOVE DONE"
def minimax_recurse(game_board,player,depth):
winner = is_winner(game_board)
if winner == active_player :
return 1
elif winner is not active_player :
return -1
elif len(get_move_list(game_board)) == 0 :
return 0
if player == player1 :
other_player = player2
other_player = player1
if player == active_player :
alpha = -1
alpha = 1
movelist = get_move_list(game_board)
for move in movelist :
board2 = game_board
make_move(board2,move,player)
subalpha = minimax_recurse(board2, other_player, depth + 1)
if player == active_player :
if depth == 0 and alpha <= subalpha:
best_move = move
alpha = max(alpha,subalpha)
return alpha
else :
alpha = min(alpha,subalpha)
return alpha
# BOARD FUNCTIONS
game_board = ([1,2,3],[4,5,6],[7,8,9])
def print_board(board) :
for row in board :
print row
def make_move(game_board,player_move,active_player):
x = 0
y = 0
player_move = int(player_move)
if player_move == 1 :
x = 0
y = 0
elif player_move == 2 :
x = 0
y = 1
elif player_move == 3 :
x = 0
y = 2
elif player_move == 4 :
x = 1
y = 0
elif player_move == 5 :
x = 1
y = 1
elif player_move == 6 :
x = 1
y = 2
elif player_move == 7 :
x = 2
y = 0
elif player_move == 8 :
x = 2
y = 1
elif player_move == 9 :
x = 2
y = 2
elif player_move >= 10 :
print "value is too high"
skip = False
return board
if game_board[x][y] == "O" or game_board[x][y] == "X" :
print "move not avaiable"
return game_board
game_board[x][y] = active_player
return game_board
def is_winner(board):
for i in range (0,3) :
if board[i][0] == player1 and board[i][1] == player1 and board[i][2] == player1 :
return player1
if board[i][0] == player2 and board[i][1] == player2 and board[i][2] == player2 :
return player2
# checking for obliqual, that's quite bad and slow check but it works
if board[0][0] == player1 and board[1][1] == player1 and board[2][2] == player1 :
return player1
if board[0][0] == player2 and board[1][1] == player2 and board[2][2] == player2 :
return player2
if board[2][0] == player1 and board[1][1] == player1 and board[0][2] == player1 :
return player1
if board[2][0] == player2 and board[1][1] == player2 and board[0][2] == player2 :
return player2
return None
def get_move_list (game_board) :
move = [0]
for row in game_board :
for i in row :
if isinstance(i,int) == True :
move.append(i)
move.remove(0)
return move
# Main Loop
player1 = "X"
player2 = "O"
print_board(game_board)
while True :
active_player = player1
# this is for player move
print get_move_list(game_board)
player_move = int(raw_input("Please insert your move >>> "))
make_move(game_board,player_move,active_player)
print_board(game_board)
if is_winner(game_board) == player1 :
print "Player1 is the winner"
break
if is_winner(game_board) == player2 :
print "Player2 is the winner"
break
print get_move_list(game_board)
# computer time
active_player = player2
computer_move()
print_board(game_board)
if is_winner(game_board) == player1 :
print "Player1 is the winner"
break
if is_winner(game_board) == player2 :
print "Player2 is the winner"
break
答案 0 :(得分:2)
在没有调试所有代码的情况下,一件似乎错误的事情是你使用“best_move”变量(全局未初始化)作为移动的持有者以及minimax_recurse的-1 / 0 / + 1结果的持有者。所以它被你的minmax算法覆盖了。您需要更多变量,更清晰的初始化以及一致的使用。
答案 1 :(得分:1)
始终返回-1的根本原因是winner is not active_player
在True
为winner
时返回None
。您可以使用变量来跟踪其他(非活动)播放器,也可以使用三元运算符:elif winner is (player1 if player2 == active_player else player2)
虽然这不是唯一的问题:
if player == active_player :
alpha = -1
alpha = 1
这将始终将alpha设置为1.直接位于上方的行具有相同的问题。另一个答案中指出的变量命名问题也是如此。