2016-02-27 193 views
1

我想讓我的minimax算法適用於8x8 Gomoku板,我必須匹配5行/ col /對角才能贏得勝利!我的算法似乎沒有正常工作,我無法確定它出錯的位置!Java中的Minimax不工作

我的generateMoves()方法工作正常,它會生成所有合法的移動,所以我知道不是這樣。最小最大值返回-1,-1作爲最佳移動,但當然會拋出一個非法移動錯誤,因爲從0,0到7,7板上不能有-1,-1。

任何幫助表示讚賞。謝謝。

private int[] minimax(int depth, Color [][] board) { 
      List<int[]> nextMoves = generateMoves(board); 

      int bestScore = (me == Color.WHITE) ? Integer.MIN_VALUE : Integer.MAX_VALUE; 
      int currentScore; 
      int bestRow = -1; 
      int bestCol = -1; 

      if (nextMoves.isEmpty() || depth == 0) { 
      bestScore = evaluateBoard(board); 
      } else { 
      for (int[] move : nextMoves) { 
       System.out.println(move[0]+","+move[1]+" THIS WAS ATTEMPTED"); 
       board[move[0]][move[1]] = Color.WHITE; 
       if (me == Color.BLACK) { 
        currentScore = minimax(depth - 1, board)[0]; 
        if (currentScore > bestScore) { 
         bestScore = currentScore; 
         bestRow = move[0]; 
         bestCol = move[1]; 
        } 
       } else { // oppSeed is minimizing player 
        currentScore = minimax(depth - 1, board)[0]; 
        if (currentScore < bestScore) { 
         bestScore = currentScore; 
         bestRow = move[0]; 
         bestCol = move[1]; 
        } 
       } 
       board[move[0]][move[1]] = null; 
      } 
      } 
      return new int[] {bestRow, bestCol}; 
     } 

public int evaluateBoard(Color[][] board) { 
    int score = 0; 
    // Check all the rows 
    for (int i = 0; i < 8; i++) { 
     int blank = 0; 
     int black = 0; 
     int white = 0; 
     for (int j = 0; j < 8 - 5; ++j) { 
      if (board[i][j] == Color.black) { 
       black++; 
       if (board[i][j + 1] == Color.BLACK) { 
        black++; 
        if (board[i][j + 2] == Color.BLACK) { 
         if (board[i][j + 3] == Color.BLACK) { 
          black++; 
          if (board[i][j + 4] == Color.BLACK) { 
           black++; 
          } 
         } 
        } 
       } 
      } else if (board[i][j] == Color.WHITE) { 
       white++; 
       if (board[i][j + 1] == Color.WHITE) { 
        white++; 
        if (board[i][j + 2] == Color.WHITE) { 
         white++; 
         if (board[i][j + 3] == Color.WHITE) { 
          white++; 
          if (board[i][j + 4] == Color.WHITE) { 
           white++; 
          } 
         } 
        } 
       } 
      } 
     } 
     System.out.println(black); 
     score += scoreChange(black, white); 
    } 

    // Check all the columns 
    for (int j = 0; j < 8; ++j) { 
     int blank = 0; 
     int black = 0; 
     int white = 0; 
     for (int i = 0; i < 8 - 5; ++i) { 
      if (board[i][j] == Color.black) { 
       black++; 
       if (board[i + 1][j] == Color.BLACK) { 
        black++; 
        if (board[i + 2][j] == Color.BLACK) { 
         black++; 
         if (board[i + 3][j] == Color.BLACK) { 
          black++; 
          if (board[i + 4][j] == Color.BLACK) { 
           black++; 
          } 
         } 
        } 
       } 
      } else if (board[i][j] == Color.WHITE) { 
       white++; 
       if (board[i + 1][j] == Color.WHITE) { 
        white++; 
        if (board[i + 2][j] == Color.WHITE) { 
         white++; 
         if (board[i + 3][j] == Color.WHITE) { 
          white++; 
          if (board[i + 4][j] == Color.WHITE) { 
           white++; 
          } 
         } 
        } 
       } 
      } 
     } 
     score += scoreChange(black, white); 
    } 

    int black = 0; 
    int white = 0; 
    // Check diagonal (Second) 
    for (int i = 7, j = 0; i > 3; --i, ++j) { 
     if (board[i][j] == Color.black) { 
      black++; 
      if (board[i - 1][j + 1] == Color.black) { 
       black++; 
       if (board[i - 2][j + 2] == Color.black) { 
        black++; 
        if (board[i - 3][j + 3] == Color.black) { 
         black++; 
         if (board[i - 4][j + 4] == Color.black) { 
          black++; 
         } 
        } 
       } 
      } 
     } else if (board[i][j] == Color.white) { 
      white++; 
      if (board[i - 1][j + 1] == Color.white) { 
       white++; 
       if (board[i - 2][j + 2] == Color.white) { 
        white++; 
        if (board[i - 3][j + 3] == Color.white) { 
         white++; 
         if (board[i - 4][j + 4] == Color.white) { 
          white++; 
         } 
        } 
       } 
      } 
     } 
    } 
    score += scoreChange(black, white); 

    black = 0; 
    white = 0; 
    // Check Diagonal (First) 
    for (int i = 0, j = 0; i < 4; ++i, ++j) { 
     if (board[i][j] == Color.black) { 
      black++; 
      if (board[i + 1][j + 1] == Color.black) { 
       black++; 
       if (board[i + 2][j + 2] == Color.black) { 
        black++; 
        if (board[i + 3][j + 3] == Color.black) { 
         black++; 
         if (board[i + 4][j + 4] == Color.black) { 
          black++; 
         } 
        } 
       } 
      } 
     } else if (board[i][j] == Color.white) { 
      white++; 
      if (board[i + 1][j + 1] == Color.white) { 
       white++; 
       if (board[i + 2][j + 2] == Color.white) { 
        white++; 
        if (board[i + 3][j + 3] == Color.white) { 
         white++; 
         if (board[i + 4][j + 4] == Color.white) { 
          white++; 
         } 
        } 
       } 
      } 
     } 
    } 
    score += scoreChange(black, white); 
    return score; 
} 

