Stephen Clark Jr.

Langton’s Ant

I wrote an implementation of Langton’s Ant in Javascript, using the canvas element to render the animation. The basic rules are as follows:

Staring with a grid of squares, one square is designated as the “ant” (the red square) and an initial direction is chosen. Each square can is binary, and can be one of two colors. In this case, I’ve started the ant near the center, and the initial direction it “up” (0°). To start, the ant is moved in its’ direction, the color of the square that the ant lands on is flipped, and the direction of the ant changes based on the following:

  • If the ant lands on a white square, flip the color to black and turn the ant 90° to the right, then move the ant forward one square.
  • If the ant lands on a black square, then flip the color, turn the ant 90° to the left and move the ant forward one square.

No <canvas> support

The ant continues on for some time, until it eventually settles into a repeating pattern that takes it off the edge of the canvas.

§