Question: java: The last column doesn't print a Queen. Most of it is source code and the error is from the displayBoard method. What am I

java: The last column doesn't print a Queen. Most of it is source code and the error is from the displayBoard method. What am I doing wrong?

public class Queens

{

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()

{

board = new int[BOARD_SIZE][BOARD_SIZE];

}

public void displayBoard () {

int counter = 0;

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

{

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

{

if (board[i][j] == QUEEN )

{

System.out.print("| X |");

counter++;

}

else

{

System.out.print("| 0 |");

}

}

System.out.println();

}

}

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)) {

++row; // consider next square in column

} // end if

else { // place queen and consider next column

setQueen(row, column);

queenPlaced = placeQueens(column+1);

// if no queen is possible in the next column,

if (!queenPlaced) {

// backtrack: remove queen placed earliers

// and try next square in column

removeQueen(row, column);

++row;

} // end if

} // end if

} // end while

return queenPlaced;

} // end if

} // end placeQueens

private void setQueen(int row, int column) {

// --------------------------------------------------

// Set 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.

// --------------------------------------------------

board[row-1][column-1] = QUEEN;

} // end setQueen

private void removeQueen(int row, int column) {

board[row-1][column-1] = EMPTY;

} // end setQueen

private boolean isUnderAttack(int row, int column) {

// check column

for (int i=0; i

if (board[i][column-1]==1){

return true;

}

}

// check row

for (int i=0; i

if (board[row-1][i] == 1){

return true;

}

}

// check lower diagnal

int lower_dir_row = row-2;

int lower_dir_column = column-2;

while (lower_dir_row>=0 && lower_dir_column>=0){

if (board[lower_dir_row][lower_dir_column]==1){

return true;

} else {

lower_dir_row = lower_dir_row -1;

lower_dir_column = lower_dir_column -1;

}

}

// check upper diagnal

int upper_dir_row = row;

int upper_dir_column = column-2;

while (upper_dir_row=0){

if(board[upper_dir_row][upper_dir_column] ==1){

return true;

}else{

upper_dir_row = upper_dir_row +1;

upper_dir_column = upper_dir_column -1;

}

}

return false;

} // end isUnderAttack

public static void main(String[] s)

{

Queens q = new Queens();

q.placeQueens(0);

q.displayBoard();

}

} // end Queens

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!