I can't follow a 2D array. |
Please explain.
keltonfan2 wrote: |
---|
I don't want to use recursion or multiple functions (if possible)
I don't want to use any header files I haven't already included
I don't know how to fill in from files or any of this.
I just want to get the program working. |
It sounds like you want to get a working program without knowing anything or doing anything.
That is barely acceptable, when some actual work has to be done using such program.
That is absolutely not acceptable, if the whole purpose is to learn programming.
You should want to know. You should want to experiment. You should want to use available options.
The bump, bump, bump is not an option.
On your latest code on line 17 you still have to test whether a value in the board matches some index, because your input routine does not make such guarantee. Lets add that for you:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
|
#include <iostream>
int main()
{
int board[8] {};
std::cout << "Enter the rows (values [1..8]) containing queens, in order by column:\n";
int col = 0;
while ( col < 8 ) {
int value = 0;
std::cout << "Row on column " << static_cast<char>('A'+col) << ' ';
if ( ! (std::cin >> value) ) return 1; // input failure
if ( 0 < value && value <= 8 ) {
board[col] = value-1;
++col;
}
}
std::cout << '\n';
// now the board contains valid values
// show the queens:
for ( col = 0; col < 8; ++col ) {
const char column = 'A' + col;
std::cout << 'Q' << 1+col << " is in " << column << 1+board[col] << '\n';
}
// checking the status of the board starts here
return 0;
}
|
This board has queens on the first two columns. The 'x' mark where they can attack (row and diagonals):
ABCDEFGH
8...xx...
7x.xx....
6xQxxxxxx
5xxx.....
4Qxxxxxxx
3.x..x...
2..x..x..
1...x..x. |
The third queen is on column C:
If the queen is on C8, C3 or C1, then it is "safe so far" and you must mark the rest of its row and diagonals, just like the first queens have marked theirs.
If the queen is on any other row, then the entire board is Unsafe.