Click to See Complete Forum and Search --> : QUEEN puzzle


f15e
09-05-2002, 10:07 AM
Using C++

I have a small problem with the while loops w/in main().
Here is the code:

#include<iostream>
#include<fstream>
#include<string>

using namespace std;

const int numCols = 8;
const int numRows = 8;
const string line = "+---+---+---+---+---+---+---+---+";

void LoadArray(char grid[numRows][numCols], int& x, int& y);
void BoardDisp(char grid[numRows][numCols], int& i, int& j );

int main()
{
char grid[numRows][numCols] = {0};
int x, y;
int c;
ifstream inFile;
inFile.open("solutions.txt");

if(!inFile)
{
cout << " No File Found!!\n\n";
return -1;
}

cout << "\n Will QUEENS Live or Die?\n";
cout << " ------------------------\n";

inFile >> x >> y;
while(inFile)
{
c = 0;
while(c < numCols)
{
LoadArray(grid, x, y); //I think the second time through,
c++; //after reading the 1st 8 pairs and
inFile >> x >> y; //processing them, it has no input file
} // values to put in LoadArray, causing my
// problem, but inFiling from diff. areas
cout << endl; //hasn't helped me unless I have overlooked
BoardDisp(grid, x, y); // something which is very possible.
}

inFile.close();
return 0;
}

/************************************************************ ************************/

void LoadArray(char grid[][numCols], int& x, int& y)
{

cout << x << y << ' ';
grid[x-1][y-1] = 'Q';


}

/************************************************************ ************************/

void BoardDisp(char grid[][numCols], int& x, int& y)
{
cout << line << endl;
for (x = 0; x < numRows; x++)
{
cout << "|";
for(y = 0; y < numCols; y++)
{
if(grid[x][y] == 'Q')
cout << ' ' << grid[x][y] << ' ' << '|';

else if(grid[x][y] == 0)
cout << " |";
}
cout << endl << line << endl;
}

for (x = 0; x < numRows; x++)
for (y = 0; y < numCols; y++)
grid[x][y] = 0;

}

/************************************************************ ***********************/

Here is what is in my in my input file(inFile:
8 5 7 7 7 4 6 1 5 3 3 6 1 6 2 2
5 3 3 6 2 4 4 8 1 2 6 1 8 5 7 7
1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8
8 1 7 2 6 3 5 4 4 5 3 6 2 7 1 8

And here is the output: The board grid is correct on the output, which here it looks out of line. Guess it has to do with the editor in here.

Will QUEENS Live or Die?
------------------------
85 77 74 61 53 36 16 22
+---+---+---+---+---+---+---+---+
| | | | | | Q | | |
+---+---+---+---+---+---+---+---+
| | Q | | | | | | |
+---+---+---+---+---+---+---+---+
| | | | | | Q | | |
+---+---+---+---+---+---+---+---+
| | | | | | | | |
+---+---+---+---+---+---+---+---+
| | | Q | | | | | |
+---+---+---+---+---+---+---+---+
| Q | | | | | | | |
+---+---+---+---+---+---+---+---+
| | | | Q | | | Q | |
+---+---+---+---+---+---+---+---+
| | | | | Q | | | |
+---+---+---+---+---+---+---+---+
88 36 24 48 12 61 85 77
+---+---+---+---+---+---+---+---+
| | Q | | | | | | |
+---+---+---+---+---+---+---+---+
| | | | Q | | | | |
+---+---+---+---+---+---+---+---+
| | | | | | Q | | |
+---+---+---+---+---+---+---+---+
| | | | | | | | Q |
+---+---+---+---+---+---+---+---+
| | | | | | | | |
+---+---+---+---+---+---+---+---+
| Q | | | | | | | |
+---+---+---+---+---+---+---+---+
| | | | | | | Q | |
+---+---+---+---+---+---+---+---+
| | | | | Q | | | Q |
+---+---+---+---+---+---+---+---+
88 22 33 44 55 66 77 88
+---+---+---+---+---+---+---+---+
| | | | | | | | |
+---+---+---+---+---+---+---+---+
| | Q | | | | | | |
+---+---+---+---+---+---+---+---+
| | | Q | | | | | |
+---+---+---+---+---+---+---+---+
| | | | Q | | | | |
+---+---+---+---+---+---+---+---+
| | | | | Q | | | |
+---+---+---+---+---+---+---+---+
| | | | | | Q | | |
+---+---+---+---+---+---+---+---+
| | | | | | | Q | |
+---+---+---+---+---+---+---+---+
| | | | | | | | Q |
+---+---+---+---+---+---+---+---+
88 72 63 54 45 36 27 18
+---+---+---+---+---+---+---+---+
| | | | | | | | Q |
+---+---+---+---+---+---+---+---+
| | | | | | | Q | |
+---+---+---+---+---+---+---+---+
| | | | | | Q | | |
+---+---+---+---+---+---+---+---+
| | | | | Q | | | |
+---+---+---+---+---+---+---+---+
| | | | Q | | | | |
+---+---+---+---+---+---+---+---+
| | | Q | | | | | |
+---+---+---+---+---+---+---+---+
| | Q | | | | | | |
+---+---+---+---+---+---+---+---+
| | | | | | | | Q |
+---+---+---+---+---+---+---+---+
Press any key to continue


As you can see, I am not getting the correct output for the 1st pairs in the 2nd, 3rd, and 4th chess boards. I know the above code is incorrect, but I tried placing the inFile>>x>>y; in many diff. places w/ no luck. After the first 8 pairs are read in, it processes the data correctly, but when it gets the next 8 pairs, the first pair in each group is not correct. Like I said, I've tried many different ways to fix this with no luck. This is the closest I've gotten to the actual output.
Any help would be appreciated. THANKS!!