我最近报名参加了 CS50 AI python 课程,要做的项目之一是为井字棋游戏实现极小极大算法。我寻求帮助并搜索了 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 …Run Code Online (Sandbox Code Playgroud)