即使得到一整列“X”我也没有赢。 下面给出的是代码
"""
Tic Tac Toe Player
"""
import math
import copy
X = "X"
O = "O"
EMPTY = None
def initial_state():
"""
Returns starting state of the board.
"""
return [[EMPTY, EMPTY, EMPTY],
[EMPTY, EMPTY, EMPTY],
[EMPTY, EMPTY, EMPTY]]
def player(board):
"""
Returns player who has the next turn on a board.
"""
countx = 0
counto = 0
for i in range(0, 3):
for j in range(0, 3):
if board[i][j] == X:
countx += 1
if board[i][j] == O:
counto += 1
if countx > counto:
return O
return X
def actions(board):
"""
Returns set of all possible actions (i, j) available on the board.
"""
allaction = set()
for i in range(0, 3):
for j in range(0, 3):
if board[i][j] == EMPTY:
allaction.add((i, j))
return allaction
def result(board, action):
"""
Returns the board that results from making move (i, j) on the board.
"""
current_player = player(board)
new_all_actions = actions(board)
if action not in new_all_actions:
raise Exception("move not in the set ")
i, j = action
copy_board = copy.deepcopy(board)
copy_board[i][j] = current_player
return copy_board
def check_row(board, player):
for i in range(0, 3):
if board[i][0] == player and board[i][1] == player and board[i][2] == player:
return True
return False
def check_column(board, player):
for i in range(len(board) - 1):
if board[0][i] == player and board[1][i] == player and board[2][i] == player:
return True
return False
def check_diagonals(board, player):
if board[0][0] == player and board[1][1] == player and board[2][2] == player:
return True
elif board[0][2] == player and board[1][1] == player and board[2][0] == player:
return True
return False
def winner(board):
"""
Returns the winner of the game, if there is one.
"""
if check_column(board, O) or check_row(board, O) or check_diagonals(board, O):
return O
if check_column(board, X) or check_row(board, X) or check_diagonals(board, X):
return X
return None
def terminal(board):
"""
Returns True if game is over, False otherwise.
"""
if winner(board) == X:
return True
elif winner(board) == O:
return True
for i in range(0, 3):
for j in range(0, 3):
if board[i][j] == EMPTY:
return False
return True
def utility(board):
"""
Returns 1 if X has won the game, -1 if O has won, 0 otherwise.
"""
if winner(board) == X:
return 1
if winner(board) == O:
return -1
else:
return 0
def max_value(board):
v = -math.inf
if terminal(board):
return utility(board)
for action in actions(board):
v = max(v, min_value(result(board, action)))
return v
def min_value(board):
v = math.inf
if terminal(board):
return utility(board)
for action in actions(board):
v = min(v, max_value(result(board, action)))
return v
def minimax(board):
if terminal(board) :
return None
elif player(board) == X:
action_storage = []
for action in actions(board):
action_storage.append([min_value(result(board, action)), action])
return sorted(action_storage, key=lambda x: x[0], reverse=True)[0][1]
# its returns min of max_value of all possible boards from the result
elif player(board) == O:
action_storage = []
for action in actions(board):
action_storage.append([max_value(result(board, action)), action])
return sorted(action_storage, key=lambda x: x[0])[0][1]
当我从 X 开始,甚至在获得以下板后 [[O,O,X],[None,X,X],[O,None,X]] 人工智能继续比赛并获胜
我在调试时尝试在 minimax 函数的终端函数调用处放置断点,假设该函数中可能存在错误但无法找到错误。即使得到三个 X,终端函数返回的布尔值也是假的。