Qwertywasd
Qwertywasd

Reputation: 45

Eight Queens Array out of bounds (Java)

I'm trying to make a program that does Eight Queens with recursion but I keep on getting an array out of bounds error. I've been having problems with this for awhile now and I can't really seem to pinpoint the problem. Here's my code:

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");
  }
 }
}

Upvotes: 1

Views: 529

Answers (4)

TheKojuEffect
TheKojuEffect

Reputation: 21091

First Problem:

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;
        }

    }
}

Array index counting starts from 0 upto size-1. There is nothing in board[8][8].

Another Problem

/* ... */
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
        { /* ... */ }

Change it to

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

Another Problem ?

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

Make it

if(column >= BOARD_SIZE)

Another Problem:

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

Make it

 row < BOARD_SIZE

Yet Another ?

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

Upvotes: 2

Suresh Atta
Suresh Atta

Reputation: 122008

In java array indexes starts from zero.

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

should be

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

Your BOARD_SIZE is 8 .so array initialized for 8 elelements. So avaliable indexes are 0 to 7.

in loop

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

when x=8 ArrayIndexOutOfBound Exception throws.

Check this point in all your arrays,where you are looping.

Upvotes: 0

aUserHimself
aUserHimself

Reputation: 1597

You try to access more elements than the declared array actually has:

// 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++)

So, a good practice is to always use < when referring to the declared size of the array.

Upvotes: 0

Ren&#233; Jensen
Ren&#233; Jensen

Reputation: 451

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

Change that to:

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

Since arrays are 0-indexed.

Upvotes: 0

Related Questions