================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 your book are some suggestions to implementing two ADTs to solve the problem in C++. I want you to follow their suggestions to implement the solution.
Please let me know if you have any doubts or you want me to modify
the answer. And if you find this answer useful then don't forget to
rate my answer as thumps up. Thank you! :)
//main.cpp
#include <iostream>
#include <vector>
#include "Queen.h"
#include "Board.h"
#include "Queen.cpp"
#include "Board.cpp"
using namespace std;
#include <iostream>
int main() {
Board board;
for (int row = 0; row < 8; row++) {
Queen queen(row,
0);
queen.setBoard(&board);
board.doEightQueens(queen);
board.display();
board.clear();
cout <<
endl;
}
system("PAUSE");
return 0;
}
-----------------------------------------------------------------------------------------------------------
//Queen.cpp
#include "Queen.h"
#include "Board.h"
#include <iostream>
using namespace std;
const Board* Queen::boardPtr;
Queen::Queen() : row(0), col(0) {
}
Queen::Queen(int inRow, int inCol) : row(inRow), col(inCol) {
}
Queen::~Queen() {
}
int Queen::getCol() const {
return col;
}
int Queen::getRow() const {
return row;
}
void Queen::nextRow() {
++row;
}
bool Queen::isUnderAttack() const {
for (int i = 0; i <
boardPtr->getNumQueens(); i++) {
const Queen* q =
boardPtr->getQueen(i);
if (q->getCol() ==
col || q->getRow() == row || q->getRow()-q->getCol() ==
row-col || q->getRow()+q->getCol() == row+col)
return true;
}
return false;
}
void Queen::setBoard(const Board* bPtr) {
boardPtr = bPtr;
}
-----------------------------------------------------------------------------------------------------------
//Queen.h
#ifndef QUEEN_H
#define QUEEN_H
class Board; class Queen {
public:
Queen();
~Queen();
Queen(int inRow, int inCol);
int getCol() const;
int getRow() const;
void nextRow();
static void setBoard(const Board* bPtr);
bool isUnderAttack() const;
private:
int row;
int col;
static const Board* boardPtr;
};
#endif
-----------------------------------------------------------------------------------------------------------
//Board.cpp
#include <iostream>
#include <vector>
#include "Board.h"
using namespace std;
Board::Board() {
Queen::setBoard(this);
}
Board::~Board() {
clear();
}
bool Board::placeQueens(Queen* queenPtr) {
if (queenPtr->getCol() >= BOARD_SIZE)
{
delete queenPtr;
return true;
}
bool isQueenPlaced = false;
while (!isQueenPlaced && queenPtr->getRow()<BOARD_SIZE) {
if
(queenPtr->isUnderAttack()) {
queenPtr->nextRow();
}
else {
setQueen(queenPtr);
Queen* newQueenPtr = new Queen(0, queenPtr->getCol() + 1);
isQueenPlaced = placeQueens(newQueenPtr);
if (!isQueenPlaced) {
delete newQueenPtr;
removeQueen();
queenPtr->nextRow();
}
}
}
return isQueenPlaced;
}
void Board::doEightQueens(Queen q) {
placeQueens(new
Queen(q.getRow(),q.getCol()));
}
void Board::clear() {
for (int i = getNumQueens(); i >
getNumQueens() ;++i) {
delete
(queens[i]);
}
queens.clear();
}
void Board::removeQueen() {
if (queens.size() != 0)
queens.pop_back();
}
bool Board::isQueen(int row, int col) const {
bool isOnBoard = false;
for (int i = 0; i < (int)queens.size(); i++)
{
if
(queens[i]->getRow() == row && queens[i]->getCol() ==
col) {
isOnBoard = true;
}
}
return isOnBoard;
}
void Board::setQueen(Queen* queenPtr) {
queens.push_back(queenPtr);
}
void Board::display() const {
cout << " ";
for (int rowNum = 0; rowNum < BOARD_SIZE;
rowNum++)
cout << rowNum
<< " ";
cout << endl;
for (int row = 0; row < BOARD_SIZE; row++)
{
cout << row
<< " ";
for (int col = 0; col
< BOARD_SIZE; col++) {
if (isQueen(row, col)) {
cout << "Q ";
}
else cout << "+ ";
} cout <<
endl;
}
}
int Board::getNumQueens() const {
return queens.size();
}
const Queen* Board::getQueen(int index) const {
return queens[index];
}
-----------------------------------------------------------------------------------------------------------
//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;
bool isQueen(int inRow, int inCol) const ;
bool placeQueens(Queen* queenPtr);
void removeQueen();
void setQueen( Queen* queenPtr);
public:
Board();
~Board();
void clear();
void display() const;
void doEightQueens(Queen q);
int getNumQueens() const;
const Queen* getQueen(int index) const;
};
#endif
![/CLionProjects/Eight m] - main.cpp main.cpp *증 *-Δ cMakel.ists.txt main.cppQueenepp Queen.h.cpp Board.h Project ▼ /Users/swap](http://img.homeworklib.com/images/4749db2d-2376-4f7e-aa42-715a8741d348.png?x-oss-process=image/resize,w_560)
================Data Structures C++=============== – Implement the Eight Queens Problem This is a...
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 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 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...
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 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 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...
I have a question about C++. I have to code the 8 queens puzzle without using any recursive methods. Every time the program runs, it will randomly place 8 queens throughout the board. I got that working, but the issue is the board is suppose to be filled with asterisks and the queens are suppose to be represented with a Q. I have put zeros instead of asterisks and ones instead of Qs, i tried to fix it but it...
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....
Can you explain how this code for the 8 Queens Problem works? It's in C++ In case you dont know what the 8 Queens problem is, "The eight queens puzzle is the problem of placing eightchess queens on an 8×8 chessboard so that no two queensthreaten each other; thus, a solution requires that no two queens share the same row, column, or diagonal." This is the code: #include <iostream> #include <cmath> using namespace std; bool ok(int q[], int c) { ...
PLEASE COMPLETE IN C++ LANGUAGE
Location row : int = -1 col : int = -1 setLocation(row : int, col : int) getRow(): int getColl): int isEmpty(): bool Details Write all the code necessary to implement the Location class as shown in the UML Class Diagram to the right. Do this in a project named snake (we'll continue adding files to this project as the term progresses). Your class definition and implementation should be in separate files. When complete, you...