Here is the header file for the Queen class
#ifndef QUEEN_H
#define QUEEN_H
class Board; // declaration of Board class
/** @class Queen
* The Queen class. */
class Queen
{
public:
/** Puts queen in upper left corner of board. */
Queen();
/** Places 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();
/** Determines whether the queen is under attack by another queen.
* @return If there is a queen in the same row or the same
* diagonal, returns true; otherwise, returns false. */
bool isUnderAttack() const;
/** Saves a pointer to the board for all queens. */
static void setBoard(const Board *bPtr);
private:
/** Row (or prospective row) of queen if it is on the board. */
int row;
/** Column (or prospective column) of queen if it is on the board. */
int col;
/** All queens share the same board. */
static const Board *boardPtr;
}; // end Queen
#endif
The Board class
#ifndef BOARD_H
#define BOARD_H
#include "Queen.h"
#include <vector>
#include <iostream>
using namespace std;
static const int BOARD_SIZE = 8;
/** The Board class. */
class Board
{
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;
private:
/** Determines whether there is a queen in position (inRow,
* inCol). */
bool isQueen(int inRow, int inCol) const;
/** Attempts to place queens on board starting with the
* designated queen. */
bool placeQueens(Queen *queenPtr);
/** Removes the last queen on the board, but does not delete
* it. */
void removeQueen();
/** Places a queen on the board. */
void setQueen(const Queen *queenPtr);
/** Array of queens on the board. */
vector<const Queen *> queens;
}; // end Board
#endif
And the author actually gives you the placeQueens function implementation
bool Board::placeQueens(Queen *queenPtr)
{
// Base case. Trying to place Queen in a non-existent 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, then 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.
else
{ setQueen(queenPtr);
Queen *newQueenPtr = new Queen(0, queenPtr->getCol() + 1);
isQueenPlaced = placeQueens(newQueenPtr);
// If it wasn't possible to put the new Queen in the next
// column, backtrack by deleting the new Queen and
// removing the last Queen placed and moving it down one
// row.
if (!isQueenPlaced)
{ delete newQueenPtr;
removeQueen();
queenPtr->nextRow();
} // end if
} // end if
} // end while
return isQueenPlaced;
} // end placeQueens
Alrighty so here is what my implentation for Queen class is thus far:
Queen::Queens(): row(0), col(0)
{}
Queen::Queen(int inRow, int inCol): row(inRow), col(inCol)
{}
int Queen::getCol() const
{
return col;
}
int Queen::getRow() const
{
return row;
}
void Queen::nextRow()
{
row += 1;
}
bool Queen:isUnderAttack() const
{
// this is where I am scratching my head
}
void Queen::setBoard(const Board *bPtr)
{
// a little confused on what exactly goes on in here
}
The Board implentation thus far:
Board::Board()
{
queens.capacity(BOARD_SIZE);
}
Board::~Board()
{
queens.clear();
}
void Board::clear()
{
queens.clear();
}
// haven't implemented anything else
So my main question is how to solve the isUnderAttack() function. How do you index items that are queens using a row and column when your vector is a 1d array? Is there an indexing trick I am just not getting? Anyhow I am hoping that once I have this question answered I will be able to solve the rest on my own. Thanks for any help! Please feel free to ask any questions if I am not being clear on anything.