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
dead -- not 3 live neighbours -- dead
alive-- 2/3 live neighbours -- alive
alive-- not 2/3 live neighbours--dead
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