I'm solving the 8 Queens Puzzle (place 8 queens on chessboard without conflicts) and created a class Square
. Now I think this class should be the father of 2 subclasses Queen
and Empty
.
There is an other class: Chessboard
that contains a bi-directional arrays, what I want is that when printing I will have 8 Q
(Queens) and 56 E
(empty see code).
These are the classes (I'll not write all):
class Square{
int x;
int y;
public :
Square() {};
Square(int ,int);
bool isAvaible();
void swapStatus();
virtual void printValue();
void setCoord(int,int);
int getX();
int getY();
};
void Square::printValue(){
std::cout << "S ";
};
#include "square.hpp"
class Queen : public Square{
public:
...
virtual void printValue();
...
};
void Queen::printValue(){
std::cout << "Q ";
}
#include "Square.hpp"
class Empty : Square {
public:
...
virtual void printValue();
...
};
void Empty::printValue(){
std::cout << "E ";
}
At the beginning all elements inside Chessboard
are Square
, when a queen is added the Square
must be replaced by the Queen
.
This is how I do it :
...
std::vector<std::vector<Square> > squares;
...
void Chessboard::setQueen(int x, int y){
squares[x][y]=Queen(x,y);
}
I use this method to set 8 queen on the chessboard and when printing all squares this is the output :
S S S S S S S S
S S S S S S S S
S S S S S S S S
S S S S S S S S
S S S S S S S S
S S S S S S S S
S S S S S S S S
S S S S S S S S
Instead of :
Q S S S S S S S
S Q S S S S S S
S S Q S S S S S
S S S Q S S S S
S S S S Q S S S
S S S S S Q S S
S S S S S S Q S
S S S S S S S Q
Any suggestion?