Java 最小-最大算法工作不正常
我正在研究一个国际象棋引擎,现在正试图实现极大极小算法。目前,我已经编写了一个mimimax代码,但它并没有真正正常工作。考虑到我不是一个好的棋手,我在几分钟内就击败了引擎 我想有人看看我的minimax代码,告诉我我写对了什么 提前谢谢 这是我的密码:Java 最小-最大算法工作不正常,java,algorithm,artificial-intelligence,chess,Java,Algorithm,Artificial Intelligence,Chess,我正在研究一个国际象棋引擎,现在正试图实现极大极小算法。目前,我已经编写了一个mimimax代码,但它并没有真正正常工作。考虑到我不是一个好的棋手,我在几分钟内就击败了引擎 我想有人看看我的minimax代码,告诉我我写对了什么 提前谢谢 这是我的密码: private int MiniMax(Game game, int depth){ return Max(depth); } private int Max(int depth){ if (depth <= 0
private int MiniMax(Game game, int depth){
return Max(depth);
}
private int Max(int depth){
if (depth <= 0
|| this.chessgame.getGameState() == Game.GAME_STATE_END_YELLOW_WON
|| this.chessgame.getGameState() == Game.GAME_STATE_END_BROWN_WON){
return EvaluatePieceScore();
}
int max = -Integer.MIN_VALUE;
List<Move> moves = generateMoves(false);
for(Move allMove : moves){
executeMove(allMove);
int score = -Mini(depth - 1);
undoMove(allMove);
if( score > max){
max = score;
}
}
return max;
}
private int Mini(int depth) {
if (depth <= 0
|| this.chessgame.getGameState() == Game.GAME_STATE_END_YELLOW_WON
|| this.chessgame.getGameState() == Game.GAME_STATE_END_BROWN_WON){
return EvaluatePieceScore();
}
int min = Integer.MIN_VALUE;
List<Move> moves = generateMoves(false);
for(Move allMove : moves){
executeMove(allMove);
int score = -Max(depth - 1);
undoMove(allMove);
if( score > min){
min = score;
}
}
return min;
}
private int MiniMax(游戏,int深度){
返回最大值(深度);
}
私有整数最大值(整数深度){
如果(最大深度){
最大值=分数;
}
}
返回最大值;
}
私有整数迷你(整数深度){
如果(最小深度){
最小值=分数;
}
}
返回最小值;
}
您完成了一项相当复杂的任务:)MiniMax实现本身几乎没有问题,请查看WIKI页面:
我认为最小化玩家应该使用最佳移动=+无限(在你的例子中是Integer.MAX_值)
然而,由于您声明您的程序运行得相当糟糕,我将提供另一个观察结果。我认为只有当您有一个非常好的求值函数(在您的例子中是EvaluatePieceScore()方法)时,该算法才会起作用。这就是“艺术”隐藏的地方。您确定您的方法实现足够好吗?我这么说是因为人们通常把主要精力花在实现这个函数上,而不是算法本身
希望这有帮助一件事是-Integer.MIN\u值不起作用。@resueman,我已经做了更改,但没有改进,您正在与哪个
深度
?如果这是一个很低的数字,就有可能轻松获胜。在MiniMax
方法中,您只调用Max
,这是可疑的(Min
应该为其他玩家调用)。我确信我的EvaluatePiceScore()工作正常