2014-07-16 49 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