Could someone help me with this problem?

Pages: 12
Feb 26, 2015 at 6:05pm
I have a problem where eight queens are to be placed on an 8x8 chessboard in such a way that no queens will be able to attack each other (i.e., lie along same row, column or diagonal). The program will prompt the user to enter, for each column, the row that contains a queen. For the given configuration, the program should print "Safe" if no queen can attack any other queen (none are sharing the same row, column or diagonal), or print "Unsafe" if an attack is possible. I'm not entirely sure how to do this however, and I'd like some help. I am working from this base code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  #include <iostream>
using namespace std;
int main() {
   int board[8];
   cout << "Enter the rows containing queens, in order by column: ";
   for(int i = 0; i < 8; ++i) {
       cin >> board[i];
   }
   cout << endl;
   for(int i = 0; i < 8; ++i) {
       for(int a = 0; a < 8; ++a) {
           if(board[a] != i) {
               cout << ".";
          //     cout << ".";
           } else {
             //     cout << ".";
               cout << "Q";
           }
       }
       cout << endl;
   }
}
Feb 26, 2015 at 7:15pm
By your definition, you do already know that each queen is on different column. That leaves the rows and diagonals.

What if you had an array of bool values that are initially all false? When you look at new queen, you know its row number. If that position in the bool array is already true, then the board is Unsafe. If the position is still false, you update it to true and continue.

Two similar bool arrays for the two diagonal directions. Calculating the position within those arrays is a bit more complicated though.

If no queen meets and Unsafe situation, the board is Safe.
Feb 26, 2015 at 8:25pm
So I need to put bool values in somewhere?
Feb 26, 2015 at 10:56pm
That is just one approach. There are other methods too, which do not use arrays of bool values.

Furthermore, you should sanitize the input in the lines 6-8 loop. All values that are not in range [0..7] should be discarded.
Feb 27, 2015 at 7:03pm
Can someone give me an example?
Feb 27, 2015 at 9:40pm
Bumping the topic, I really really need help with this
Feb 27, 2015 at 11:16pm
bump
Feb 28, 2015 at 1:24am
bump....
Feb 28, 2015 at 2:06am
so your asking how to make a Q a bool value ???
Feb 28, 2015 at 3:35am
Yes, or whatever solves the problem.
Feb 28, 2015 at 2:08pm
bump..
Feb 28, 2015 at 2:48pm
Initialise an 8x8 array, representing attacked squares on a chess board of bool to all false.
1
2
const int N = 8 ;
bool attacked[N][N] = { {false} };

When a queen is placed, check if all the values in the row, column and the two diagonals are false.
If any of them is true, print "unsafe"
If all of them are false, mark all the values in the row, column and the two diagonals as true and print "safe"
Feb 28, 2015 at 7:31pm
What would the code for that be?
Feb 28, 2015 at 8:07pm
bump...... I really need to get this working!
Feb 28, 2015 at 9:00pm
bump
Feb 28, 2015 at 10:01pm
bump.
Feb 28, 2015 at 10:27pm
haha, lot of bumps!
normally it takes several hours to get reply.

it will be more reasonable question:
suppose one of your friend trying to solve eight queen puzzle in his chessboard. he is trying many variations and asking you if he could solved it!
this is one board position:
00000001
01000000
00010000
10000000
00000100
00001000
00100000
00000010

0=empty square
1=queen

now write a C++ program for your friend!
the inputs are given in a file.
Feb 28, 2015 at 10:31pm
bool board[8][8];
first fill the 2d array from input file.
Feb 28, 2015 at 11:05pm
What? I don't know how to fill in from files or any of this. I just want to get the program I detailed in the OP working.
I will stop bumping so often though.
Feb 28, 2015 at 11:27pm
keltonfan2: the program you gave us is hardly even able to do anything. JLBorges and anup30 are giving you advice on how to build up your board by reading it in from file. this way you wont need to manually do it each time.

this might help you on your way to reading stuff from a file.
http://www.cplusplus.com/doc/tutorial/files/

keskiverto, JLBorges and anup30 are giving you advice to so you can make you program work. think about what JLBorges and anup30 are saying and try incorporating that design into your program.
Pages: 12