Game of life is the simplest example of zero player game for cellular automata and it was proposed by mathematician John Conway. I have tried to re implement Nicolas P. Rougier's tutorial for python code with arrays for game of life in C.
#include<stdio.h>
#define clear() printf("\033[H\033[J")
void compute(int N[6][6],int Z[6][6])
{
//compute neighbours
int i,j;
for (i = 1; i < 5; i++)
{
for (j = 1; j < 5; j++)
{
N[i][j]=Z[i-1][j-1]+Z[i][j-1]+Z[i+1][j-1] \
+ Z[i-1][j] +Z[i+1][j] \
+ Z[i-1][j+1]+Z[i][j+1]+Z[i+1][j+1];
}
}
}
void show(int Z[6][6])
{
//show output
int i,j;
for (i = 0; i < 6; i++)
{
for (j = 0; j < 6; j++)
{
printf("%d ", Z[i][j]);
}
printf("\n");
}
}
void iteration(int Z[6][6])
{
int N[6][6]= {{0}};
int i,j;
compute(N,Z);
for (i = 0; i < 6; i++)
{
for (j = 0; j < 6; j++)
{
if((Z[i][j] == 1)&&(N[i][j] < 2 || N[i][j] > 3))
{
Z[i][j] = 0;
}
else if((Z[i][j] == 0)&&(N[i][j] == 3))
{
Z[i][j] = 1;
}
}
}
}
int main()
{
/* 2D arraj declaration*/
int Z[6][6]=
{
{0, 0, 0, 0, 0, 0},
{0, 0, 0, 1, 0, 0},
{0, 1, 0, 1, 0, 0},
{0, 0, 1, 1, 0, 0},
{0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0},
};
int i;
show(Z);
printf("\n");
for(i=0; i<20; i++)
{
//clear(); //clear screen
iteration(Z);
}
show(Z);
return 0;
}