Langton's Ant

We're glad you came by, but you might find what you're looking for elsewhere.

TI-Basic Developer is not the site it once was. While its information on commands and other calculator features remains almost second-to-none, its forum, archives, and even hosting service, Wikidot, have been decaying for years. The calculator community would love to see what you're working on, or help you in your next coding adventure, but TI-Basic Developer is no longer the place to do it.

Instead, you should head over to Cemetech (primarily American) or TI-Planet (primarily international). Both are active, well-established forums with their own archives, chatrooms, reference material, and abundant coding tools and resources. We'll see you there, we hope.

Routine Summary

Simulates Langton's ant on the screen.

Inputs

None

Outputs

None

Variables Used

A, B, C

Calculator Compatibility

TI-83/84/+/SE

Author

Arcane Wizard

URL: United TI

Download

langtonant.zip

:ClrDraw
:31→A:47→B
:randInt(1,4→C
:Repeat getKey
:C-1+2pxl-Test(A,B
:Ans+4((Ans<1)-(Ans>4→C
:Pxl-Change(A,B
:B+(Ans=2)-(Ans=4→B
:A+(C=3)-(C=1→A
:End

Langton's ant is a simulation of the movement of an ant, with some simple rules governing it: the ant can move in any of the four cardinal directions, but whenever it goes over one of the previous spots that it was at, it will change direction 90°. Although the ant movement appears chaotic, given enough time it actually ends up being a pattern.

In our routine, the ant is going to be a simple pixel. Before we can display the ant on the screen, however, we need to clear it and initialize its starting position. (47,31) was chosen because it is the exact center of the screen, which should provide the ant with ample room to move around.

We then randomly select a starting direction for the ant to go — 1 for down, 2 for right, 3 for up, and 4 for left. Once we have a direction selected, we display the ant and it will start moving around. The ant will keep moving around indefinitely until you press a key, although you could limit it to a set number of moves by replacing the Repeat loop with a For( loop.

.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-Noncommercial 2.5 License.