在这些天里,我一直在努力使用 minimax 算法,我可以说我终于明白了(感谢 stackoverflow 中的另一篇文章)。因此,我打开了我的编辑器,并尝试将其实现为极其简单的(请不要责怪我的代码:P)井字游戏,只是为了尝试一下。一切正常,但计算机移动功能总是检索我-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