2013-11-25 39 views
0
public class OthelloJPlayer extends OthelloPlayer { 
    @Override 
    public OthelloMove getMove(OthelloState state) { 
    int bestchoice = 0; 
    int bestscore = Integer.MIN_VALUE; 
    boolean maximizingPlayer = true; 

    // generate the list of moves: 
    List<OthelloMove> moves = state.generateMoves(); 

    if (moves.isEmpty()) { 
     // If there are no possible moves, just return "pass": 
     return null; 
    } else { 
     // turn moves to states 
     List<OthelloState> states = new ArrayList<OthelloState>(); 

     for (int i = 0; i < moves.size(); i++) { 
     states.add(state.applyMoveCloning(moves.get(i))); 
     } 

     for (int i = 0; i < states.size(); i++) { 
     // uses minmax to determine best move. 
     int score = (MinMax(3, states.get(i), maximizingPlayer)); 

     if (score > bestscore) { 
      bestscore = score; 
      bestchoice = i; 

     } 
     } 
    } 

    return moves.get(bestchoice); 
    } 

    // min max algorithm 
    public int MinMax(int depth, OthelloState game_board, boolean maximizingPlayer) { 
    List<OthelloMove> moves; 

    if (depth == 0) { 
     int score = game_board.score(); 

     return score; 
    } 

    if (maximizingPlayer) { 
     int bestvalue = Integer.MIN_VALUE; 
     // gets other players moves 
     moves = game_board.generateMoves(1); 

     if (moves.isEmpty()) { 
     int score = game_board.score(); 

     return score; 

     } else { 
     for (int i = 0; i < moves.size(); i++) { 
      OthelloState new_game_board = new OthelloState(8); 
      new_game_board = game_board.applyMoveCloning(moves.get(i)); 

      int returned_score = MinMax(depth - 1, new_game_board, false); 
      bestvalue = max(bestvalue, returned_score); 
     } 
     } 
     return bestvalue; 
    } else { 
     int bestvalue = Integer.MAX_VALUE; 
     // gets your moves 
     moves = game_board.generateMoves(0); 

     if (moves.isEmpty()) { 
     int score = game_board.score(); 

     return score; 
     } else { 
     for (int i = 0; i < moves.size(); i++) { 
      OthelloState new_game_board = new OthelloState(8); 
      new_game_board = game_board.applyMoveCloning(moves.get(i)); 

      int returned_score = MinMax(depth - 1, new_game_board, true); 
      bestvalue = min(bestvalue, returned_score); 
     } 
     } 

     return bestvalue; 
    } 
    } 
} 

我的minimax算法似乎沒有返回最優的移動。當我的代理使用minimax代理玩對抗執行隨機移動的代理時,它有時會失去。從我認識的一切看起來好的可以有人請檢查我的邏輯我一定錯過了一些東西。啓發式就是得分。一個積極的分數意味着你贏得一個負分數意味着另一個玩家正在獲勝。用於黑白棋的Minimax算法不能正常工作

+1

看到這個問題也:http://stackoverflow.com/questions/9511814/minimax-algorithm-doesnt-return-best-move – Fernando

+0

在遊戲高達這一個分支因子,你可以通常只會在樹中探索幾個級別。因此可以做出不理想的舉動。絕對不可以Minimax(有或沒有修復)可以避免這種情況。保證最佳的唯一舉動是當樹的底部「可見」時,通常在遊戲結束時。 –

回答

0

您有許多問題。

  1. getMove方法是真正的搜索的根,這是一個最大的節點。因此,它應該調用MinMaxmaximizingPlayer = false

  2. 當您調用MinMax時,您需要交換玩家。現在,您只需從max - > max - > min - > min - > min ...,因爲您使用了truefalse常量。將您的調用(對於最小和最大情況)更改爲MinMax(depth - 1, new_game_board, !maximizingPlayer)

  3. 確保game_board.score()從最大玩家角度給出評估。