I'm attempting to write the code for Conway's Game of Life for my C++ class (out of '.' '*' and '@') Everything compiles fine, yet it doesn't do what I want. I've been pouring over the damn thing, and I thought you guys might be kind enough to help out.
Where i is the row, t is column, M is the array storing the number of live cells. I think the problem is in what I've posted here, or the functions, which I will post in a second. When I run this all it does is output the original grid of cells, even though my functions should have changed them in SOME way.
for (int i = 1; i <= n; i++)
{
for (int t = 1; t <= n; t++)
{
M[i][t] = count(A, n, i, t);
}
}
for (int x = 0; x >= steps; x++)
{
grow(A, M, n, i, t);
}
output_array(A, n);
Name:
Anonymous2008-03-01 21:24
The aforementioned functions. They are all prototyped and everything is declared properly.
//Live cells function.
bool is_alive(char array[MAX_SIZE][MAX_SIZE], int size, int row, int col)
{
if (row < 0 || row >= size || col < 0 || col >= size)
{ return false; }
else if (array[row][col] == '.')
{ return false; }
else
{ return true; }
}
//Count function.
int count(char array[MAX_SIZE][MAX_SIZE], int size, int row, int col)
{
int lives;
for (row = row-1; row == row+1; row++)
{
for (col = col-1; col == col+1; col++)
{
if (is_alive(array, size, row, col) == true)
{
lives = lives + 1;
}
}
}
return lives;
}
//Growth function.
void grow(char array[MAX_SIZE][MAX_SIZE], int M[MAX_SIZE][MAX_SIZE], int size, int row, int col)
{
for (row = 1; row <= size; row++)
{
for (col = 1; col <= size; col++)
{
array[row][col] == '@';
if (array[row][col] == '.' && M[row][col] == 3)
{
array[row][col] = '*';
}
else if (M[row][col] == 0 || M[row][col] >= 4)
{
array[row][col] = '.';
}
}
}
Name:
Anonymous2008-03-01 21:30
It's because you have a shitty indentation style.
Name:
Anonymous2008-03-01 21:36
It was better before copy paste, but a valid point none the less! I'll go check that.
>>12
Try thinking about what that code does. It will never run. And if it would, it wouldn't terminate anyway.
Name:
Anonymous2008-03-01 22:35
Shouldn't it evaluate from the given row-1 until row+2? The function's intent is to check the eight cells around one.
(around the center cell, which would be (row, col))
...
...
...
Wouldn't putting that function in this loop
for (int i = 1; i <= n; i++)
{
for (int t = 1; t <= n; t++)
{
M[i][t] = count(A, n, i, t);
}
}
I realized the array's started at 0 a while back, but for all intents and purposes I didn't think it would make that much of a difference while I was doing the actual coding. I plan on fixing it once there is some kind of effective output.
>>22 Why won't that work? It seems like it makes sense to me.
>>32
Fucking owned. That's what you get for posting Anonymously.
Next time learn to use a goddamn debugger and work the fucking logic errors yourself instead of coming here and bothering us with your stupid fucking Sepples problem. Help yourself, thus help us, and we will help you in return (by raping you up the ass with beautiful Konata ASCII art she is my waifu bampu pantsu etc).