# Game of Life Problems

• 02-25-2009
emj83
Game of Life Problems
I am having problems coding the game of life for an assignment. I can generate my initial matrix and then subsequently add a border of 0's around it to aid in the summation of live/dead cells.

the criteria is:
dead -- 3 live neighbours -- alive
alive-- 2/3 live neighbours -- alive

i have code outside of main() to calculate the number of dead/live cells

Code:

```#define A 1000 #define B 1000 /*calculates the state of the cell after each generation*/ int state(int v[A][B],int i,int j) {      int k,q,amount=0; /*Counts how many live cells in the neighbourhood*/     for(k=i-1;k<=i+1;k++){         for(q=j-1;q<=j+1;q++){             amount=amount+v[k][q]; } } /*if alive, then die if not 2,3. if dead then remain dead if not 3*/ /*returns a 0 if cell is now dead*/     if((v[i][j]=1 && (amount<2 || amount>3)) || (v[i][j]=0 && (amount<3 || amount>4))) return 0; /*if alive, then remain alive if 2 or 3. if dead then become alive if 3*/ /*returns a 1 if cell is now alive*/     else if((v[i][j]=1 && (amount==2 || amount==3)) || (v[i][j]=0 && amount==3) ) return 1;   }```
and in main() I have a do loop, which incorporates my initial matrix with the border of 0's

Code:

```do     {         for(i=1;i<=n;i++)         {             for(j=1;j<=p;j++)             {                 d=state(v,i,j);                 if(d==0)                     v[i][j]=0;                 else if(d==1)                     v[i][j]=1;             }         } /*Counts the number of generations down each time one has been programmed*/         generations--; /*Stops the program when the new generations number gets to 0*/     }while(generations!=0);```
for some (probably obvious reason) I don't seem to be getting the correct output in my final matrix.

any help would be much appreciated

thanks Emma
• 02-25-2009
vart

do you check somewhere that your indexes not go out of range [0,A-1] when you do i-1, i+1 access?

you have

if(condition1) return 0;
else if(condition2) return 1;

what will be returned if both conditions are false?

if it is not possible why do you need the second if?
• 02-25-2009
tabstop
Your neighbor count counts the cell itself, so is one too high if the cell is alive.
• 02-25-2009
emj83
it shouldn't go out of bounds, i have removed the second else if and changed it to

Code:

``` else return 1;```
this still doesn't rectify my problem though. i am new to c programming and finding it hard to understand what is going on to be honest.

• 02-25-2009
emj83
Quote:

Originally Posted by tabstop
Your neighbor count counts the cell itself, so is one too high if the cell is alive.

Code:

```    if((v[i][j]=1 && (amount<3 || amount>4)) || (v[i][j]=0 && (amount<3 || amount>4))) return 0; /*if alive, then remain alive if 2 or 3. if dead then become alive if 3*/ /*returns a 1 if cell is now alive*/     else  return 1;```
so if i change it to this?

it is still not giving me the final board that I expect
• 02-25-2009
vart
BTW - you should have 2 matrixes - current and next step

in your case you start replacing nodes in the current matrix which disturbes calculation of neighbours for the nodes in the next row
• 02-25-2009
emj83
Quote:

Originally Posted by vart
BTW - you should have 2 matrixes - current and next step

in your case you start replacing nodes in the current matrix which disturbes calculation of neighbours for the nodes in the next row

ok this probably is where I am going wrong. do you think you could give me a hint on how to get going on this please? would be very much appreciated
• 02-25-2009
vart
I would build array of two matrix

and use index = 0,1 and switch it in each iteration
index = 1-index

and when I will count nighbours for a[index]
and update the new values in the matrix a[1-index]