Menu

(Solved) : Data Structures C Implement Eight Queens Problem Object Oriented Program 1 Page 187 Book O Q37130455 . . .

================Data Structures C++===============

– Implement the Eight Queens Problem

This is an object oriented program. This is #1 on page 187 ofyour book with ONE difference, I’m requiring you add a clientprogram to test your code (a main). If you have the old book thequestion says “Complete the Queen and Board class for the EightQueens problem.”

On page 179 of your book is a function placeQueens that solvesthe Eight Queens problem.

On page 180 of your book are some suggestions to implementingtwo ADTs to solve the problem in C++. I want you to follow theirsuggestions to implement the solution.

  1. First create a Board class datatype. This class data type should represent the chessboard (you can use any implementation you want, a two dimensionalarray (however this may waste space but not if you’re displaying inmy opinion), a vector (it could represent only the board placesoccupied by queens), an array of vectors etc – there is more thanone right way!). The board should keep track of the Queenscurrently placed on it and contain operations/methods (such asplaceQueens and displayBoard) to solve the problem and display asolution. You can add other methods as you need them. Note theboard will need to contain a Queen (or perhaps an array or vectorof queens) – I haven’t done this problem yet but I immediatelythought composition might be needed.
  2. Next create a Queen data type. A queeninstance should be able to keep track of its current position (rowand column) and be able to move to the next row. Again you can addother methods as you see fit.
  3. Add a client program to test your code:
    • This program should place the first queen anywhere on the boardand then call placeQueens to see if a solution can be reached. Ifthe solution can be reached the program should display the boardwith the queens properly placed.
    • The header fi le for the class Board from the bookexample
      /** @file Board.h */
      #ifndef _BOARD
      #define _BOARD
      #include “Queen.h”
      #include <vector>
      #include <cassert>#include <iostream>
      using namespace std;
      static const int BOARD_SIZE = 8;
      class Board
      {
      private :
      vector<Queen*> queens; // Array of pointers to queens on theboard
      /** Sees whether a queen exists in position (inRow, inCol).*/
      bool isQueen( int inRow, int inCol) const;
      /** Attempts to place queens on board, starting with the designatedqueen. */
      const placeQueens(Queen* queenPtr);
      /** Removes the last queen from the board, but does not deallocateit. */
      void removeQueen();
      /** Places a queen on the board. */
      void setQueen( const Queen* queenPtr);
      public:
      /** Supplies the Queen class with a pointer to the board. */
      Board();
      /** Clears the board and removes pointer from queens. */
      ~Board();
      /** Clears board. */
      void clear();
      /** Displays board. */
      void display() const;
      /** Initiates the Eight Queens problem. */
      void doEightQueens();
      /** @return The number of queens on the board. */
      int getNumQueens() const;
      /** @return A pointer to the queen at the designated index.*/
      const Queen* getQueen( int index) const;
      }; // end Board
      #endif
      LISTING 5-2 The class Queen
      class Board; // Forward declaration of Board class
      /** The Queen class. */
      class Queen
      {
      public:
      /** Places a queen in upper-left corner of board. */
      Queen();/** Places a queen in supplied location. */
      Queen( int inRow, int inCol);
      /** @return Column number. */
      int getCol() const;
      /** @return Row number. */
      int getRow() const;
      /** Moves queen to next row. */
      void nextRow();
      /** Sees whether the queen is under attack by another queen.
      @return True if another queen is in the same row or the same
      diagonal; otherwise, returns false. */
      bool isUnderAttack() const;
      /** Saves a pointer to the board for all queens. */
      static void setBoard( const Board* bPtr);
      private:
      int row; // Row (or prospective row) of queen if it is on theboard
      int col; // Column (or prospective column) of queen if it ison
      // the board
      // All queens share the same board
      static const Board* boardPtr;
      }; // end Queen
      An implementation of placeQueens follows:
      bool Board::placeQueens(Queen* queenPtr)
      {
      // Base case: Try to place a queen in a nonexistent column.
      if (queenPtr->getCol() >= BOARD_SIZE)
      {
      delete queenPtr;
      return true;
      } // end if
      bool isQueenPlaced = false;
      while (!isQueenPlaced && queenPtr->getRow() <BOARD_SIZE)
      {
      // If the queen can be attacked, try moving it
      // to the next row in the current column
      if (queenPtr->isUnderAttack())
      queenPtr->nextRow();
      else
      {
      // Put this queen on the board and try putting a
      // new queen in the first row of the next column
      setQueen(queenPtr);
      Queen* newQueenPtr = new Queen(0, queenPtr->getCol() + 1);
      isQueenPlaced = placeQueens(newQueenPtr);// If it wasn’t possibleto put the new queen in the next column,
      // backtrack by deleting the new queen and moving the last
      // queen placed down one row
      if (!isQueenPlaced)
      {
      delete newQueenPtr;
      removeQueen();
      queenPtr->nextRow();
      } // end if
      } // end if
      } // end while
      return isQueenPlaced;
      } // end placeQueens

Expert Answer


Answer to Data Structures C Implement Eight Queens Problem Object Oriented Program 1 Page 187 Book O Q37130455 . . .

OR