作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我最近注册了 CS50 AI python 类(class),其中一个项目是为 tictactoe 游戏实现一个极小极大算法。我寻求帮助并搜索了 stackoverflow,但没有找到可以帮助我的答案。它的图形部分已经实现,您需要做的就是对模板的给定功能进行编程,我相信除了算法部分外,我都做对了,功能如下:
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.
"""
if board == initial_state():
return X
xcounter = 0
ocounter = 0
for row in board:
xcounter += row.count(X)
ocounter += row.count(O)
if xcounter == ocounter:
return X
else:
return O
def actions(board):
"""
Returns set of all possible actions (i, j) available on the board.
"""
possible_moves = []
for i in range(3):
for j in range(3):
if board[i][j] == EMPTY:
possible_moves.append([i, j])
return possible_moves
def result(board, action):
"""
Returns the board that results from making move (i, j) on the board.
"""
boardcopy = copy.deepcopy(board)
try:
if boardcopy[action[0]][action[1]] != EMPTY:
raise IndexError
else:
boardcopy[action[0]][action[1]] = player(boardcopy)
return boardcopy
except IndexError:
print('Spot already occupied')
def winner(board):
"""
Returns the winner of the game, if there is one.
"""
columns = []
# Checks rows
for row in board:
xcounter = row.count(X)
ocounter = row.count(O)
if xcounter == 3:
return X
if ocounter == 3:
return O
# Checks columns
for j in range(len(board)):
column = [row[j] for row in board]
columns.append(column)
for j in columns:
xcounter = j.count(X)
ocounter = j.count(O)
if xcounter == 3:
return X
if ocounter == 3:
return O
# Checks diagonals
if board[0][0] == O and board[1][1] == O and board[2][2] == O:
return O
if board[0][0] == X and board[1][1] == X and board[2][2] == X:
return X
if board[0][2] == O and board[1][1] == O and board[2][0] == O:
return O
if board[0][2] == X and board[1][1] == X and board[2][0] == X:
return X
# No winner/tie
return None
def terminal(board):
"""
Returns True if game is over, False otherwise.
"""
# Checks if board is full or if there is a winner
empty_counter = 0
for row in board:
empty_counter += row.count(EMPTY)
if empty_counter == 0:
return True
elif winner(board) is not None:
return True
else:
return False
def utility(board):
"""
Returns 1 if X has won the game, -1 if O has won, 0 otherwise.
"""
if winner(board) == X:
return 1
elif winner(board) == O:
return -1
else:
return 0
def minimax(board):
current_player = player(board)
if current_player == X:
v = -math.inf
for action in actions(board):
k = min_value(result(board, action)) #FIXED
if k > v:
v = k
best_move = action
else:
v = math.inf
for action in actions(board):
k = max_value(result(board, action)) #FIXED
if k < v:
v = k
best_move = action
return best_move
def max_value(board):
if terminal(board):
return utility(board)
v = -math.inf
for action in actions(board):
v = max(v, min_value(result(board, action)))
return v #FIXED
def min_value(board):
if terminal(board):
return utility(board)
v = math.inf
for action in actions(board):
v = min(v, max_value(result(board, action)))
return v #FIXED
最佳答案
首先谈谈调试:如果您要打印在递归调用中完成的计算,您可以跟踪问题的执行情况并快速找到答案。
但是,您的问题似乎在树的顶部。在您的 minimax 调用中,如果当前玩家是 X,则您调用该状态的每个 child 的 max_value,然后取该结果的最大值。但是,这在树的顶部应用了两次 max 函数。游戏中的下一个玩家是 O,因此您应该为下一个玩家调用 min_value 函数。
因此,在 minimax 调用中,如果 current_player 为 X,则应调用 min_value,如果 current_player 为 O,则应调用 max_value。
关于python - Python 中 Tictactoe 的极小极大算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61072185/
我是一名优秀的程序员,十分优秀!