# Thread: Game of Life help needed

1. ## Game of Life help needed

Code:
#include <iostream.h>
const int ROW = 7;
const int COL = 7;

void main()
{

char M[ROW][COL]={
{' ',' ',' ',' ',' ',' ',' '},
{' ',' ',' ','*','*',' ',' '},
{' ',' ','*',' ','*',' ',' '},
{' ',' ',' ',' ','*',' ',' '},
{' ',' ','*',' ',' ',' ',' '},
{' ',' ','*',' ','*',' ',' '},
{' ',' ',' ',' ',' ',' ',' '}
};

int num = 0;
cout << "\nPlease enter the number of generations to print: ";
cin >> num;

cout << "\n\n\tGeneration: " << 0 << endl;
for (int i=1; i<ROW-1; i++){
cout << endl << "\t" << '|';
for (int j=1; j<COL-1; j++){
cout << M[i][j] << '|';
}//for
}//for

cout << endl <<endl;

for (int k=0; k<num; k++){
for (int i=1; i<ROW-1; i++){
for (int j=1; j<COL-1; j++){
if (M[i-1][j]   == '*') num++;
if (M[i-1][j-1] == '*') num++;
if (M[i-1][j=1] == '*') num++;
if (M[i][j-1]   == '*') num++;
if (M[i][j+1]   == '*') num++;
if (M[i+1][j]   == '*') num++;
if (M[i+1][j-1] == '*') num++;
if (M[i+1][j+1] == '*') num++;
}//for
}//for
cout << "\tGeneration: " << k+1 << endl;
for (int i=1; i<ROW-1; i++){
cout << endl << "\t" << '|';
for (int j=1; j<COL-1; j++){
cout << M[i][j] << '|';
}//for
}//for

cout << endl <<endl;
}//for
}//main

Here is my code. When I try to execute this, it will only print the first generation no matter how many generation I enter. What am I doing wrong?

2. It's an endless loop problem:
Code:
for (int k=0; k<num; k++){
for (int i=1; i<ROW-1; i++){
for (int j=1; j<COL-1; j++){
if (M[i-1][j]   == '*') num++;
if (M[i-1][j-1] == '*') num++;
if (M[i-1][j=1] == '*') num++;
if (M[i][j-1]   == '*') num++;
if (M[i][j+1]   == '*') num++;
if (M[i+1][j]   == '*') num++;
if (M[i+1][j-1] == '*') num++;
if (M[i+1][j+1] == '*') num++;
}//for
}//for
You are always evaluating to true on at least one of the if statements and so the value of num is always increased. Thus k will always be less than num and you're stuck in the loop.

David

3. First problem:
>if (M[i-1][j=1] == '*') num++;
This is why it only prints the first generation, infinite loop #1, j is always less than COL-1, program no workie.

Second problem (with first problem corrected):
Code:
for (int k=0; k<num; k++){
for (int i=1; i<ROW-1; i++){
for (int j=1; j<COL-1; j++){
if (M[i-1][j]   == '*') num++;
if (M[i-1][j-1] == '*') num++;
if (M[i-1][j-1] == '*') num++;
if (M[i][j-1]   == '*') num++;
if (M[i][j+1]   == '*') num++;
if (M[i+1][j]   == '*') num++;
if (M[i+1][j-1] == '*') num++;
if (M[i+1][j+1] == '*') num++;
}//for
}//for
See it? k gets incremented by one with every iteration of the outer loop, but num gets incremented quite a bit more than that so k will always be less than num. Infinite loop #2*, program no workie.

*Well, not quite infinite. It'll break on some really high value, but it's still not what you want.

-Prelude

4. ## I think I found the problem

Was it the j = 1 that you were refering to?

5. ## Fixed

I forgot a big part of my code. Sorry guys here is the working code.

Code:
#include <iostream.h>
const int ROW = 7;
const int COL = 7;

int main()
{
char ch = 'X';
char M[ROW][COL]={
{' ',' ',' ',' ',' ',' ',' '},
{' ',' ',' ','*','*',' ',' '},
{' ',' ','*',' ','*',' ',' '},
{' ',' ',' ',' ','*',' ',' '},
{' ',' ','*',' ',' ',' ',' '},
{' ',' ','*',' ','*',' ',' '},
{' ',' ',' ',' ',' ',' ',' '}
};

int num = 0;
cout << "\nPlease enter the number of generations to print: ";
cin >> num;

cout << "\n\n\tGeneration: " << 0 << endl;
for (int i=1; i<ROW-1; i++){
cout << endl << "\t" << '|';
for (int j=1; j<COL-1; j++){
cout << M[i][j] << '|';
}//for
}//for

cout << endl <<endl;

for (int k=0; k<num; k++){
for (int i=1; i<ROW-1; i++){
for (int j=1; j<COL-1; j++){
int num = 0;
if (M[i-1][j]   == '*') num++;
if (M[i-1][j-1] == '*') num++;
if (M[i-1][j+1] == '*') num++;
if (M[i][j-1]   == '*') num++;
if (M[i][j+1]   == '*') num++;
if (M[i+1][j]   == '*') num++;
if (M[i+1][j-1] == '*') num++;
if (M[i+1][j+1] == '*') num++;
if(M[i][j]=='*'){
if((num < 2) || (num >=4))
M[i][j] =' ';
} else if (M[i][j] == ' '){

if (num == 3)
M[i][j] = '*';

}
}//for
}//for
cout << "\tGeneration: " << k+1 << endl;
for (int i=1; i<ROW-1; i++){
cout << endl << "\t" << '|';
for (int j=1; j<COL-1; j++){
cout << M[i][j] << '|';
}//for
}//for

cout << endl <<endl;

}//for
cout << "That's the Game of Life!! Press any key to exit: ";
cin >> ch;
while (ch == '_');
}//main

6. theres one major problem.............post this in the game programming forums

7. No it's fine here. It has more to do with C++ than it has to do with game programming. Moron.