2015-05-04 66 views
0

我是一名初學者,我試圖通過應用minimax算法來開發Connect4遊戲,我被困在決定它是最小玩家回合還是最大玩家回合的條件下。我有這種感覺,但我已經想了兩天,試圖找出答案。 有什麼幫助嗎?Java:正確使用條件語句

private int evaluatePlayerMove(int depth, int maxDepth, int col, int alpha, int beta) { 
    boardsAnalyzed++; 

    int evaluatedMove=0; // For evaluating min player move or max player move 

    int min = Integer.MAX_VALUE, minScore = 0; // For min player 

    int max = Integer.MIN_VALUE, maxScore = 0; // For max player 


    if (col != -1) { 

    // Check whether it's min player turn or max player turn 

    // If it's min player turn then evaluate min move: 

    if(//it's min player turn){ 
     minScore = board.getHeuristicScore(Board.MARK_BLACK, col, depth, maxDepth); 
     if(board.blackWinFound()) { 
      blackWinFound = true; 
      return minScore; 
     } 


    if (depth == maxDepth) { 
     return minScore; 
    } 
    for (int c = 0; c < Board.COLUMNS; c++) { 
     if (board.isColumnAvailable(c)) { 
      board.mark(c, Board.MARK_RED); 
      int value = evaluatePlayerMove(depth + 1, maxDepth, c, alpha, beta); 
      board.unset(c); 
      if (value < min) { 
       min = value; 
       if (depth == 0) { 
        column = c; 
       } 
      } 
      if (value < beta) { 
       beta = value; 
      } 
      if (alpha >= beta) { 
       return beta; 
      } 
     } 


    } 

    if (min == Integer.MAX_VALUE) { 
     return 0; 
    } 
    evaluatedMove = min; 
    } 


    // If it's max player turn then evaluate max move: 

    if(//it's max player turn) { 
     maxScore = board.getHeuristicScore(Board.MARK_RED, col, depth, maxDepth); 
     if (board.redWinFound()) { 
      redWinFound = true; 
      return maxScore; 
     } 
    if (depth == maxDepth) { 
     return maxScore; 
    } 
    for (int c = 0; c < Board.COLUMNS; c++) { 
     if (board.isColumnAvailable(c)) { 
      board.mark(c, Board.MARK_BLACK); 
      int value = evaluatePlayerMove(depth + 1, maxDepth, c, alpha, beta); 
      board.unset(c); 
      if (value > max) { 
       max = value; 
       if (depth == 0) { 
        column = c; 
       } 
      } 
      if (value > alpha) { 
       alpha = value; 
      } 
      if (alpha >= beta) { 
       return alpha; 
      } 
     } 


    } 
    if (max == Integer.MIN_VALUE) { 
     return 0; 
    } 

    evaluatedMove= max; 
    } 


} 
return evaluatedMove; 

} 

回答

0

在大多數實時人工智能的情況下,這是你的人工智能程序與人類玩家。所以,通常,如果你正在構建一個最小最大樹,那麼AI程序將只能是最小或最大值,並且同樣會成爲樹的根。對於例如如果您嘗試將AI程序設置爲max,那麼樹的根將始終保持最大值,並且只需要計算最大值(最小值移動將是用戶輸入)。對於這種情況,我建議使用樹的深度作爲檢查條件。

if(root == max){ 
    for any node n: 
      if(n.depth%2 == 0){ 
        n is max 
      } 
      else{ 
       n is min 
      } 
} 

因爲深度通常用於幾乎所有問題,所以這將是一種有效的方法。但是,如果這是一個家庭作業問題,並且確實需要計算最小和最大移動,我建議使用一個實例靜態布爾變量isMax,它應該在每次移動後翻轉。