Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I have a question about C++. I have to code the 8 queens puzzle without using any recursive methods. Every time the program runs, it

I have a question about C++. I have to code the 8 queens puzzle without using any recursive methods. Every time the program runs, it will randomly place 8 queens throughout the board. I got that working, but the issue is the board is suppose to be filled with asterisks and the queens are suppose to be represented with a Q. I have put zeros instead of asterisks and ones instead of Qs, i tried to fix it but it wont work for some reason, thats the only issue with my program, down below i have to code to it, may someone please take a look at it and try to fix my problem and copy the code in here please! Thank you!

// Eight Queens

// 1-2-2020

// Period 5

#include

#include

#include

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 match coordinates

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 original coordinates

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, return True;

return true;

}

bool canPlaceRC(int c1, int c2)

{

//Variables to check if a queen is in the same row or column

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 iteration starting 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 iteration starting 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 to zero

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 rows for 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 placement has 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 are true, 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. Even though 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 counts and number of times a queen can be placed

resetBoard();

notWork = 0;

count = 0;

}

//If the fail count is not 64, generate a new random coordinate

continue;

}

}

printBoard();

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

OCA Oracle Database SQL Exam Guide Exam 1Z0-071

Authors: Steve O'Hearn

1st Edition

1259585492, 978-1259585494

More Books

Students also viewed these Databases questions

Question

What is consumer surplus?

Answered: 1 week ago