#include <iostream>
#include <iomanip>
#include <ctime>
using namespace std;
const int S = 8;
bool validMove1(int, int, char[][S]);
bool isEmpty(char[][S]);
int counter1 = 0;
void printBoard1(char[8][8]);
char board1[8][8] = { '\0' };
int r1, c1;
int m1 = 0;
bool done1 = false;
int main(){
srand(time(0));
int row = rand() % 8;
int column = rand() % 8;
while (isEmpty(board1))
{
if (validMove1(row, column, board1))
{
// Y axis
for (int i = 0; i < S; ++i){
board1[i][column] = '*';
}
//X axis
for (int j = 0; j < S; ++j){
board1[row][j] = '*';
}
//diagonal right top
for (int j = 1; j < 8 - column; j++)
{
board1[row - j][column + j] = '*';
}
//diagonal left bottom
for (int j = 1; j <= column; j++){
board1[row + j][column - j] = '*';
}
//diagonal right bottom
for (int j = 1; j < 8 - column; j++){
board1[row + j][column + j] = '*';
}
//diagonal left top
for (int j = 1; j <= column; j++){
board1[row - j][column - j] = '*';
}
board1[row][column] = 'Q';
}
row = rand() % 8;
column = rand() % 8;
counter1++;
}
printBoard1(board1);
printf("\n\n\n Total loop = %d",counter1);
cin.get();
return 0;
}
void printBoard1(char board1[8][8]){
for (int row = 0; row < S; ++row){
for (int col = 0; col < S; ++col)
{
cout << setw(3) << board1[row][col];
}
cout << '\n';
}
}
bool validMove1(int r2, int c2, char board2[][S])
{
// NOTE: This test stops as soon as it becomes false
return (r2 >= 0 && r2 < S && c2 >= 0 && c2 < S
&& board2[r2][c2] == '\0'&& board2[r2][c2] != '*');
}
bool isEmpty(char board[][8])
{
for (int r3 = 0; r3 < 8; ++r3)
for (int c3 = 0; c3 < 8; ++c3)
if (board[r3][c3] == '\0')
return true; // at least one open square is available
return false; // no available squares
}
Output:
* * * * * Q * *
* * * Q * * * *
* * * * * * Q *
Q * * * * * * *
* * * * * * * Q
* * * * Q * * *
* Q * * * * * *
* * * * * * * *
Total loop = 707406462
Post a Comment