Question

Complete the program that solves the Eight Queens problem in java only please (pages 318 through...

Complete the program that solves the Eight Queens problem in java only please (pages 318 through

320). The program’s 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|

PlaceQueens(in currColumn:integer)

//places queens in columns numbered currColumn through 8

If (currColumn>8){

The problem is solved }

Else {

While(unconsidered squares exist in curr column and the problem is unsolved ){

Determine the next square in column currColumn that is not under attack by a queen in an earlier column

If (such a square exists){

Place a queen in the square

placeQueens(currColumns+1)//try next column

if (no queen is possible in column and consider the next square in that column

}//end if

}//end if

}//end while

}//end if

The method placeQueens is used in the following context

Clear all squares on the board

Using placeQueens placeQueens(1) //begin with first column

If (asolution exists){

Display solution

}else

Display message //no solution found

}//end if

Public class Queens {

// square per roll or column

Public static final int BOARD_SIZE=8;

//used to indicate an empty square

Public static final int EMPTY=0;

Public static final int QUEEN=1;

Private int board [][]; //chess board

Public Queens (){

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

//Constructor : Creates and empty board.

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

board = new int [BOARD_SIZE] [BOARD_SIZE];

}//end constructor

Public void clearBoard(){

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

//Clears the board.

//precondition: none

//postcondition: sets all squares to EMPTY.

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

//to be implemented in programming problem 1

}// end constructor

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

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

To implement programming problem 1

}//end displayBoard

Public boolean placeQueens(int column){

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

//places queens in columns of the board beginning

//at the column specified

//preconditions : Queen 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

//return true; otherwise ,return false (no

//solutionexists for a queen anywhere in column

//specified

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

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 (isUnderatttack(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 next column,

If (!queenPlaced){

//backtrack: remove queen placedearlier

//and try next square in column

removeQueen (row, column );

++row;

}//end if

}//end if

}//end while

return queenPlaced;

}//end if

}// end PlaceQueens

Private Void setQueens(int row,int column){

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

Sets a queen at square indicated by rowand column

//precondition:none

//postcondition: sets the square on the board in a given row and column.

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

//to implement be implemented in programming problem 1

}//end removeQueens

private boolean isUnderAtttack (int row ,itn column){

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

//determine whether the square on the board at a

//given row or column is underAttackby any queens

//in the columns 1 through coulumn-1.

Precondition. Each column between 1 and coulumn -1

//has a queen placed in a square at a specific row

//none of these queens can be attacked by any other queen

Post condition; if the designated square is under attack

//return true; otherwise,return false.

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

To implement programming problem 1

}// end isUnderAttack

Private in index(int number ){

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

Returns the array index that correspond to

//a row or column number

//precondition ; 1<= number <=BOARD_SIZE.

//post condition ; returns adjusted index value.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//places queens in columns numbered currColumn through 8

If (currColumn>8){

The problem is solved }

Else {

While(unconsidered squares exist in curr column and the problem is unsolved ){

Determine the next square in column currColumn that is not under attack by a queen in an earlier column

If (such a square exists){

Place a queen in the square

placeQueens(currColumns+1)//try next column

if (no queen is possible in column and consider the next square in that column

}//end if

}//end if

}//end while

}//end if

The method placeQueens is used in the following context

Clear all squares on the board

Using placeQueens placeQueens(1) //begin with first column

If (asolution exists){

Display solution

}else

Display message //no solution found

}//end if

Public class Queens {

// square per roll or column

Public static final int BOARD_SIZE=8;

//used to indicate an empty square

Public static final int EMPTY=0;

Public static final int QUEEN=1;

Private int board [][]; //chess board

Public Queens (){

...........................................

//Constructor : Creates and empty board.


...........................................

board = new int [BOARD_SIZE] [BOARD_SIZE];

}//end constructor

Public void clearBoard(){

...........................................

//Clears the board.

//precondition: none

//postcondition: sets all squares to EMPTY.

...........................................

//to be implemented in programming problem 1

}// end constructor

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


...........................................

To implement programming problem 1

}//end displayBoard

Public boolean placeQueens(int column){


...........................................

//places queens in columns of the board beginning

//at the column specified

//preconditions : Queen 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

//return true; otherwise ,return false (no

//solutionexists for a queen anywhere in column

//specified

...........................................

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 (isUnderatttack(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 next column,

If (!queenPlaced){

//backtrack: remove queen placedearlier

//and try next square in column

removeQueen (row, column );

++row;

}//end if

}//end if

}//end while

return queenPlaced;

}//end if

}// end PlaceQueens

Private Void setQueens(int row,int column){

...................................................................

Sets a queen at square indicated by rowand column

//precondition:none

//postcondition: sets the square on the board in a given row and column.

.........................................................

//to implement be implemented in programming problem 1

}//end removeQueens

private boolean isUnderAtttack (int row ,itn column){

..........................................................

//determine whether the square on the board at a

//given row or column is underAttackby any queens

//in the columns 1 through coulumn-1.

Precondition. Each column between 1 and coulumn -1

//has a queen placed in a square at a specific row

//none of these queens can be attacked by any other queen

Post condition; if the designated square is under attack

//return true; otherwise,return false.

.....................................................

To implement programming problem 1

}// end isUnderAttack

