我正在尝试制作一个用递归执行八皇后的程序,但我继续得到一个数组越界错误。我一直遇到这个问题已经有一段时间了,我似乎无法确定这个问题。这是我的代码:
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");
}
}
}
答案 0 :(得分: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-1
开始。 board[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
答案 1 :(得分: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 .so数组初始化为8个元素。所以可用的索引是0到7。
循环
for(int x = 0; x <= BOARD_SIZE; x++)
当x=8
ArrayIndexOutOfBound
异常抛出时。
在您循环的所有阵列中检查此点。
答案 2 :(得分:0)
for(int x = 0; x <= BOARD_SIZE; x++)
将其更改为:
for(int x = 0; x < BOARD_SIZE; x++)
因为数组是0索引的。
答案 3 :(得分: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++)
因此,一个好的做法是在引用数组的声明大小时始终使用<
。