# Game of life in C

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,int Z)
{
//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)
{
//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)
{
int N= {{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=
{
{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;
}

• You've shown the initial state and the state after skipping 19 generations. That's not typical for the Game of Life. – 200_success Oct 4 '16 at 7:05
• Not sure what your question is? – Tolani Oct 4 '16 at 7:15

#define clear() printf("\033[H\033[J")

        N[i][j]=Z[i-1][j-1]+Z[i][j-1]+Z[i+1][j-1] \

No need for the \ when starting a new line in the middle of a expression. The semicolon is the delimiter of statements.