private int scoreChange(int black, int white) { 
    int change; 

    if (black == 5) { 
     change = -10000; 
    } else if (black == 4 && white == 0) { 
     change = -1000; 
    } else if (black == 3 && white == 0) { 
     change = -100; 
    } else if (black == 2 && white == 0) { 
     change = -10; 
    } else if (black == 1 && white == 0) { 
     change = -1; 
    } else if (white == 5) { 
     change = 10000; 
    } else if (white == 4 && black == 0) { 
     change = 1000; 
    } else if (white == 3 && black == 0) { 
     change = 100; 
    } else if (white == 2 && black == 0) { 
     change = 10; 
    } else if (white == 1 && black == 0) { 
     change = 1; 
    } else { 
     change = 0; 
    } 
    return change; 
} 
+0

你有你的< and >錯...... – elyashiv

回答

0

如果玩家是黑人,最好的分數是Integer.Max。目前的分數永遠不會比這大,所以最好的舉動從未設置。對於白人來說,問題是一致的:最好的分數是Integer.Min,現在的分數永遠不會更小。

用調試器找到像這樣的錯誤要容易得多。設置一個斷點,您將移動設置爲-1,然後前進。當你看到這些if語句時,如果代碼沒有進入,你會感到驚訝。你將仔細看看被比較的值,然後意識到你的錯誤。

+0

所以我基本定currentScore < >南轅北轍,我已經沒有固定的,但它似乎並沒有被衛冕也不以爲聰明還是 – Aceboy1993

+0

我不玩遊戲,我沒有你所有的代碼,所以我無法評估你的代碼不工作的原因。 (我也不應該)但是我懷疑你想要bestScore代表的不是「最好的得分」,而是「我發現到現在爲止的最好成績」。改變你的比較方向只是接受*每一個*舉動。你也可以用調試器解決問題。 –