The program we are working on is the "Game of Life". The main problem we are having is storing the board information from the given text file (that we must use).
- how do we get our lines of text to be put directly into our array (skipping the string business) and allow for infinite size (not limited to 10 lines). We think we need to do while loops for this some how but cant figure out how?
-We have to pull the pattern from the given text file (attached), Is there a better way to read the first line form it to get board parameters. we tried to use this bit bellow to change it to integers by using substrings? The first line has four numbers the columns and rows of the board then the column and row to start the initial pattern on. Looks like,
if (a[i-1][j-1] == '*') neighbors += 1;
if (a[i-1][j] == '*') neighbors += 1;
if (a[i-1][j+1] == '*') neighbors += 1;
if (a[i][j-1] == '*') neighbors += 1;
if (a[i][j+1] == '*') neighbors += 1;
if (a[i+1][j-1] == '*') neighbors += 1;
if (a[i+1][j] == '*') neighbors += 1;
if (a[i+1][j+1] == '*') neighbors += 1;
//counts number of neighbors:
if (a[i][j] == '*' && neighbors < 2)
b[i][j] = ' ';
else if (a[i][j] == '*' && neighbors > 3)
b[i][j] = ' ';
else if (a[i][j] == '*' && (neighbors == 2 || neighbors == 3))
b[i][j] = '*';
else if (a[i][j] == ' ' && neighbors == 3)
b[i][j] = '*';
//store new generation in b array
Gens_ran++; //count the number of generation ran
cout << "Press the ENTER key to run next generation or anyother key to quit";
if (cin.get() == '\n')
cout << "\n";
for(int i=0; i<MaxCol+2; i++) //generation "b" becomes "a" generation
for(int j=0; j<MaxRow+2; j++)
r = 1;
cout << "Your game of life is over" << "\n";
Since you are familiar with arrays, I'm very surprised to see the rather long-winded use of variables line1, line2, line3 etc.
You might instead use an array for that purpose too.
The code at present doesn't compile. It uses an undefined function stoi(). It's reasonable to assume that should be atoi(). But it still doesn't compile because atoi() requires a c-string and you are passing a c++ string as a parameter.
That's easily remedied with .c_str().
But still, as you asked, is there an easier way. Well, you could use normal c++ formatted input.
int a, b, c, d;
openfile >> a >> b >> c>> d;
You'll probably have to use openfile.ignore(20,'\n'); or something similar to ignore to the end of the line.
Further down, you use char a[MaxCol+2][MaxRow+2];
This is problematic in standard C++ as the size of the array needs to be a constant, known at compile time. Since you plan to read these values from the file, you are using a variable-length array. Some compilers offer a non-standard extension to allow this, but other compilers will reject it. You may instead have to use the operator new to allocate space.
Beyond that, I've not checked the code in detail. It seems to be going roughly in the right direction, but looks quite inefficient in the way the initial pattern from the file is stored in the array, and not just because of the use of individual line variables.
I have rewrote most of the code but I keep getting these warnings.
bradler2@CF162-10:~/Desktop$ g++ test2.cpp
test2.cpp: In function âint main()â:
test2.cpp:268:16: warning: name lookup of âjâ changed [enabled by default]
test2.cpp:255:14: warning: matches this âjâ under ISO standard rules [enabled by default]
test2.cpp:258:13: warning: matches this âjâ under old rules [enabled by default]
But looking at the code, it still seems just as long-winded and excessively complex as before. I don't understand why you are using substrings and atoi() when you could just use cin to get the integers you need.
If you did that, 22 lines of complex code would reduce to a single line.
And the variables line1 to line19 - just use an array string line; and be done with it.
The associated code where each line is processed will then be correspondingly shorter and simpler. Your 300+ lines of code would be much more concise, easy to read and consequently more likely to be bug-free.
As for the compiler warning messages, notice each message gives the corresponding line number so you should be able to track them down.
Because you haven't used the code formatting tags [code]your code here[/code] its difficult to identify the line numbers here. Please use the <> formatting button on the right.