我想在我的Abalone游戏中实现Minimax,但我不知道怎么做。确切地说,我不知道阿尔法何时需要最大或最小的球员。如果我已经理解了逻辑,我需要最小的球员和最大的人工智能?
这是维基百科的伪代码
function minimax(node, depth, maximizingPlayer)
if depth = 0 or node is a terminal node
return the heuristic value of node
if maximizingPlayer
bestValue := -∞
for each child of node
val := minimax(child, depth - 1, FALSE))
bestValue := max(bestValue, val);
return bestValue
else
bestValue := +∞
for each child of node
val := minimax(child, depth - 1, TRUE))
bestValue := min(bestValue, val);
return bestValue
(* Initial call for maximizing player *)
minimax(origin, depth, TRUE)以及我的实现
private Integer minimax(Board board, Integer depth, Color current, Boolean maximizingPlayer) {
Integer bestValue;
if (0 == depth)
return ((current == selfColor) ? 1 : -1) * this.evaluateBoard(board, current);
Integer val;
if (maximizingPlayer) {
bestValue = -INF;
for (Move m : board.getPossibleMoves(current)) {
board.apply(m);
val = minimax(board, depth - 1, current, Boolean.FALSE);
bestValue = Math.max(bestValue, val);
board.revert(m);
}
return bestValue;
} else {
bestValue = INF;
for (Move m : board.getPossibleMoves(current)) {
board.apply(m);
val = minimax(board, depth - 1, current, Boolean.TRUE);
bestValue = Math.min(bestValue, val);
board.revert(m);
}
return bestValue;
}
}和我的评估功能
private Integer evaluateBoard(Board board, Color player) {
return board.ballsCount(player) - board.ballsCount(player.other());
}发布于 2014-04-22 16:56:38
这取决于你的评估功能;在你的情况下,假设目标是比你的对手有更多的球,球员将是最大化& AI将最小化。
发布于 2014-04-22 17:07:11
在2人游戏中通常的方法是总是最大化,但当它被传递给父方时却否定了它的价值。
发布于 2020-01-28 06:58:31
您的评估功能不是非常有用的极小极大搜索,因为它将是常数的大多数移动的游戏。在Abalone的动作远没有国际象棋那么戏剧化。试着使用玩家所有弹珠之间的距离之和。这个函数给了minimax一些可以使用的东西。
您还需要确保selfColor是播放机在进行初始调用时要移动的颜色。
也可以编写递归结束。
if (0 == depth)
return this.evaluateBoard(board, selfColor);超出了问题的范围,但可能与您相关:我发现[医]消旋更容易使用。
https://stackoverflow.com/questions/23225540
复制相似问题