CONWAY GAME OF LIFE

Conway’s game of Life is a type of cellular automata. It is simple enough to use on a monochrome display. The game is set on an infinite plane however computer implementations often use a toroidal universe due to memory and display limitations. A torus is simple to understand, roll a sheet of paper into a tube and this shows the idea in one dimension. Connecting the ends of the tube makes the x and y direction continual. A more advanced treatment can be seen with manifolds.

In mathematics, a manifold is a topological space that locally resembles Euclidean space near each point. More precisely, an n-dimensional manifold, or n-manifold for short, is a topological space with the property that each point has a neighborhood that is homeomorphic to the Euclidean space of dimension n.

LIFE RULES

• Any live cell with fewer than two live neighbours dies, as if by underpopulation.
• Any live cell with two or three live neighbours lives on to the next generation.
• Any live cell with more than three live neighbours dies, as if by overpopulation.
• Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.

These rules, which compare the behavior of the automaton to real life, can be condensed into the following:

• Any live cell with two or three live neighbours survives.
• Any dead cell with three live neighbours becomes a live cell.
• All other live cells die in the next generation. Similarly, all other dead cells stay dead.

It should be noted that RAND is a pseudo random number. Random numbers are important in many areas but the designers neglected to address this problem. RAND simply returns a 0 or 1 to handle the rules of Life. This game and many others depend on good random numbers which has been a field of study of its own.

Following the rules it is not too hard to build a game of Life for your mainframe. Many mainframe machines were running the simulation which was a popular diversion from the day today drudgery.

C VERSION

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#define for_x for (int x = 0; x < w; x++)
#define for_y for (int y = 0; y < h; y++)
#define for_xy for_x for_y
void show(void *u, int w, int h)
{
int (*univ)[w] = u;
printf("\033[H");
for_y {
for_x printf(univ[y][x] ? "\033[07m  \033[m" : "  ");
printf("\033[E");
}
fflush(stdout);
}

void evolve(void *u, int w, int h)
{
unsigned (*univ)[w] = u;
unsigned new[h][w];

for_y for_x {
int n = 0;
for (int y1 = y - 1; y1 <= y + 1; y1++)
for (int x1 = x - 1; x1 <= x + 1; x1++)
if (univ[(y1 + h) % h][(x1 + w) % w])
n++;

if (univ[y][x]) n--;
new[y][x] = (n == 3 || (n == 2 && univ[y][x]));
}
for_y for_x univ[y][x] = new[y][x];
}

void game(int w, int h)
{
unsigned univ[h][w];
for_xy univ[y][x] = rand() < RAND_MAX / 10 ? 1 : 0;
while (1) {
show(univ, w, h);
evolve(univ, w, h);
usleep(200000);
}
}

int main(int c, char **v)
{
int w = 0, h = 0;
if (c > 1) w = atoi(v);
if (c > 2) h = atoi(v);
if (w <= 0) w = 30;
if (h <= 0) h = 30;
game(w, h);
}