Wikipedia entry: Langton’s ant is a 2D universal Turing machine with a very simple set of rules but complex emergent behavior, invented by Chris Langton in 1986.
Squares on a plane are colored variously either black or white. One square is identified as the “ant” which 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° clockwise, flip the color of the square, move forward one unit
- At a black square, turn 90° counter-clockwise, flip the color of the square, move forward one unit
I recreated that program in Python using turtle (so it’s more like Langton’s turtle here) and tkinter. In addition I selected a red color instead of black.
For a more convenient way to change parameters, I wrote a function to define how the next step is defined:
- tileColor is the detected color of the tile the turtle is currently on
- colors is an array of predefined colors to be used
- R/L right or left turn
- 90 turn degree
- tileSize is the length of a step, which here is the same as one tile
doStep(turtlePos, tileColor, colors[0], 'R', 90, tileSize, colors[1])
doStep(turtlePos, tileColor, colors[1], 'L', 90, tileSize, colors[0])
After around 11.000 steps, the ant starts to repeat the same pattern over and over. For the program I defined a function which stops the program it as soon as the ant reaches the grey borderframe.
For the next variation I played around with different patterns and colorsets. The pattern below still starts with the default rules which are extended by 2 colors and followed by the same ruleset but inversed:
In short it’s a RLLR pattern
detect colors [white, black, green, red]
set colors [black, green, red, white]
Other interesting patterns:
- Grid buildup from outside a side, in the order:
- top, right, left, bottom.
- Changes:
- When doing a right-turn move forward 2 steps, not just 1
- Grid buildup in clockwise order
- Changes:
- When doing a right-turn move forward 2 steps, not just 1
- When doing a left-turn, turn 180 degree around instead
- To reverse the creation to counter clockwise, the right-turn and left-turn changes just need to be flipped
- Semi-random build up
- Changes:
- When doing a left-turn, turn 180 degree around instead and move forward 2 units
- Rather chaotic and ends quite fast by reaching the left border
- The end-result looks like this:
- Starts to repeat itself very quickly.
- Same rules as the 2nd video above, only the second left-turn rule is changed to do a 180 turn and move 2 units
- If the rule change from above is moved to the third rule instead (the second left-turn), it results in this