Ok. I've always had trouble with the Eight Queens problems that I've been given. Yet again another one appears. This one is supposed to see if eight queens are placed on an 8x8 chessboard in such a way that no queens will be able to attack each other. This means that they have to lie along same row, column, or diagonal.
It's supposed to look like this:
Enter the rows containing queens, in order by column: 2 0 6 4 7 1 3 5
Safe
Or supposed to look like this:
Enter the rows containing queens, in order by column: 2 3 4 0 1 7 6 5
Unsafe
I've been able to handle the other Eight Queens codes with the help from people on this forum but this one I don't even know where to start.
Once you have that, loop through each of the queens and see if any of them are in the same row/column/diagonal from any other. As soon as you find one able to attack another, you can print "Unsafe" and exit. If you never find one that can be attacked... then you know it's safe.
EDIT:
For logic problems... I typically try to think about how I would solve it myself... like... without a computer.
If you were given a chess set and given this problem. What would you do to determine whether or not any queen can attack any other?
Take your answer to that question, and translate it into code.
Ok so I was able to get it down to where I just need to check for the diagonal. Right now I have it where if any of the queens are in the same row or column as another, it will output "Unsafe". But how do I get it to determine if any are on the same diagonal as another?
#include <iostream>
usingnamespace std;
int main()
{
cout << "Enter the rows containing queens, in order by column: ";
//Collects the data for the array.
int board[8];
for(int i = 0; i < 8; i++)
cin >> board[i];
int q = 0;
for (int i = 0; i < 8; i++)
{
q = 0;
for (int j = 0; j < 8; j++)
{
if (board [j] == i)
{
q++;
}
}
//Already unsafe even without diagonal check.
if (q != 1)
{
cout << endl << "Unsafe";
return 0;
}
}
So where would I put that in my code? I understand you're looking at the two numbers of each corner behind the queen and in front of the queen hence the absolute value but I don't know where to put it.
I didn't really give you any code to put anywhere. I outlined the logic involved with determining whether or not 2 pieces are diagonal from each other.
So at whatever point in your code where you are checking to see if 2 pieces are diagonal... you would write some code which does a check based on the above logic.