cellular_automata.langtons_ant

Langton’s ant

@ https://en.wikipedia.org/wiki/Langton%27s_ant @ https://upload.wikimedia.org/wikipedia/commons/0/09/LangtonsAntAnimated.gif

Attributes

HEIGHT

WIDTH

Classes

LangtonsAnt

Represents the main LangonsAnt algorithm.

Module Contents

class cellular_automata.langtons_ant.LangtonsAnt(width: int, height: int)

Represents the main LangonsAnt algorithm.

>>> la = LangtonsAnt(2, 2)
>>> la.board
[[True, True], [True, True]]
>>> la.ant_position
(1, 1)
display(frames: int = 100000) None

Displays the board without delay in a matplotlib plot to visually understand and track the ant.

>>> _ = LangtonsAnt(WIDTH, HEIGHT)
move_ant(axes: matplotlib.pyplot.Axes | None, display: bool, _frame: int) None
Performs three tasks:

1. The ant turns either clockwise or anti-clockwise according to the colour of the square that it is currently on. If the square is white, the ant turns clockwise, and if the square is black the ant turns anti-clockwise 2. The ant moves one square in the direction that it is currently facing 3. The square the ant was previously on is inverted (White -> Black and Black -> White)

If display is True, the board will also be displayed on the axes

>>> la = LangtonsAnt(2, 2)
>>> la.move_ant(None, True, 0)
>>> la.board
[[True, True], [True, False]]
>>> la.move_ant(None, True, 0)
>>> la.board
[[True, False], [True, False]]
ant_direction: int = 3
ant_position: tuple[int, int]
board
cellular_automata.langtons_ant.HEIGHT = 80
cellular_automata.langtons_ant.WIDTH = 80