01. Conway's Game of Life - Coding a life simulation

ยท

6 min read

01. Conway's Game of Life - Coding a life simulation

Introduction

Computers are hardcoded to follow strict rules without any knowledge of what they're doing. They can't create any unpredictable and life-like simulations. But can they? John Horton Conway proved otherwise. With four simple rules, he created a simulation that can emulate lifelike behaviour. This is now called Conway's Game of Life. Implementing this in code is quite simple. In this post, we will try to create this on a computer.

Setup

To start off, Conway's Game of Life is called a cellular automaton. It is a zero player game to put it simply. This means that no external inputs are needed to alter the current state of the system. The change is brought forward by the rules themselves. So the next stage of the simulation depends on the previous state. Let's try to build this! First, where can we develop the simulation? I chose Processing because it is the perfect programming language for visual simulations. If you'd like to know more, check out the Processing website.

GOL-1.png

Each cell can be dead or alive. So in our code, we will create a 2D array of booleans called cells.

boolean cells[][] = new boolean[cellsinRow ][cellsinRow];

Now there is a catch though. Consider the 1st cell, it can have neighbours on the right and bottom only, not all around it. In fact, all the cells on the border have this issue. This can create issues when we check the rules of the game because we have to check all neighbours and different cells have different kinds of neighbours. We can fix this in code but this can get really complicated. Instead, we will apply an easy and dirty fix to it. Let's create an extra border of cells around the grid. This border of cells will not be considered cells in the simulation. But when checking the rules on each cell, for the border cells we now have other cells bordering them, eliminating the border issue.

boolean cells[][] = new boolean[cellsinRow + 2][cellsinRow + 2];

In processing, we can use the rect() function to draw rectangles to show each cell in the grid. If we use a nested loop for rows and columns, we can easily go through all the cells in the grid. Now we must remember the border issue. So make sure that in all of these nested loops, we start with 1 instead of 0 and end at x - 1 instead of x to discard the hidden border. We will also use two colours to show if the cell is alive or dead. Here, we'll use white (#FFFFF) to indicate dead and green (#61DB68) to indicate alive.

I created an array of colours beforehand to save us the trouble.

color colourPallette[] = {#FFFFFF, #61DB68};

With this, we don't have to use if conditionals to set the colour of the cell.

fill(colourPallette[int(cells[i][j])]);
rect((cellsinRow / 2) + ((i - 1) * cellSize), (cellsinRow / 2) + ((j - 1) * cellSize), cellSize, cellSize, 3);

We will also implement a way to pause and change the state of cells according to our liking because it is important in the Game of Life. We will make a variable work which, if true, will simulate the game, if not we can move around clicking the cells to kill them or revive them. We'll implement this using the mouse clicked and key pressed functions of Processing.

If the spacebar is pressed, we will pause or continue the simulation. If we click on a cell, we will flip its state.

cells[(mouseX * cellsinRow / width) + 1][(mouseY * cellsinRow / width) + 1] = !cells[(mouseX * cellsinRow / width) + 1][(mouseY * cellsinRow / width)  + 1];

The Rules Themselves

Now for the important part. There are four rules that Conway proposed for the Game of Life:

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

We will implement this in code. As we see, the first three rules apply to live cells. So we will first check if the cell we're checking is alive. If so, we'll then check if the cell has two or three live neighbours. If so, we will let it continue to live, and otherwise, we'll kill it. Next, if the cell is alive, we simply have to check if there are exactly three live neighbours. If so, we revive the cell and otherwise, we let it continue to be dead.

Implementing this in code is easy now:

  • Check if the cell is alive
  • If so, check if the cell does not have exactly two or three neighbours
  • If so, kill it.
  • Otherwise, continue
  • If dead, check if the cell has exactly three neighbours
  • If so, revive it
  • Otherwise, continue

Voila! We have the Game of Life! Now there is a caveat, we need to do this check for each cell. Imagine we're at the (2, 3) cell. It is alive and now we have to kill it. Now we have to go to the next cell, (2, 4). When we check the neighbours, we get one less live neighbour than the actual neighbours, because we just killed (2, 3) neighbour. Conway's game pretends that each cell decides to die or live simultaneously. One will not wait till the neighbour completes the decision. But in a computer, we have to check each one serially (Forget parallel computing for now). Fortunately, the fix is super simple.

Well create a temporary cell grid, and for all the cells, instead of changing the state of the core cell grid, we will set the state of each cell of this cell grid. Once all cells are done, well set the original cell grid to this new grid. This is like each cell one by one deciding whether it is going to live or die one by one and once all the cells have completed the decision, changes their state simultaneously.

And this actually completes the Game of Life. We simply need to run the code, set any pattern we like, then run the simulation. Some interesting things happen when we let it run.

GOL.gif

GOL2.gif

If you'd like to check out the raw code, you can find it in my Github Repository. Go ahead and make your own interesting simulations!

External Links:

ย