2013-09-26 100 views
1

我想做一个程序,做八皇后与递归,但我一直在得到一个数组越界的错误。现在我一直有这个问题,而且我似乎无法确定这个问题。这里是我的代码:八皇后阵列越界(爪哇)

public class Queens { 

public int currColumn = 0; 
public static final int BOARD_SIZE = 8; 
public static final int EMPTY = 0; 
public static final int QUEEN = 1; 
private int board[][]; 

public Queens() { 
    board = new int[BOARD_SIZE][BOARD_SIZE]; 
} 

public void clearBoard() { 
    for (int x = 0; x <= BOARD_SIZE; x++) { 
    for (int y = 0; y <= BOARD_SIZE; y++) { 
    board[x][y] = 0; 
    } 

    } 
} 

public void displayBoard() { 
    for (int x = 0; x < BOARD_SIZE; x++) { 
    System.out.print("\n"); 
    for (int y = 0; y < BOARD_SIZE; y++) { 
    System.out.print(board[x][y]); 
    } 

    } 
} 

public boolean placeQueens(int column) { 
    if (column > BOARD_SIZE) { 
    return true; 
    } else { 
    boolean queenPlaced = false; 
    int row = 1; 

    while (!queenPlaced && (row <= BOARD_SIZE)) { 
    if (isUnderAttack(row, column)) { 
    ++row; 
    } else { 
    setQueen(row, column); 
    queenPlaced = placeQueens(column + 1); 

    if (!queenPlaced) { 
     removeQueen(row, column); 
     ++row; 
    } 

    } 
    } 

    return queenPlaced; 
    } 
} 

public void setQueen(int row, int column) //SET BACK TO PRIVATE 
    { 
    board[row][column] = 1; 
    } 

private void removeQueen(int row, int column) { 
    board[row][column] = 0; 
} 

private boolean isUnderAttack(int row, int column) { 
    if (column == 0) { 
    return false; 
    } 

    int prevColumn = column - 1; 
    int prevRow = index(prevColumn); 

    while (prevColumn >= 0) { 
    prevRow = index(prevColumn); 

    for (int i = 0; i > BOARD_SIZE; i++) { 
    if (prevRow == row && prevColumn + i == column) //Going right 
    { 
    return true; 
    } 

    if (prevRow + i == row && prevColumn + i == column) //Going up/right 
    { 
    return true; 
    } 


    if (prevRow - i == row && prevColumn + i == column) //Going down/right 
    { 
    return true; 
    } 
    } 


    prevColumn--; 
    } 

    return false; 

} 

public int index(int column) //BACK TO PRIVATE 
    { 

    for (int i = 0; i < 8; i++) { 
    if (board[i][column] == 1) { 
    return i; 
    } 
    } 

    return 0; 


    } 

public static void main(String[] args) { 
    Queens x = new Queens(); 

    if (x.placeQueens(1) == true) { 
    x.displayBoard(); 
    } else { 
    System.out.println("No solution found"); 
    } 
} 
} 
+1

在你的代码中,错误行在哪里? – aUserHimself

回答

2

第一个问题:

public void clearBoard() 
{ 
    for(int x = 0; x < BOARD_SIZE; x++) // changed from x <= BOARD_SIZE 
    { 
     for(int y = 0; y < BOARD_SIZE; y++) // changed from y <= BOARD_SIZE 
     { 
      board[x][y] = 0; 
     } 

    } 
} 

数组索引计数从0开始高达size-1board[8][8]没有任何内容。

另一个问题

/* ... */ 
while(prevColumn >= 0) 
    { 
     prevRow = index(prevColumn); 

     for(int i = 0; i > BOARD_SIZE; i++) // i=0; i> BOARD_SIZE is always false, so no looping mechanism 
     { /* ... */ } 

将其更改为

for(int i = 0; i < BOARD_SIZE; i++) // corrected 

另一个问题?

if(column > BOARD_SIZE) when `column = 8` 

让它

if(column >= BOARD_SIZE) 

另一个问题:

while(!queenPlaced && (row <= BOARD_SIZE)) 

让它

row < BOARD_SIZE 

还有吗?

queenPlaced = placeQueens(column + 1); // What if column = 7 
+0

谢谢。我仍然得到一个ArrayIndexOutOfBounds虽然:( – Qwertywasd

+0

@ Qwertywasd在哪一行? – TheKojuEffect

+0

@Qwertywasd查看更新的答案。 – TheKojuEffect

0

在java数组索引从零开始。

for(int x = 0; x <= BOARD_SIZE; x++) 

应该是

for(int x = 0; x < BOARD_SIZE; x++) 
    { 
     for(int y = 0; y < BOARD_SIZE; y++) 
     { 
      board[x][y] = 0; 
     } 

BOARD_SIZE是8。所以阵列8个elelements初始化。所以avaliable索引均为0至7

环路

for(int x = 0; x <= BOARD_SIZE; x++) 
x=8 ArrayIndexOutOfBound异常抛出

在你所有的数组中循环检查这一点。

0
for(int x = 0; x <= BOARD_SIZE; x++) 

改变,要:

for(int x = 0; x < BOARD_SIZE; x++) 

由于阵列是0索引。

0

您尝试访问更多的元素比声明数组实际上有:

// this means you can access index in [0, BOARD_SIZE - 1]. 
board = new int[BOARD_SIZE][BOARD_SIZE]; 

// this means you access index in [0, BOARD_SIZE]. 
for(int x = 0; x <= BOARD_SIZE; x++) 

因此,一个好的做法是参照数组的声明大小时总是使用<