Langton’s Ant is a cellular automaton that models an ant moving on a grid of cells following some very basic rules.
At the start of the simulation, the ant is randomly positioned on a 2D-grid of white cells. The ant is also given a direction (either facing up, down, left or right).
The ant then moves according to the colour of the cell it is currently sitting in, with the following rules:
- If the cell is white, it changes to black and the ant turns right 90°.
- If the cell is black, it changes to white and the ant turns left 90°.
- The ant then moves forward to the next cell, and repeat from step 1.
These simple rules lead to complex behaviours. Three distinct modes of behaviour are apparent, when starting on a completely white grid:
- Simplicity: During the first few hundred moves it creates very simple patterns which are often symmetric.
- Chaos: After a few hundred moves, a big, irregular pattern of black and white squares appears. The ant traces a pseudo-random path until around 10,000 steps.
- Emergent order: Finally the ant starts building a recurrent “highway” pattern of 104 steps that repeats indefinitely.
All finite initial configurations tested eventually converge to the same repetitive pattern, suggesting that the “highway” is an attractor of Langton’s ant, but no one has been able to prove that this is true for all such initial configurations.
Python Code (Using Python Turtle)
Below is our implementation of Langton’s Ant model using Python Turtle. Note that, on a fixed-size 2D grid, we have had to add one rule to the model:
- On a fixed-size 2D-grid, the simulation stops when the ant reaches the edge of the grid.
You can complete this code by adding multiple ants to this model or by starting with a random grid of black and white cells instead of an an empty grid of white cells only.
A more complex extension to this challenge is to add multiple colours to the model (not just black and white cells). See examples on this page.