A Cellular Automaton meets Love2D

Mansoor Aldosari
3 min readNov 27, 2021

--

The following is an implementation of cellular automata using Love2D (an engine for game development).

So, what is Cellular Automata?

A cellular automaton is a collection of “colored” cells on a grid of specified shape that evolves through a number of discrete time steps according to a set of rules based on the states of neighboring cells. The rules are then applied iteratively for as many time steps as desired. — wolfram.com

Conway’s Game of life is the most popular 2D cellular automata. However, this example is an implementation of 1D cellular automation. Below is the outcome of rule 30 (there are many rules embedded in the code):

Game of life by Conway is one of the most famous 2D cellular automata. However, this example is an implementation of 1D cellular automation. Below is the outcome of rule 30 (there are many rules embedded in the code).

This case represents a stack of rows that represents epochs. Each row is an array of cells (black or white), and epochs start from top to bottom. The first row has one black cell. The next row is the result of the previous row and a rule.

Each cell is determined by looking at the neighboring cells from the last epoch. Let’s assume that the length of each row is 5 for simplicity and the rule:

000 = 0
001 = 1
010 = 1
011 = 1
100 = 1
101 = 0
110 = 0
111 = 0
*The rule 0001 1110 is equivalent to 30 in decimal, hence rule 30.

Now, we set the first epoch to 00100, and the result is 01110.

[epoch 0]    0    0    1    0    0
[epoch 1] 0
[epoch 0] 0 0 1 0 0
[epoch 1] 0 1
[epoch 0] 0 0 1 0 0
[epoch 1] 0 1 1
[epoch 0] 0 0 1 0 0
[epoch 1] 0 1 1 1
[epoch 0] 0 0 1 0 0
[epoch 1] 0 1 1 1 0
*The code assumes 0 on the edge cases.

If we keep applying this simple rule on every row, a very complex behavior will emerge, resulting in the generation of the pattern displayed in the screenshot above!

As you can see, simple rules will evolve into unpredictable patterns. The complete code is linked below, and since the code follows a data-driven approach, you can easily add your rule-set.

function rules (i)
local ruleset = {
{0, 0, 0, 1, 1, 1, 1, 0}, -- Rule 30
{0, 0, 1, 0, 1, 1, 0, 1}, -- Rule 45
{0, 1, 0, 1, 1, 0, 1, 0}, -- Rule 90
{0, 1, 1, 0, 1, 1, 1, 0}, -- rule 110
{1, 0, 1, 1, 0, 1, 1, 0}, -- rule 182
{1, 0, 1, 1, 1, 1, 1, 0}, -- rule 190
{1, 1, 0, 1, 1, 1, 1, 0}, -- rule 222
}
...

It’s a new kind of science. I still remember the excitement I got from realizing the paradigm shift of algorithms generated by algorithms (neural networks). Now cellular automata give me the same vibes, I’m not sure what it will evolve to, but I believe it will be something beautiful.

This story was influenced by Stephen Wolfram, many thanks to him.

--

--

No responses yet