Question

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 {

public static final int BOARD_SIZE = 8;
//squares per row and column

public static final int EMPTY = 0;
//used to indicate an empty square

public static final int QUEEN = 1;
//used to indicate sqaure contains a queen

private int board[][];//board
  
public Queens() {
//---------------------------------------------------
//Constructor: creates an empty square borad.
//---------------------------------------------------
board = new int[BOARD_SIZE][BOARD_SIZE];
}//end constructor

public void clearBoard() {
//-----------------------------------------------
// clears the board

// Precondition: None

//postcondition: sets all sqaures to empty
//------------------------------------------------

//To be implemented in Program Problem 1

}//end clearBoard

public void displayBoard() {
//------------------------------------------------
// display the board

//Precondition: None

//Postcondition: Board is written to standard
//output; zero is an EMPTY sqaure
//one is a square containing a queen (QUEEN)
//-------------------------------------------------

//To be implemented in Program Problem 1
}//end displayBoard

public boolean placeQueens(int column) {
//----------------------------------------------
//Places queens in column of the board beginning
//at the column specified.

//Precondition: queens are placed correctly in column 1 to through clumn -1

//Postcondition: If a solution is found, each column of the board contains one queen
//and method returns true; otherwise, return false(no solution exists 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 sqaure can be attacked
if (isUnderAttack(row, column)) {
  
++row;//consider next column
} 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 placed earlier
//and try next square in column
removeQueen(row, column);

++row;
}//end if
}//end end if
}//end while
  
return queenPlaced;
}//end if
}//end placeQueens

private void setQueen(int row, int column){
//-------------------------------------
//sets a queen at aquare indicated by row and column
  
//Precondition: Mone
//Postcondition: sets the sqaure on the board in a given rose and column to QUEEN.
//--------------------------------------
//To be implemented in Program Problem 1
}//end setQueen

private void removeQueen(int row, int column){
//-----------------------------------------------------
//Removes a qiueen at square indicated by row and cliumn
  
//Precondition: None
//Postcondition: Sets a sqaure on the board in a given row and column to EMPTY.
//---------------------------------------------------
//To be implemented in Program Problem 1
}//end removeQueen

private boolean isUnderAttack(int row, int column){
//------------------------------------------------------
//Determimne whether a square on the board at a given row and column
//is under attack by any queens.
//in the columns 1 thorugh -1
  
//Precondition:Each column between 1 and column -1 has a 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.
//------------------------------------------------------------
//To be implemented in Program Problem 1
}//end isUnderAttack ERROR

private int index(int number){
  
//----------------------------------------------------
//Returns the array index that corresponds to a row or colum numnber.
  
//Precondtion: 1 <= number <= BOARD_SIZE
  
//Postcondition: Returns adjusted index value
//--------------------------------------------------------
//To be implemented in Program Problem 1
}//end index ERROR
}//Queens

Thank you

0 0
Add a comment Improve this question Transcribed image text
Answer #1
public class Queens {
        // 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 contains a queen
        public static final int QUEEN = 1;

        private int board[][]; // chess board

        public Queens() {
                // -------------------------------------------------
                // Constructor: Creates an empty square board.
                // -------------------------------------------------
                board = new int[BOARD_SIZE][BOARD_SIZE];
        } // end constructor

        public void clearBoard() {
                // -------------------------------------------------
                // Clears the board.
                // Precondition: None.
                // Postcondition: Sets all squares to EMPTY.
                // -------------------------------------------------
                board = new int[BOARD_SIZE][BOARD_SIZE];
                for (int i = 0; i < BOARD_SIZE; i++) {
                        for (int j = 0; j < BOARD_SIZE; j++) {
                                removeQueen(i, j);
                        }
                }
        } // 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 (QUEEN).
                // -------------------------------------------------
                for (int i = 0; i < BOARD_SIZE; i++) {
                        for (int j = 0; j < BOARD_SIZE; j++) {
                                if (board[i][j] == QUEEN) {
                                        System.out.print("|1|");
                                } else {
                                        System.out.print("|0|");
                                }
                        }
                        System.out.println();
                }
        } // end displayBoard

        public boolean placeQueens(int column) {
                // -------------------------------------------------
                // Places queens in columns of the board beginning
                // at the 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 (no
                // solution exists 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 (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 next column,
                                        if (!queenPlaced) {
                                                // backtrack: remove queen placed earlier
                                                // 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) {
                // --------------------------------------------------
                // 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.
                // --------------------------------------------------
                board[index(row)][index(column)] = QUEEN;
        } // end setQueen

        private void removeQueen(int row, int column) {
                // --------------------------------------------------
                // Removes 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 EMPTY.
                // --------------------------------------------------
                board[index(row)][index(column)] = EMPTY;
        } // end removeQueen

        private boolean isUnderAttack(int row, int column) {
                // --------------------------------------------------
                // Determines whether the square on the board at a
                // given row and column is under attack by any queens
                // in the columns 1 through column-1.
                // Precondition: Each column between 1 and column-1
                // has a 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, returns true; otherwise, returns false.
                // --------------------------------------------------
                // check column
                for (int i = 0; i < index(row); i++) {
                        if (board[i][index(column)] == 1) {
                                return true;
                        }
                }
                // check row
                for (int i = 0; i < index(column); i++) {
                        if (board[index(row)][i] == 1) {
                                return true;
                        }
                }

                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.
                // Postcondition: Returns adjusted index value.
                // --------------------------------------------------
                return number - 1;
        } // end index
} // end Queens
Add a comment
Know the answer?
Add Answer to:
Complete the program that solves the Eight Queens problem. The program’s output should look similar to:...
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 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...

  • 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...

  • Write a program to place eight queens on an 8 x 8 chessboard in such a...

    Write a program to place eight queens on an 8 x 8 chessboard in such a way that one queen is to be in each row. A program will use 2 DIMENIONAL array x[r][c] to do this configuration. If x[r] has value c, then in row r there is a queen in column c. Write a program that asks a user to enter the columns that contain queens in the 8 rows. The program then places the queens in these...

  • 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....

  • CMPS 12B Introduction to Data Structures Programming Assignment 2 In this project, you will write...

    can i get some help with this program CMPS 12B Introduction to Data Structures Programming Assignment 2 In this project, you will write a Java program that uses recursion to find all solutions to the n-Queens problem, for 1 Sns 15. (Students who took CMPS 12A from me worked on an iterative, non-recursive approach to this same problem. You can see it at https://classes.soe.ucsc.edu/cmps012a/Spring l8/pa5.pdf.) Begin by reading the Wikipcdia article on the Eight Queens puzzle at: http://en.wikipedia.org/wiki/Eight queens_puzzle In...

  • Main objective: Solve the n queens problems. You have to place n queens on an n...

    Main objective: Solve the n queens problems. You have to place n queens on an n × n chessboard such that no two attack each other. Important: the chessboard should be indexed starting from 1, in standard (x, y) coordinates. Thus, (4, 3) refers to the square in the 4th column and 3rd row. We have a slight twist in this assignment. We will take as input the position of one queen, and have to generate a solution with a...

  • 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....

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