C# 理解Minimax算法
我正在尝试为两人8x8棋盘游戏创建AI对手。经过一番研究,我发现极大极小算法足够方便地完成这项工作。我正在创建的AI对手将与其他AI对手或人类对抗 我对理解极小极大算法有疑问 我正试图创建一个AI对手,但网络上的解释说,我需要为两个玩家(最小玩家和最大玩家)编写代码,正如我从下面的伪代码中所理解的那样C# 理解Minimax算法,c#,algorithm,artificial-intelligence,minimax,alpha-beta-pruning,C#,Algorithm,Artificial Intelligence,Minimax,Alpha Beta Pruning,我正在尝试为两人8x8棋盘游戏创建AI对手。经过一番研究,我发现极大极小算法足够方便地完成这项工作。我正在创建的AI对手将与其他AI对手或人类对抗 我对理解极小极大算法有疑问 我正试图创建一个AI对手,但网络上的解释说,我需要为两个玩家(最小玩家和最大玩家)编写代码,正如我从下面的伪代码中所理解的那样 MinMax (GamePosition game) { return MaxMove (game); } MaxMove (GamePosition game) { if (GameE
MinMax (GamePosition game) {
return MaxMove (game);
}
MaxMove (GamePosition game) {
if (GameEnded(game)) {
return EvalGameState(game);
}
else {
best_move < - {};
moves <- GenerateMoves(game);
ForEach moves {
move <- MinMove(ApplyMove(game));
if (Value(move) > Value(best_move)) {
best_move < - move;
}
}
return best_move;
}
}
MinMove (GamePosition game) {
best_move <- {};
moves <- GenerateMoves(game);
ForEach moves {
move <- MaxMove(ApplyMove(game));
if (Value(move) > Value(best_move)) {
best_move < - move;
}
}
return best_move;
}
MinMax(游戏位置游戏){
返回MaxMove(游戏);
}
MaxMove(游戏位置游戏){
如果(游戏结束(游戏)){
返回EvalGameState(游戏);
}
否则{
最佳移动<-{};
移动您只需在最坏的情况下为两名玩家搜索最佳解决方案,这就是为什么它被称为minmax,您不需要更多:
function minimax( node, depth )
if node is a terminal node or depth <= 0:
return the heuristic value of node
α = -∞
foreach child in node:
α = max( a, -minimax( child, depth-1 ) )
return α
最好先使用没有很多位置的游戏(例如tic tac toe).人工智能必须想象人类玩家将如何应对自己的移动。你不必。我见过的一些实现使用单个BestMove函数,只需翻转分数符号。但是,如果你想用Alpha-Beta修剪来增加MiniMax,它可能会变得混乱。
function negamax( node, depth, α, β, color )
if node is a terminal node or depth = 0
return color * the heuristic value of node
foreach child of node
value = -negamax( child, depth-1, -β, -α, -color )
if value ≥ β
return value /** Alpha-Beta cut-off */
if value ≥ α
α = value
return α