Python 从minimax到Alpa-beta修剪minimax的变化
我发现了一个3乘3的tic-tac-toe游戏的minimax代码,我想把它改成Alpha-beta minimax,因为我计划创建一个6乘6的tic-tac-toe 以下是源代码:Python 从minimax到Alpa-beta修剪minimax的变化,python,algorithm,tic-tac-toe,minimax,alpha-beta-pruning,Python,Algorithm,Tic Tac Toe,Minimax,Alpha Beta Pruning,我发现了一个3乘3的tic-tac-toe游戏的minimax代码,我想把它改成Alpha-beta minimax,因为我计划创建一个6乘6的tic-tac-toe 以下是源代码: def minimax(状态、深度、玩家): """ AI功能,选择最好的动作 :param state:电路板的当前状态 :param depth:树中的节点索引(0 def minimax(state, depth, player): """ AI function
def minimax(状态、深度、玩家):
"""
AI功能,选择最好的动作
:param state:电路板的当前状态
:param depth:树中的节点索引(0
def minimax(state, depth, player):
"""
AI function that choice the best move
:param state: current state of the board
:param depth: node index in the tree (0 <= depth <= 9),
but never nine in this case (see iaturn() function)
:param player: an human or a computer
:return: a list with [the best row, best col, best score]
"""
if player == COMP:
best = [-1, -1, -infinity]
else:
best = [-1, -1, +infinity]
if depth == 0 or game_over(state):
score = evaluate(state)
return [-1, -1, score]
for cell in empty_cells(state):
x, y = cell[0], cell[1]
state[x][y] = player
score = minimax(state, depth - 1, -player)
state[x][y] = 0
score[0], score[1] = x, y
if player == COMP:
if score[2] > best[2]:
best = score # max value
else:
if score[2] < best[2]:
best = score # min value
return best
def minimax(state, depth,alpha,beta, player):
"""
AI function that choice the best move
:param state: current state of the board
:param depth: node index in the tree (0 <= depth <= 9),
but never nine in this case (see iaturn() function)
:param player: an human or a computer
:return: a list with [the best row, best col, best score]
"""
if depth == 0 or game_over(state):
score = evaluate(state)
return [-1, -1, score]
if player == COMP:
best = [-1, -1, -infinity]
else:
best = [-1, -1, +infinity]
for cell in empty_cells(state):
x, y = cell[0], cell[1]
state[x][y] = player
score = minimax(state, depth - 1,alpha,beta, -player)
state[x][y] = 0
score[0], score[1] = x, y
if player == COMP:
if score[2] > best[2]:
best = score # max value
alpha = max(alpha, score[2]) #added alpha
if beta <= alpha:
break
else:
if score[2] < best[2]:
best = score # min value
beta= min(beta, score[2]) #added beta
if beta <= alpha:
break
return best