$ cd .. — all projects
langtons-ant
A few simple rules and a handful of colors are enough to grow a chaotic, ever-expanding pattern from an empty board.
JavaScript · SCSS · Canvas · Gulp · 2018

# what is langton's ant
Langton's ant is a cellular automaton: it moves across a grid of cells, turns depending on the color of the cell underneath it, and leaves a new color behind. From a handful of such rules, with no plan at all, a complex pattern emerges.
The classic version has two colors and two turning rules — after tens of thousands of steps the ant settles into an endlessly repeating diagonal "highway". This implementation lets you add any number of colors, each with its own rule: turn left or turn right.
# how it works


The panel on the left is the rule list: each colored block pairs a color with a turning direction. Clicking a block opens a color picker, the arrow flips the direction, and "+" adds a new rule with a random color. The board grows automatically toward the ant, so the simulation has no hard boundaries.
moveAnt: function () {
let currentColor = ant.board[ant.antPosition.y][ant.antPosition.x];
currentColor = currentColor || ant.firstColor;
ant.board[ant.antPosition.y][ant.antPosition.x] = ant.colorSequence[currentColor].nextColor;
let direction = ant.setAndDirection(ant.colorSequence[ant.colorSequence[currentColor].nextColor].direction);
if (direction === 'top') {
ant.antPosition.y--;
} else if (direction === 'right') {
ant.antPosition.x++;
} else if (direction === 'down') {
ant.antPosition.y++;
} else {
ant.antPosition.x--;
}
ant.checkAntMargin();
}
# built with
- canvas
- rule panel: color + turn direction
- board that grows automatically
- run / pause loop
- SCSS compiled with Gulp