Change language

Python | Langton’s ant

Langton’s Ant — it is a four-dimensional universal Turing machine with four states. It was invented by Chris Langton in 1986. It is basically an ant sitting on a square lattice of cages that are initially white in color. The ant moves along the plane and changes the color of the cells, creating patterns on it. But the movement of the ant is not accidental; follows the following set of rules:

  • If an ant is on a black square, it turns right 90 degrees and moves forward one unit.
  • If an ant is on a white square, it rotates left 90 degrees and moves forward one unit.
  • When the ant leaves the square, it inverts the color.

As the ant begins, it creates a black and white pattern in time of movement. Initially, the changes are not distinctive, but as we repeat this over and over, a beautiful pattern emerges. But if we increase the number of iterations even more (say ~ 10,000), the ant starts repeating its path with a gradual shift instead of creating new patterns. This gives us an infinite highway-like model. The ant continues along this highway and displays the following diagram.

/ figure>

See a visual explanation of Langton’s ant from here . This helps to visualize exactly how the ant works.

The Python-3 code for Langton’s ant is below:

# import turtle module

import turtle

  

def langton ():

 

# Window initialization

window = turtle.Screen ()

window.bgcolor ( ’ white’ )

window .screensize ( 1000 , 1000 )

 

# Contains coordinate and color

maps = {}

 

# Ant initialization

  ant = turtle.Turtle ()

 

# ant shape

ant.shape ( ’square’

  

  # ant size

ant.shapesize ( 0.5 )

 

# ant speed

ant.speed ( 10000

 

# gives the coordinates of the ant

pos = coordinate (ant) 

 

while  True :

 

# distance the ant will move

step = 10  

if pos not in maps or maps [pos] = = " white " :

 

# inverts the color

ant.fillcolor ( "black"

 

# puts a copy of the ant on the canvas

ant.stamp () 

invert (maps, ant, "black" )

ant.right ( 90 )

 

# moves the ant forward

ant.forward (step) 

pos = coordinate (ant )

  

elif maps [pos] = = " black " :

ant.fillcolor ( "white" )

invert (maps, ant, "white" )

 

ant.stamp ()

ant.left ( 90 )

ant.forward (step)

pos = coordinate (ant)

  

def invert (graph, ant, color):

graph [coordinate (ant)] = color

 

def coordinate (ant):

return ( round (ant.xcor ()), round (ant.ycor ()))

 
langton ()

Output:

White cells are indicated as "" (space) and black cells are — like “(dot).

Shop

Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best laptop for development

$499+
Gifts for programmers

Best laptop for Cricut Maker

$299+
Gifts for programmers

Best laptop for hacking

$890
Gifts for programmers

Best laptop for Machine Learning

$699+
Gifts for programmers

Raspberry Pi robot kit

$150

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically