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,
//char fpar == parameters.substr(0,2);
//const int MaxCol = atoi(fpar);
//char spar == parameters.substr(3,2);
//const int MaxCol = atoi(fpar);
//char tpar == parameters.substr(6,2);
//const int MaxCol = atoi(fpar);
//char frpar == parameters.substr(9,2);
//const int MaxCol = atoi(fpar);
const int MaxCol = stoi(parameters.substr(0,2));
const int MaxRow = stoi(parameters.substr(3,2));
const int startCol = stoi(parameters.substr(6,2));
const int startRow = stoi(parameters.substr(9,2));
char a[MaxCol+2][MaxRow+2], b[MaxCol+2][MaxRow+2]; // Defining initial game board with border
}
for(int i=0; i<line4.length(); i++) //hey if it works it works
{
if(line4.substr(i,1)=="*"){
a[startCol+i][startRow+3] = '*';}
else{
a[startCol+i][startRow+3] = ' ';}
//Do note: if a file has more than 10 lines, any lines after 10 will be omitted from the initial pattern, we could easily add more strings to accomidate more lines (rows) and fix the issue
int Gens_to_run;
cout << "Enter number of generatiions to run" << "\n" ;
cin >> Gens_to_run;
//asks user for number of generations to run
//game board is now set from file, now compute the nearest neighbors
int Gens_ran = 0; //counts generations that have been ran
int r = 0;
while (Gens_ran < Gens_to_run && r == 0)
{
for (int i = 1; i < MaxCol+1; i++) //starting at 1 and going to maxrow+1 to stay within borders
{
for (int j = 1; j < MaxRow+1; j++) //starting at 1 and going to maxrow+1 to stay within borders
{
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++)
a[i][j]=b[i][j];
}
else {
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.
1 2
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]
//stores first line of text file then converst each parameter to a character array to use as integers
//-------------------------------------------------------------------------------
const int MaxCol = atoi(Parameter1);
const int MaxRow = atoi(Parameter2);
const int startCol = atoi(Parameter3);
const int startRow = atoi(Parameter4);
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[20]; 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.