Question C Code 8 Queens Puzzle Without Using Recursive Methods Every Time Program Runs R Q43800308
I have a question about C++. I have to code the 8 queens puzzlewithout using any recursive methods. Every time the program runs,it will randomly place 8 queens throughout the board. I got thatworking, but the issue is the board is suppose to be filled withasterisks and the queens are suppose to be represented with a Q. Ihave put zeros instead of asterisks and ones instead of Qs, i triedto fix it but it wont work for some reason, thats the only issuewith my program, down below i have to code to it, may someoneplease take a look at it and try to fix my problem and copy thecode in here please! Thank you!
// Eight Queens
// 1-2-2020
// Period 5
#include <iostream>
#include <time.h>
#include <stdlib.h>
using namespace std;
bool canPlaceRC(int, int);
bool canPlaceDiag(int, int);
void fillQueens();
void resetBoard();
void printBoard();
//Constant variable to replace all ‘8’
const int SIZE = 8;
int board[SIZE][SIZE];
int main()
{
srand(time(0));
resetBoard();
fillQueens();
return 0;
}
bool canPlaceDiag(int c1, int c2)
{
//Creates new variables to matchcoordinates
int x = c1;
int y = c2;
//Going to top left
while (x >= 0 && y >= 0)
{
x–;
y–;
if (board[x][y] == 1)
{
return false;
}
else
{
continue;
}
}
//Resets x and y to match originalcoordinates
x = c1;
y = c2;
//Going to bottom right
while (x < SIZE && y < SIZE)
{
x++;
y++;
if (board[x][y] == 1)
{
return false;
}
else
{
continue;
}
}
x = c1;
y = c2;
//Going to top right
while (x >= 0 && y < SIZE)
{
x–;
y++;
if (board[x][y] == 1)
{
return false;
}
else
{
continue;
}
}
x = c1;
y = c2;
//Going to bottom left
while (x < SIZE && y >= 0)
{
x++;
y–;
if (board[x][y] == 1)
{
return false;
}
else
{
continue;
}
}
//If all have not returned False, returnTrue;
return true;
}
bool canPlaceRC(int c1, int c2)
{
//Variables to check if a queen is in the same row orcolumn
int sum1 = 0;
int sum2 = 0;
bool checkC;
bool checkR;
//Checker for columns
for (int x = 0; x < SIZE; x++)
{
//Sum1 is adding all numbers in a vertical iterationstarting from zero
sum1 += board[x][c2];
}
//If the sum is 0, then there were no Queens in its’column
if (sum1 == 0)
{
checkC = true;
}
else
{
checkC = false;
}
//Checker for rows
for (int x = 0; x < SIZE; x++)
{
//Sum2 is adding all numbers in a horizontal iterationstarting from zero
sum2 += board[c1][x];
}
if (sum2 == 0)
{
checkR = true;
}
else
{
checkR = false;
}
//Checks if both returned true
if ((checkR == true) && (checkC ==true))
{
return true;
}
else
{
return false;
}
}
void resetBoard()
{
//Goes through every spot in board, setting it all tozero
for (int r = 0; r < SIZE; r++)
{
for (int c = 0; c < SIZE; c++)
{
board[r][c] = 0;
}
}
}
void printBoard()
{
//Space to seperate headers
cout << ” “;
//Prints the header
for (int i = 0; i < SIZE; i++)
{
cout << i << ” “;
}
cout << endl;
//Prints the side header while also printing the rowsfor the matrix
for (int y = 0; y < SIZE; y++)
{
cout << y << ” “;
for (int x = 0; x < SIZE; x++)
{
//Goes through the matrix horizontally
cout << board[x][y] << ” “;
}
cout << endl;
}
}
void fillQueens()
{
//Counter so that only 8 queens are placed
int count = 0;
//Counter to check how many times the queen placementhas not worked
int notWork = 0;
while (count < SIZE)
{
//Generates a random coordinate for a queen
int c1 = rand() % SIZE;
int c2 = rand() % SIZE;
//If both horizontal, vertical and diagonal checks aretrue, place the queen
if (canPlaceRC(c1, c2) == true &&canPlaceDiag(c1, c2) == true)
{
board[c1][c2] = 1;
// USED FOR DEBUGGING: cout << “Queen at: “<< c1 << “, ” << c2 << endl;
count++;
}
else
{
notWork++;
//64 because there are 64 slots in the board. Eventhough some may be repeated, this just ensures most, if not all,spots
if (notWork == 64)
{
// USED FOR DEBUGGING: cout << “RESET” <<endl;
//Resets the board to all 0’s, resets the failure countsand number of times a queen can be placed
resetBoard();
notWork = 0;
count = 0;
}
//If the fail count is not 64, generate a new randomcoordinate
continue;
}
}
printBoard();
}
Expert Answer
Answer to I have a question about C++. I have to code the 8 queens puzzle without using any recursive methods. Every time the prog…
OR