我在尝试为跳棋游戏编写使用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里。我几乎可以肯定这不是一个很严重的问题,但我实在找不到 🙁
回答:
我没有你所有的代码,所以我无法完全验证这些问题。但我相信至少你的一个问题是你在玩家之间混淆了颜色。在你的MinMax调用中,你为两个位置传递了相同的颜色:
这是最大化玩家内部的调用:
val = MinMax(gameBoard, Extend.GetEnemyPlayerColor(myColor), false, depth - 1);
这是另一个调用:
val = MinMax(gameBoard, Extend.GetEnemyPlayerColor(myColor), true, depth - 1);
你正确地切换了是否最大化,但你不应该在玩家之间切换颜色。
实际上,你真的希望始终跟踪根节点处的玩家是谁,因为所有的评估都是相对于根节点处的玩家进行的。在这种情况下,你只需始终传递颜色,并且只在评估函数中使用敌人的颜色。