#include <iostream>
#define ROW 10
#define COL 10
using namespace std;
void display_board();
void populate();
void checkDiag(char [][COL], int, int);
void checkSides(char [][COL], int, int);
bool checkNull(char [][COL]);
int main() {
int option;
do {
populate();
cout << "Anoter configuration?" << endl;
cout << "1) Yes" << endl << "2) Quit" << endl;
cin >> option;
} while (option != 2);
system("pause");
return 0;
}
void display_board(char chessBoard[][COL]) {
for (int i=0; i < ROW; i++) {
for (int j=0; j< COL; j++) {
cout << "|_" << chessBoard[i][j] << "_| ";
}
cout << endl;
}
}
void populate() { // Placing the first queen.
static char chessBoard[ROW][COL];
int x, y;
do {
srand(time(NULL)); // random position
x = rand()%COL;
y = rand()%ROW;
while (chessBoard[x][y] != 'Q' && chessBoard[x][y] != 'x') {
chessBoard[x][y] = 'Q';
} // queen
checkSides(chessBoard, x, y); // fills horizontal/vertical with Xs
checkDiag(chessBoard, x, y); // diagonal checks
checkNull(chessBoard);
} while (checkNull);
display_board(chessBoard); // displays the board
}
// Checks if there's an empty space in the chessboard
bool checkNull(char chessBoard[][COL]) {
bool isFilled = false;
for (int i = 0; i < ROW; i++) {
for (int j = 0; j < COL; j++) {
if (chessBoard[i][j] != NULL) {
isFilled = true;
break;
}// if
}// for j
}// for i
return isFilled;
}
void checkSides(char chessBoard[][COL], int x, int y) {
for (int i =0; i < ROW; i++) {
if (chessBoard[i][y] != 'Q' && chessBoard[i][y] == NULL) {
chessBoard[i][y] = 'x'; //vertical
}
}
for (int i=0; i < COL; i++) {
if (chessBoard[x][i] != 'Q' && chessBoard[x][i] == NULL) {
chessBoard[x][i] = 'x'; // horizontal
}
}
}
void checkDiag(char chessBoard[][COL], int x, int y) {
for (int i = 0 ; i < 10; i++) {
if (chessBoard[x+i][y+i] == NULL && chessBoard[x+i][y+i] != 'Q')
chessBoard[x+i][y+i] = 'x';
if (chessBoard[x+i][y-i] == NULL && chessBoard[x+i][y-i] != 'Q')
chessBoard[x+i][y-i] = 'x';
if (chessBoard[x-i][y-i] == NULL && chessBoard[x+i][y-i] != 'Q')
chessBoard[x-i][y-i] = 'x';
if (chessBoard[x-i][y+i] == NULL && chessBoard[x-i][y+i] != 'Q')
chessBoard[x-i][y+i] = 'x'; // diagonal X
}
}