我尝试使用一个教程制作了一个MiniMax AI,
AI无法正常工作,只是在底行移动并每次向上构建,报告的列索引为0, 1, 2, 3, 4, 5, 6
,然后是下一行,依此类推。我发现与我的代码输出相比,教程的工作版本中唯一明显不同的是Minimax算法的深度。在递归过程中,这个是工作版本的深度,而这个是我的版本的深度(实际上比这更长,但我的控制台没有保留所有内容)。我尝试了很多方法来使其工作,包括重新设计棋盘系统为列表中的多个列表,以及删除代码的某些部分和重新设计分数系统,但似乎都没有改变。
我的minimax函数是:
def minimax(board, depth, alpha, beta, maximizingPlayer): print (depth) validLocations = getValidLocations(board) isTerminal = isTerminalNode(board) if depth == 0 or isTerminal: if isTerminal: if checkWin(board, computerDisc): return (None, math.inf) elif checkWin(board, playerDisc): return (None, -math.inf) else: # 游戏结束,没有更多空间 return (None, 0) else: # 深度为零 # print ("THIS IS DEPTH 0") return (None, scorePos(board, computerDisc)) if maximizingPlayer: value = -math.inf column = random.choice(validLocations) for c in validLocations: boardCopy = copy.deepcopy(board) dropPiece(boardCopy, c, computerDisc) newScore = minimax(boardCopy, depth-1, alpha, beta, False)[1] if newScore > value: value = newScore column = c alpha = max(alpha, value) if alpha >= beta: break return (column, value) else: # 最小化玩家 value = math.inf column = random.choice(validLocations) for c in validLocations: boardCopy = copy.deepcopy(board) dropPiece(boardCopy, c, playerDisc) newScore = minimax(boardCopy, depth-1, alpha, beta, True)[1] if newScore < value: value = newScore column = c beta = min(beta, value) if alpha >= beta: break return (column, value)
教程的函数是:
def minimax(board, depth, alpha, beta, maximizingPlayer):valid_locations = get_valid_locations(board)is_terminal = is_terminal_node(board)if depth == 0 or is_terminal: if is_terminal: if winning_move(board, AI_PIECE): return (None, 100000000000000) elif winning_move(board, PLAYER_PIECE): return (None, -10000000000000) else: # 游戏结束,没有更多有效移动 return (None, 0) else: # 深度为零 return (None, score_position(board, AI_PIECE))if maximizingPlayer: value = -math.inf column = random.choice(valid_locations) for col in valid_locations: row = get_next_open_row(board, col) b_copy = board.copy() drop_piece(b_copy, row, col, AI_PIECE) new_score = minimax(b_copy, depth-1, alpha, beta, False)[1] if new_score > value: value = new_score column = col alpha = max(alpha, value) if alpha >= beta: break return column, valueelse: # 最小化玩家 value = math.inf column = random.choice(valid_locations) for col in valid_locations: row = get_next_open_row(board, col) b_copy = board.copy() drop_piece(b_copy, row, col, PLAYER_PIECE) new_score = minimax(b_copy, depth-1, alpha, beta, True)[1] if new_score < value: value = new_score column = col beta = min(beta, value) if alpha >= beta: break return column, value
当我的版本无法工作时,我尝试将其尽可能接近教程的函数,但它仍然似乎不起作用。我需要做些什么才能让它工作?
完整程序:我的:https://repl.it/@MyloBishop/Connect-4教程:https://github.com/KeithGalli/Connect4-Python/blob/master/connect4_with_ai.py
回答:
棋盘作为参数node传递给你的minimax函数,但在函数内部你使用的是board。
参考程序:def minimax(board, depth, alpha, beta, maximizingPlayer):
你的程序:def minimax(node, depth, alpha, beta, maximizingPlayer):
因此,你的递归 minimax函数无法按预期工作。
编辑:要修复这个问题,请将minimax函数内部的board替换为node(不要将函数定义中的node更改为board)
编辑2:还请检查scorePos函数 – 你使用了硬编码的computerDisc,而不是使用函数参数。
编辑3:此外,其他辅助函数如isBoardFull()和isSpaceFree()应该操作棋盘的副本,而不是全局变量。