1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
|
#include <iostream>
#include <vector>
using namespace std;
class Queen {
public:
void setRow(int inRow);
int getRow() const;
private:
int row;
};
int Queen::getRow() const {
return row;
}
void Queen::setRow(int inRow) {
row = inRow;
}
class Board {
public:
static const int BOARD_SIZE = 8;
Board();
void doQueens();
void display() const;
private:
bool placeQueens(int row, int col);
bool findNextSafeSquare(int& row, int col);
bool isUnderAttack(int row, int col);
vector<Queen> queens;
};
Board::Board() {
queens.resize(BOARD_SIZE);
}
void Board::doQueens() {
if (placeQueens(0, 0)) {
display();
} else {
cout << "No solution found." << endl;
}
}
bool Board::placeQueens(int row, int col) {
// use the pseudocode above to complete this function.
findNextSafeSquare(row,col);
while(row < BOARD_SIZE && col < BOARD_SIZE)
{
queens[col].setRow(row);
if(col == BOARD_SIZE-1 || placeQueens(0, col + 1))
{
return true;
}
else
{
queens[col].setRow(row + 1);
row = queens[col].getRow();
}
findNextSafeSquare(row,col);
}
return false;
}
bool Board::isUnderAttack(int testRow, int testCol) {
/*
for (int i = testRow; i < BOARD_SIZE; i++)
{
}
*/
}
}
//Cannot work on this until underAttack is done
// Sets "row" to the row of the next safe square in column col. Important note:
// Starts with the given row and col. In other words, the given row and col may
// be the "next safe square".
// returns true if a safe square is found, false if no safe square is found. If
// return value is false, row is undefined.
bool Board::findNextSafeSquare(int& row, int col) {
}
//After FindNextSafeSquare is done then do this
// Displays a visual representation of the current state of the board. For each position
// on the board, displays "X" if a queen is located at that position, otherwise displays
// "_" (underscore).
void Board::display() const {
}
int main() {
Board board;
board.doQueens();
}
|