2013-09-24 18 views
1

我試圖做一個程序來解決八皇后問題,但我不斷收到和異常錯誤,每次我運行代碼這就是我所擁有的。我有點困惑在做什麼。任何方向的幫助將不勝感激。八皇后Java異常錯誤

異常在線程 「主」 java.lang.ArrayIndexOutOfBoundsException:-1在 Queens.isUnderAttack(Queens.java:132)在 Queens.placeQueens(Queens.java:78)在 Queens.main( Queens.java:155)

public class Queens 
{ 
    //squares per row or column 
    public static final int BOARD_SIZE = 8; 

    //used to indicate an empty square 
    public static final int EMPTY = 0; 

    //used to indicate square contains a queen 
    public static final int QUEEN = 1; 

    private int board[][]; //chess board 

    public Queens() 
    { 
     //constructor: Creates an empty square board. 
     board = new int[BOARD_SIZE][BOARD_SIZE]; 

    } 


    //clears the board 
    //Precondition: None 
    //Postcondition: Sets all squares to EMPTY 
    public void clearBoard() 
    { 
     //loops through the rows 
     for(int row = 0; row < BOARD_SIZE; row++) 
     { 
      //loops through the columns 
      for (int column = 0; column < BOARD_SIZE; column++) 
      { 
       board[row][column] = EMPTY; 
      } 
     } 
    } 


    //Displays the board 
    //precondition: None 
    //postcondition: Board is written to standard output; 
    //zero is an EMPTY square, one is a square containing a queen (QUEEN). 
    public void displayBoard() 
    { 
     for (int row = 0; row < BOARD_SIZE; row++) 
     { 
      System.out.println(""); 

      for (int column = 0; column < BOARD_SIZE; column++) 
      { 
       System.out.print(board[row][column] + " "); 
      } 
     } 
    } 



    //Places queens in columns of the board beginning at the column specified. 
    //Precondition: Queens are placed correctly in columns 1 through column-1. 
    //Postcondition: If a solution is found, each column of the board contains one queen and 
    //method returns true; otherwise, returns false (no solution exists for a queen anywhere in column specified). 
    public boolean placeQueens(int column) 
    { 
     if(column >= BOARD_SIZE) 
     { 
      return true; //base case 
     } 
     else 
     { 
      boolean queenPlaced = false; 
      int row = 1; // number of square in column 

      while(!queenPlaced && (row < BOARD_SIZE)) 
      { 
       //if square can be attacked 
       **if (!isUnderAttack(row, column))** 
       { 
        setQueen(row, column); //consider next square in column 
        queenPlaced = placeQueens(column+1); 
        //if no queen is possible in next column, 
        if(!queenPlaced) 
        { 
         //backtrack: remover queen placed earlier 
         //and try next square in column 
         removeQueen(row, column); 
         //++row; 
        } 
       } 
       row++; 
      } 
      return queenPlaced; 
     } 

    } 



    //Sets a queen at square indicated by row and column 
    //Precondition: None 
    //Postcondition: Sets the square on the board in a given row and column to Queen. 
    private void setQueen(int row, int column) 
    { 
     board[row][column] = QUEEN; 
    } 



    //removes a queen at square indicated by row and column 
    //Precondition: None 
    //Postcondition: Sets the square on the board in a given row and column to EMPTY. 
    private void removeQueen(int row, int column) 
    { 
     board[row][column] = EMPTY; 
    } 


    //Determines whether the square on the board at a given row and column is 
    //under attack by any queens in the columns 1 through column-1. 
    //Precondition: Each column between 1 and column-1 has a queen paced in a square at 
    //a specific row. None of these queens can be attacked by any other queen. 
    //Postcondition: If the designated square is under attack, returns true: otherwise return false. 
    private boolean isUnderAttack(int row, int column) 
    { 


      for (int y=0; y<BOARD_SIZE; y++) 
      { 
       if (board[row][y] == QUEEN || // possible horizontal attack 
         board[row-column+y][y] == QUEEN || // diagonal NW 
         **board[row+column-y][y] == QUEEN)  // diagonal SW** 
         return true; 
      } 

     return false; 
    } 



    private int index(int number) 
    { 
     //Returns the array index that corresponds to a row or column number. 
     //Precondition: 1 <= number <= BOARD_SIZE. 
     //Postcondition: Returns adjusted index value 

     return number -1 ; 
    } 


    //main to test program 
    public static void main(String[] args) 
    { 
     Queens Q = new Queens(); 
     **if(Q.placeQueens(0))** 
     { 
      System.out.println(Q); 
     } 
     else 
     { 
      System.out.println("Not Possible"); 
     } 
    } 


} 
+1

請指出132行,78行和155 – Prateek

+0

我向他們添加了粗體(**)標籤...請再次查看上面的代碼 – user1896464

+0

您需要重新對角線檢查。當'row == 1'和'column == 0'時,'row-column + y == 1 + y'從1變爲'1 + BOARD_SIZE = 9',超過了數組的大小。類似地,'row + column-y == 1-y'從1到1-BOARD_SIZE = -7,它低於零(最低可能的索引)。另外,我想你想從'row = 0'開始放置皇后,在這種情況下你仍然會出界。 –

回答

3
private boolean isUnderAttack(int row, int column) 
{ 
    for (int y = 0; y < BOARD_SIZE; y++) 
    { 
     if (board[row][y] == QUEEN) 
      return true; // possible horizontal attack 

     int x1 = row - column + y; 
     if (0 <= x1 && x1 < BOARD_SIZE && board[x1][y] == QUEEN) 
      return true; // diagonal NW 

     int x2 = row + column - y; 
     if (0 <= x2 && x2 < BOARD_SIZE && board[x2][y] == QUEEN) 
      return true; // diagonal SW 
    } 

    return false; 
} 
+0

非常感謝...有道理 – user1896464

2

的任一值會低於零

row-column+y 
row+column-y 
3

在你的廁所p在「isUnderAttack()」,這是不好的:由於「Y」從「0」變爲「板尺寸」

board[row-column+y][y] 
board[row+column-y][y] 

,這將意味着索引你的數組的邊界的(除非行列都是0) - 如錯誤信息清楚地表明。

迴路應與相應的索引,或通過添加條件檢查索引在邊界進行修正:

int rowIndex = row-column+y; 
if(rowIndex>=0 && rowIndex<BOARD_SIZE) { 
     if(board[row-column+y][y] == QUEEN) { 
      return true; 
     } 
} 

當然,同爲另一條對角線...

媽的,這是緩慢的Android手機上鍵入密碼,即使有QWERTY ...

0

其中一個方案的哪些引起錯誤 考慮這種情況,電路板[行+列-y] [y] == QUEEN 假設行= 1,列= 0和y是4,索引變成-ve導致錯誤