【发布时间】:2020-07-19 04:51:08
【问题描述】:
我正在尝试使用 MinMax 为跳棋游戏编写 AI 算法。在最终测试之前一切都很好......电脑选择任何一块,然后他会来找我的家伙。他真想快点死。这是我的代码:
public MoveAndPoints MinMax(Dictionary<int, Field> gameBoard, string myColor, Boolean maximizingPlayer, int depth)
{
MoveAndPoints bestValue = new MoveAndPoints();
if (0 == depth)
{
return new MoveAndPoints(((myColor == enemyColor) ? 1 : -1) * evaluateGameBoard(gameBoard, myColor), bestValue.move);
}
MoveAndPoints val = new MoveAndPoints();
if (maximizingPlayer)
{
bestValue.points = int.MinValue;
foreach (Move move in GetPossibleMoves(gameBoard, myColor))
{
gameBoard = ApplyMove(gameBoard, move);
bestValue.move = move;
val = MinMax(gameBoard, Extend.GetEnemyPlayerColor(myColor), false, depth - 1);
bestValue.points = Math.Max(bestValue.points, val.points);
gameBoard = RevertMove(gameBoard, move);
}
return bestValue;
}
else
{
bestValue.points = int.MaxValue;
foreach (Move move in GetPossibleMoves(gameBoard, myColor))
{
gameBoard = ApplyMove(gameBoard, move);
val = MinMax(gameBoard, Extend.GetEnemyPlayerColor(myColor), true, depth - 1);
bestValue.points = Math.Min(bestValue.points, val.points);
gameBoard = RevertMove(gameBoard, move);
}
return bestValue;
}
}
这是我的启发:
public int evaluateGameBoard(Dictionary<int, Field> gameBoard, string color)
{
return Extend.GetNumberOfPieces(gameBoard, color) - Extend.GetNumberOfPieces(gameBoard, Extend.GetEnemyPlayerColor(color));
}
例如,我有这样的场景:
他选择死亡而不是移动任何其他地方:
如果我自己选择死亡,他只会杀了我,因为他必须这样做,因为这是他唯一的举动。 有人可以帮我解决这个问题吗?我认为问题出在 MinMax 内部。我几乎可以肯定这不是很成问题,但我真的找不到它:(
【问题讨论】:
-
查找“alpha beta pruning”
-
是的,我知道,但我想看看他,因为 MinMax 会以某种方式工作。
标签: c# algorithm artificial-intelligence minmax