-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
Create langtons ant algorithm #8967
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
190afd4
updating DIRECTORY.md
c1d7692
Merge branch 'master' of https://github.com/caedenph/python
CaedenPH 1ba195a
feat(cellular_automata): Langonts ant algorithm
CaedenPH b01927c
updating DIRECTORY.md
0506930
Update cellular_automata/langtons_ant.py
CaedenPH 3789e5e
Apply suggestions from code review
CaedenPH 2e77104
fix(langtons-ant): Set funcanimation interval to 1
CaedenPH 02a8c14
Merge branch 'langtons-ant' of https://github.com/caedenph/python int…
CaedenPH File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
""" | ||
Langton's ant | ||
|
||
@ https://en.wikipedia.org/wiki/Langton%27s_ant | ||
@ https://upload.wikimedia.org/wikipedia/commons/0/09/LangtonsAntAnimated.gif | ||
""" | ||
|
||
from functools import partial | ||
|
||
from matplotlib import pyplot as plt | ||
from matplotlib.animation import FuncAnimation | ||
|
||
WIDTH = 80 | ||
HEIGHT = 80 | ||
|
||
|
||
class LangtonsAnt: | ||
""" | ||
Represents the main LangonsAnt algorithm. | ||
|
||
>>> la = LangtonsAnt(2, 2) | ||
>>> la.board | ||
[[True, True], [True, True]] | ||
>>> la.ant_position | ||
(1, 1) | ||
""" | ||
|
||
def __init__(self, width: int, height: int) -> None: | ||
# Each square is either True or False where True is white and False is black | ||
self.board = [[True] * width for _ in range(height)] | ||
self.ant_position: tuple[int, int] = (width // 2, height // 2) | ||
|
||
# Initially pointing left (similar to the the wikipedia image) | ||
# (0 = 0° | 1 = 90° | 2 = 180 ° | 3 = 270°) | ||
self.ant_direction: int = 3 | ||
|
||
def move_ant(self, axes: plt.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]] | ||
""" | ||
directions = { | ||
0: [-1, 0], # 0° | ||
1: [0, 1], # 90° | ||
2: [1, 0], # 180° | ||
3: [0, -1], # 270° | ||
} | ||
CaedenPH marked this conversation as resolved.
Show resolved
Hide resolved
|
||
x, y = self.ant_position | ||
|
||
# Turn clockwise or anti-clockwise according to colour of square | ||
if self.board[x][y] is True: | ||
# The square is white so turn 90° clockwise | ||
self.ant_direction = ( | ||
0 if self.ant_direction == 3 else self.ant_direction + 1 | ||
) | ||
CaedenPH marked this conversation as resolved.
Show resolved
Hide resolved
|
||
else: | ||
# The square is black so turn 90° anti-clockwise | ||
self.ant_direction = ( | ||
3 if self.ant_direction == 0 else self.ant_direction - 1 | ||
) | ||
CaedenPH marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# Move ant | ||
move_x, move_y = directions[self.ant_direction] | ||
self.ant_position = (x + move_x, y + move_y) | ||
|
||
# Flip colour of square | ||
self.board[x][y] = not self.board[x][y] | ||
|
||
if display and axes: | ||
# Display the board on the axes | ||
axes.get_xaxis().set_ticks([]) | ||
axes.get_yaxis().set_ticks([]) | ||
axes.imshow(self.board, cmap="gray", interpolation="nearest") | ||
|
||
def display(self, frames: int = 100_000) -> None: | ||
""" | ||
Displays the board without delay in a matplotlib plot | ||
to visually understand and track the ant. | ||
|
||
>>> _ = LangtonsAnt(WIDTH, HEIGHT) | ||
""" | ||
fig, ax = plt.subplots() | ||
# Assign animation to a variable to prevent it from getting garbage collected | ||
self.animation = FuncAnimation( | ||
fig, partial(self.move_ant, ax, True), frames=frames, interval=0 | ||
CaedenPH marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) | ||
plt.show() | ||
|
||
|
||
if __name__ == "__main__": | ||
import doctest | ||
|
||
doctest.testmod() | ||
|
||
LangtonsAnt(WIDTH, HEIGHT).display() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.