Private in index(int number ){

.................................................

Returns the array index that correspond to
//a row or column number

//precondition ; 1<= number <=BOARD_SIZE.

//post condition ; returns adjusted index value.

Add a comment
Know the answer?
Add Answer to:
Complete the program that solves the Eight Queens problem in java only please (pages 318 through...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Complete the program that solves the Eight Queens problem. The program’s output should look similar to:...

    Complete the program that solves the Eight Queens problem. The program’s 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. 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. Here is what I have so far with notes of what is needed. public class Queens...

  • Please help i need a C++ version of this code and keep getting java versions. Please...

    Please help i need a C++ version of this code and keep getting java versions. Please C++ only Purpose: This lab will give you experience harnessing an existing backtracking algorithm for the eight queens problem, and seeing its results displayed on your console window (that is, the location of standard output). Lab A mostly complete version of the eight queens problem has been provided for you to download. This version has the class Queens nearly completed. You are to provide...

  • in c++ Purpose: This lab will give you experience harnessing an existing backtracking algorithm for the...

    in c++ Purpose: This lab will give you experience harnessing an existing backtracking algorithm for the eight queens problem, and seeing its results displayed on your console window (that is, the location of standard output). Lab A mostly complete version of the eight queens problem has been provided for you to download. This version has the class Queens nearly completed. You are to provide missing logic for the class Queens that will enable it to create a two-dimensional array that...

  • ================Data Structures C++=============== – Implement the Eight Queens Problem This is a...

    ================Data Structures C++=============== – Implement the Eight Queens Problem This is an object oriented program. This is #1 on page 187 of your book with ONE difference, I’m requiring you add a client program to test your code (a main). If you have the old book the question says “Complete the Queen and Board class for the Eight Queens problem.” On page 179 of your book is a function placeQueens that solves the Eight Queens problem. On page 180 of...

  • please explain/ comment 3. Eight Queens Write a program that places eight queens on a chessboard...

    please explain/ comment 3. Eight Queens Write a program that places eight queens on a chessboard (8 x 8 board) such that no queen is "attacking" another. Queens in chess can move vertically, horizontally, or diagonally. How you solve this problem is entirely up to you. You may choose to write a recursive program or an iterative (i.e., non-recursive) program. You will not be penalized/rewarded for choosing one method or another. Do what is easiest for you. 3.1. Output Below...

  • Hello I am having trouble with a connectFour java program. this issue is in my findLocalWinner...

    Hello I am having trouble with a connectFour java program. this issue is in my findLocalWinner method, it declares a winner for horizontal wins, but not for vertical. if anyone can see what im doing wrong. public class ConnectFour { /** Number of columns on the board. */ public static final int COLUMNS = 7; /** Number of rows on the board. */ public static final int ROWS = 6; /** Character for computer player's pieces */ public static final...

  • JAVA Only Help on the sections that say Student provide code. The student Provide code has...

    JAVA Only Help on the sections that say Student provide code. The student Provide code has comments that i put to state what i need help with. import java.util.Scanner; public class TicTacToe {     private final int BOARDSIZE = 3; // size of the board     private enum Status { WIN, DRAW, CONTINUE }; // game states     private char[][] board; // board representation     private boolean firstPlayer; // whether it's player 1's move     private boolean gameOver; // whether...

  • 110Marks Question No. 4 Eight queens problem: place 8 queens on a chess board so that no two queens attack each other. -state: locations of 0 to 8 queens (with no two queens attacking each other) goa...

    110Marks Question No. 4 Eight queens problem: place 8 queens on a chess board so that no two queens attack each other. -state: locations of 0 to 8 queens (with no two queens attacking each other) goal test: 8 queens placed on the board (with none attacked) operator: place a queen in the left-most empty column such that it is not attacked by any other queen (and does not attack any other queen) path cost: 0 Depth first search: i....

  • Complete an Array-Based implementation of the ADT List including a main method and show that the...

    Complete an Array-Based implementation of the ADT List including a main method and show that the ADT List works. Draw a class diagram of the ADT List __________________________________________ public interface IntegerListInterface{ public boolean isEmpty(); //Determines whether a list is empty. //Precondition: None. //Postcondition: Returns true if the list is empty, //otherwise returns false. //Throws: None. public int size(); // Determines the length of a list. // Precondition: None. // Postcondition: Returns the number of items in this IntegerList. //Throws: None....

  • JAVA QUESTION!!! please help! The following codes are a Maze program. There are TWO traversable paths...

    JAVA QUESTION!!! please help! The following codes are a Maze program. There are TWO traversable paths possible in this maze. The program uses recursion. Can someone please explain to me why the program will pick one path if multiple traversable paths are available? Why does the program choose one path over a different path? (I believe it has something to do with the recursion terminating when a solution is found but I'm not sure.) Thank you!!!! The codes: public class...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT