Originally Posted by
JoshR
Code:
...
if (table[ x ][ y ] == '*')
{
if ((a == 3) || (a == 2))
return true;
else
return false;
}
}
I beleve this should be
Code:
if(a == 3)
return true;
else if(a == 2)
f(table[x][y] == '*')
return true;
else
return false;
else
return false;
// or equivilently
return a == 3 || (a==2 && table[x][y] == '*');
That is a life cell is added for every location with exactly 3 neighbors, life cells that have exactly two neighbors survive until the next generation. I also suggest breaking the program down into smaller, yet more idiot-proof functions.
Code:
bool alive(const char table[MAX][MAX], int x, int y) {
return 0 <= x && x < MAX
&& 0 <= y && y < MAX
&& table[x][y] == '*';
}
int neighbors(const char table[MAX][MAX], int x, int y) {
int count = 0;
if(alive(x-1,y-1)) ++count;
if(alive(x,y-1)) ++count;
if(alive(x+1,y-1)) ++count;
if(alive(x-1,y)) ++count;
if(alive(x+1,y)) ++count;
if(alive(x-1,y+1)) ++count;
if(alive(x,y+1)) ++count;
if(alive(x+1,y+1)) ++count;
return count;
}
char next_gen(const char table[MAX][MAX], int x, int y) {
int n = neighbors(table,x,y);
if(n==3 || (n==2 && alive(table,x,y)) return '*';
else return ' ';
}