Question
Complete the program that solves the Eight Queens problem (pages 318 through 320). The programs output should look similar to: |1|0|0|0|0|0|0|0| |0|0|0|0|0|0|1|0| |0|0|0|0|1|0|0|0| |0|0|0|0|0|0|0|1| |0|1|0|0|0|0|0|0|
Complete the program that solves the Eight Queens problem (pages 318 through 320). The programs output should look similar to:
|1|0|0|0|0|0|0|0|
|0|0|0|0|0|0|1|0|
|0|0|0|0|1|0|0|0|
|0|0|0|0|0|0|0|1|
|0|1|0|0|0|0|0|0|
|0|0|0|1|0|0|0|0|
|0|0|0|0|0|1|0|0|
|0|0|1|0|0|0|0|0|
Use the Queens class given on pages 318 through 321 of your textbook. In your implementation of the Queens class, complete the body of all methods marked as To be implemented in Programming Problem 1. Do not change any of the global variable declarations, constructor or placeQueens methods.
public class Queen {
//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 conains a queen public static final int QUEEN = 1;
private int board[][]; public void Queens(){
//Constructor: Creates an empty square board
board = new int [BOARD_SIZE][BOARD_SIZE]; } //end constructor public void clearBoard(){ //clears board
// preconditon: none
//postcondition:set all squares to empty
} //End clearBoard public void displayBoard(){
//displays the board
//precondition: none.
//postcondition: Board is written to standard
//output; zero is an empty square, one is a square containing a queen } //end displayBoard public boolean placeQueens(in column){ //places queens in columns of the board beginning at 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
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; } } private void setQueen(int row, int column){ //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 } //end setQueen private void removeQueen(int row, int column){ //removes a queen at square indicated by row and column
//preconditon: none
//postcondition: sets the square indicated by row and column to EMPTY } //end removeQueen private boolean isUnderAttack(int row, int column){ //determines wether the square on the board ar a fiven row and column is under attack by any queens in the columns 1 through column - 1
//precondition: each column is under attack by any queen placed 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, return true; otherwise return false } //end isUnderAttack private int index(int number){ //returns the array index that corresponds to a row or column number
//precondition: 1 <= number <= BOARD_SIZE
//postcondtion: returns adjusted index value } //end Index } //end Queens
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started