BEGIN TYPING YOUR SEARCH ABOVE AND PRESS RETURN TO SEARCH. PRESS ESC TO CANCEL

Langton’s Ant Animation

Langton’s Ant is a two-dimensional Turing machine with a very simple set of rules but complicated emergent behavior. It was invented by Chris Langton in 1986 and runs on a square lattice of black and white cells.


Lately, I’ve seen an interesting post about Langton’s Ant on Hacker News. I came across the Hacker News post just after I had started reading The Hacker and The Ants. So, I checked it out and I liked it so much that I decided to create an animation of it using Javascript and the HTML5 Canvas (My first time using them).

The idea is to draw an ant on a grid. The ant moves around the grid and the ant can travel in any of the four cardinal directions at each step it takes. The ant moves according to the rules below:

  • At a white square, turn 90° right, flip the colour of the square, move forward one unit
  • At a black square, turn 90° left, flip the colour of the square, move forward one unit

Here is an example of the first 200 moves for Langton’s Ant:

Basic

The animation, which I’ve created, can be found on Github. Although my version is quite basic, the ant eventually creates a highway and bursts out of the canvas, which is the expected behaviour after a few hundred moves. For example:

L-ant

Possibilities

It can lead to many interesting behaviours and shapes, especially when you introduce more than one ant and boundaries that the ant must move within. Feel free to use my code to get you started.

My Code (One Ant – No boundaries):

You can check out a Demo HERE.

Leave a comment

Please be polite. We appreciate that. Your email address will not be published and required fields are marked