2014-07-16 114 views
1

我正在研究象棋引擎,现在试图实现Minimax算法。目前我已经编写了一个mimimax代码,但它并不能正常工作。考虑到我不是一个好的棋手,我几分钟之内就击败了引擎。MinMax算法不能正常工作

我想有人善意地看看我的极小极大码,并告诉我我写的是正确的。

在此先感谢。

这里是我的代码:

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; 
} 
+0

有一件事是-Integer.MIN_VALUE将无法正常工作。 – resueman

+0

@resueman,我已经做出了改变,但没有改善 – mish

+0

而你正在对抗哪个“深度”?如果数字很低,可以轻松取胜。在'MiniMax'方法中,你只需要调用'Max',这是可疑的(应该为其他播放器调用'Min')。 –

回答

0

你把一个相当复杂的任务:)极小实现本身几乎是好的,看看维基页面:

Minimax Algorithm

我想尽量减少玩家应该使用最佳移动= +无限(在你的情况Integer.MAX_VALUE)

但是,既然你声明你的程序pl ays相当不好,我会提供另一个观察。我认为只有在您的案例中有一个非常好的评估函数(EvaluatePieceScore()方法)时,该算法才能正常工作。这是“艺术”隐藏的地方。你确定你的方法实现是否足够好?我是这样说的,因为通常人们花费主要精力来实现这个功能,而不是算法本身。

希望这有助于

+0

我确定我的EvaluatePieceScore()工作正常 – mish