⚠️ Warning: This is a draft ⚠️

This means it might contain formatting issues, incorrect code, conceptual problems, or other severe issues.

If you want to help to improve and eventually enable this page, please fork RosettaGit's repository and open a merge request on GitHub.

{{collection|Conway's Game of Life}}

I wrote functions that can be used in a rather general way.

```#define CELL(I,J) (field[size*(I)+(J)])
#define ALIVE(I,J) t[size*(I)+(J)] = 1

int count_alive(const char *field, int i, int j, int size)
{
int x, y, a=0;
for(x=i-1; x <= (i+1) ; x++)
{
for(y=j-1; y <= (j+1) ; y++)
{
if ( (x==i) && (y==j) ) continue;
if ( (y<size) && (x<size) &&
(x>=0)   && (y>=0) )
{
a += CELL(x,y);
}
}
}
return a;
}

void evolve(const char *field, char *t, int size)
{
int i, j, alive, cs;
for(i=0; i < size; i++)
{
for(j=0; j < size; j++)
{
alive = count_alive(field, i, j, size);
cs = CELL(i,j);
if ( cs )
{
if ( (alive > 3) || ( alive < 2 ) )
else
ALIVE(i,j);
} else {
if ( alive == 3 )
ALIVE(i,j);
else
}
}
}
}
```

The function '''evolve''' needs a buffer where to store the result, and this buffer must be provided by the user calling the function. An example of usage to test the blinker and the glider is the following.

```#include <stdio.h>

previously, or just copy/paste the given code here */

0,0,0,
1,1,1,
0,0,0
};

#define FIELD_SIZE 45
#define FIELD_GEN 175
char field[FIELD_SIZE * FIELD_SIZE];
char temp_field[FIELD_SIZE*FIELD_SIZE];

/* set the cell i,j as alive */
#define SCELL(I,J) field[FIELD_SIZE*(I)+(J)] = 1

void dump_field(const char *f, int size)
{
int i;
for (i=0; i < (size*size); i++)
{
if ( (i % size) == 0 ) printf("\n");
printf("%c", f[i] ? 'X' : '.');
}
printf("\n");
}

int main(int argc, char **argv)
{
int i;
char *fa, *fb, *tt, op;

/* fast and dirty selection option */
if ( argc > 1 )
{
op = *argv[1];
} else {
op = 'b';
}

switch ( op )
{
case 'B':
case 'b':
{
tt = fb; fb = fa; fa = tt;
}
return 0;
case 'G':
case 'g':
for(i=0; i < (FIELD_SIZE*FIELD_SIZE) ; i++) field[i]=0;
/* prepare the glider */
SCELL(0, 1);
SCELL(1, 2);
SCELL(2, 0); SCELL(2, 1); SCELL(2, 2);
/* evolve */
fa = field;
fb = temp_field;
for (i=0; i < FIELD_GEN; i++)
{
dump_field(fa, FIELD_SIZE);
evolve(fa, fb, FIELD_SIZE);
tt = fb; fb = fa; fa = tt;
}
return 0;
default:
fprintf(stderr, "no CA for this\n");
break;
}
return 1;
}
```

The blinker output (reformatted) is simply:

```
...  .X.  ...
XXX  .X.  XXX
...  .X.  ...

```