Skip to content
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

Add Langton's Ant Algorithm in Python #1345

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions Langtons Ant/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Langton's Ant

## Algorithm Description

Langton's Ant is a two-dimensional universal Turing machine invented by **Chris Langton** in 1986. It's a simple cellular automaton that, despite its simplicity, exhibits complex emergent behavior.

### Movement Rules:

1. **White Square**:
- **Turn Right** 90 degrees.
- **Flip the color** of the square to **black**.
- **Move forward** one unit.

2. **Black Square**:
- **Turn Left** 90 degrees.
- **Flip the color** of the square to **white**.
- **Move forward** one unit.

Despite the simplicity of these rules, after a large number of steps, the ant starts building a repetitive pattern known as a "highway".

## Origin

The algorithm was first proposed by **Chris Langton**, an American computer scientist, as part of his studies on cellular automata and complex systems. Langton's Ant is a classic example of how simple local rules can lead to complex global behavior.

## Uses and Applications

- **Education**: An excellent tool for teaching concepts of cellular automata, dynamic systems, and emergent complexity.
- **Visualization**: Helps visualize how complex patterns can emerge from simple rules.
- **Research**: Used in studies on chaos theory, fractals, and complex systems.

## Available Implementations

Currently, the implementation is available in **Python**. In the future, we plan to add implementations in other programming languages to increase accessibility and allow more people to experiment with this fascinating algorithm.

## How to Run the Code

### Prerequisites

- **Python 3.x** installed on your system.
- *(Optional)* A terminal that supports Unicode characters for better visualization.

### Execution

1. **Clone the repository or download the `langtons_ant.py` file.**

2. **Open a terminal and navigate to the directory containing the file.**

3. **Run the script with the following command:**

```bash
python langtons_ant.py
96 changes: 96 additions & 0 deletions Langtons Ant/solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import sys
import time
import os

# Define the possible directions: North, East, South, West
# These directions represent the cardinal points and guide the ant's movement on the grid.
# The ant rotates right or left depending on the color of the cell it is currently on,
# changing its direction accordingly.
directions = ['N', 'E', 'S', 'W']

# Corresponding movement vectors for each direction
# This dictionary maps each cardinal direction to a tuple that represents the change in (x, y) coordinates.
# It is used to update the ant's position on the grid based on its current facing direction.
# The grid uses a coordinate system where:
# The x-axis runs horizontally, increasing to the right.
# The y-axis runs vertically, increasing downward.
move = {
'N': (0, -1), # Moving North decreases the y-coordinate by 1 (upward movement)
'E': (1, 0),
'S': (0, 1),
'W': (-1, 0)
}

def turn_right(current_direction):
idx = directions.index(current_direction)
return directions[(idx + 1) % 4]

def turn_left(current_direction):
idx = directions.index(current_direction)
return directions[(idx - 1) % 4]

def print_grid(grid, ant_position):
# Clear the terminal screen
# This command clears the console output to update the grid display for each iteration.
# It uses 'cls' for Windows systems and 'clear' for Unix-like systems.
os.system('cls' if os.name == 'nt' else 'clear')

# Obtain the grid boundaries
# This section calculates the minimum and maximum x and y coordinates
# to determine the visible area of the grid.
# It ensures that all cells that have been visited or modified, including the ant's current position,
# are included when printing the grid to the terminal.
x_positions = [x for x, y in grid.keys()]
y_positions = [y for x, y in grid.keys()]
x_positions.append(ant_position[0])
y_positions.append(ant_position[1])

min_x = min(x_positions)
max_x = max(x_positions)
min_y = min(y_positions)
max_y = max(y_positions)

for y in range(min_y, max_y + 1):
row = ''
for x in range(min_x, max_x + 1):
if (x, y) == ant_position:
row += '🐜' # Ant representation
else:
cell = grid.get((x, y), 'white')
row += '⬛' if cell == 'black' else '⬜'
print(row)
print('\n')

def langtons_ant(steps):
# Initial position of the ant at the center of the grid
# The ant starts at coordinates (0, 0) facing North.
# This central starting point allows equal exploration in all directions.
x, y = 0, 0
direction = 'N'

# The grid is represented as a dictionary with coordinates as keys and colors as values
# This data structure allows us to model the grid dynamically
grid = {}

for step in range(steps):
color = grid.get((x, y), 'white')

if color == 'white':
direction = turn_right(direction)
grid[(x, y)] = 'black'
else:
direction = turn_left(direction)
grid[(x, y)] = 'white'

# Move to the next position based on the current direction
dx, dy = move[direction]
x += dx
y += dy

# Print the grid with the ant's current position
print_grid(grid, (x, y))
time.sleep(0.05) # Control the speed of the simulation

if __name__ == "__main__":
steps = 2000 # Maximum number of steps for the ant to take
langtons_ant(steps)