Coding Conway’s Game of Life from scratch

Augmentation Lab
5 min readJan 30, 2024

--

Midjourney’s interpretation of Imagine Conway’s Game of Life.

Conway’s Game of Life is one of the most well-known modern-day automatons. This article will talk about automatons and models of computations, using the example of Conway’s game of life. I will walk you through implementing the Game of Life by yourself in Python using nothing more than numpy and pygame libraries.

Abstract art representing Conway’s Game of Life

Understanding Automatons

Automatons are relatively self-operating machines that follow a certain set of instructions that are usually pre-determined. They are sometimes designed specifically to raise the illusion of operating under their control and will. The study of automata theory looks at abstract machines and automata andthe computational problems they can solve.

Conway’s Game of Life

Conway’s Game of Life is a cellular automaton, consisting of a grid of cells, each in one of the states of the system — usually ON or OFF. Each cell has a set of cells called its neighborhood, which is defined relative to the specified cell. The grid has an initial state selected by assigning one of the states for each cell. The rules of the world determine the next state.

In a way, Conway’s Game of Life simulates a virtual world. A world in which there are certain rules, including life and death. It sounds more complicated than it actually is, as Conway’s Game of Life is a straightforward simulation that can generate unbelievable patterns and structures.

Setup

You have a 2D environment — a grid — that has dead cells and living cells. We represent this grid as a matrix of 0s and 1s, each entry representing one grid cell. A cell with 0 is dead, whereas a cell with 1 is alive. We set up a random initial matrix that has both entries with 0 and 1.

Shows the set up for the 2D environment on which we will be running Conway’s Game of Life.

Dynamics

Having the basic cells set up, the question remains: how do these cells evolve? There are 3 extremely basic rules to the Game of Life:

  1. Loneliness — we need other life to survive, we are a social species. Any living cell with less than 2 living neighbors dies.
  2. Overpopulation — the more, the better? Not always. Any living cell with more than 3 living neighbors dies.
  3. Birth — coming together creates. Any dead cell with exactly 3 living neighbors becomes alive.

This means that for each step of our simulation, we have to scan our neighbors to see whether they are alive or dead.

We do this by first initializing on the broad level which directions we can move in:

Directions we could move in when determining the livelihood of the neighborhood of a cell.

We can put this into an array we will iterate over when we go through the cells. Simultaneously, we have to make sure that we don’t check the directions that would violate the boundary conditions (i.e., less than 0 index or more than matrix size index).

# Going from a cell i, this is all the directions you would have to go to scan the neighboring cells

Function setting up the scan of the neighborhood to determine a score of alive cells and dead cells around the current cell.

This snippet of code calculates the score for neighboring cells and we will be able to use this score to determine the next state of the cell (alive or dead).

We determine the next state by checking the state against the rules we defined above:

Function that updates the state of a cell by evaluating the score of the livelihood of the neighbors.

Finally, we need to initialize a game loop in which all of these components are called step by step. Making sure that there won’t be space difficulties for our environment, we must create a copy each time the gameloop is run.

# Overall logic — gameloop to be called

Overall, game loop initiation. Looping through the current environment and its alive & dead cells to then scan each cell and then update it to its next state.

To visualize the entire thing, we will set up a pygame window and then run our gameloop throughout each loop (updating the loop).

Check out the full code here.

The beauty of it

Emergent patterns that have the same behavior over time. Image from: https://pi.math.cornell.edu/~lipa/mec/lesson6.html

Running the simulation many times with different starting conditions, people identified a set of patterns that always enact the same behavior if not touched. This means that if these patterns arise and are separated from any other living system, they will enact these behaviors as time progresses.

We can see that the living system eventually dies out for the first and last patterns.

Interestingly, the second pattern enters a loop where cells live and die but always are in an equilibrium that keeps life alive.

Lastly, the second to last pattern represents a stable space in which the system will not change unless it is being acted on externally. This means that, in itself this dynamic satisfies itself and is self-perserving.

Creating computers in the game of life, Image and post: https://hackaday.com/2020/11/21/a-computer-in-the-game-of-life/

Among other things, Conway’s game of life can create many fascinating, real-world constructs, such as spaceships, information flows, and entire structures just like the computer up there. This is a beautiful website portraying some wonderful patterns that are created through Conway’s Game of Life.

Well, is there more?

If you are interested in more than just Conway’s Game of Life, check out Lenia, another cellular automaton derived from Conway’s Game of Life “by making everything smooth, continuous, and generalized.” (Lenia)

--

--

Augmentation Lab
0 Followers

We are a transdisciplinary student-led lab developing augmentatios to understand & improve the human condition. https://www.augmentationlab.org/