Langton’s Ants, a complex world


I°) What if the world was more complex…?

In the first part, we saw that the world where the ant live is composed by two states (white and red in my video).
It is possible to implement some others rules, which means some others states. The ant will need to choose between his left and right.

For this, we need to change two things in the previous code made : the number of states, and the corresponding colors. Let’s start by a world with 3 states : “0”, “1” and “2”.

In the draw() method :

void draw() {
  background(255);
  displayGridAndColor();

  for (int i = 0; i < nbrCycle; i++) {
    int state = grid[x][y];
    if (state == 0) {
      turnRight();
      grid[x][y] = 1;
    } else if (state == 1) {
      turnLeft();
      grid[x][y] = 2;
    } else if (state == 2) {
      turnLeft();
      grid[x][y] = 0;
    }
    moveForward();
  }
}

And in the displayGridAndColor() method :

void displayGridAndColor() {
  stroke(0);
  strokeWeight(1);
  beginShape(QUAD);
  for (int i =0; i < w; i++) {
    for (int j =0; j < h; j++) {
      if (grid[i][j] == 0) {
        fill(255);
      }
      if (grid[i][j] == 1) { // RED
        fill(255, 0, 0);
      }
      if (grid[i][j] == 2) { // GREEN
        fill(0, 255, 0);
      }

      vertex(i*rows, j*cols);
      vertex(i*rows, (j+1)*cols);
      vertex((i+1)*rows, (j+1)*cols);
      vertex((i+1)*rows, j*cols);
    }
  }
  endShape();
}

Here, we indicate to the ant that it needs to follow theses rules :

  • Turn right if the cell state is “0”, cell is white,
  • Turn left if the cell state is “1”, cell is red,
  • Turn left if the cell state is “2”, cell is green.


II°) Case 3 states : Right – Left – Left (RLL)

We get the following result after some iterations :

This image has an empty alt attribute; its file name is langtons-ant-3-states.png
Langton’s ant – 3 states – RLL

Surprinsigly, thoses rules allow a compact structure which tend to expend itself.


III°) Case 3 states : Right – Left – Right (RLR)

We get the following result after some iterations :

This image has an empty alt attribute; its file name is langtons-ant-3-states-dgd.png
Langton’s ant – 3 states – RLR

Here, the result is chaotic.


IV°) Case 7 states : (LRLRLRL)

We get the following result after some iterations :

This image has an empty alt attribute; its file name is langtons-ant-7-states-lrlrlrl.png
Langton’s Ant – LRLRLRL


V°) Case 30 states :
LRLRLRLRLRLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL

We get the following result after some iterations :

This image has an empty alt attribute; its file name is langtons-ant-30-states.png
Langton’s Ant – 30 states – Toric world

In this case, there is a fast convergence (< 100 iterations) to a structured behaviour with an “highway” pattern. The figure above is possible since this world isn’t flat but toric. The left is link to the right, and the top at the bottom. If this world was flat, we would have get :

This image has an empty alt attribute; its file name is langtons-ant-30-states-partie-nc2b02.png
Langton’s Ant – 30 states – Flat world

Leave a comment

Design a site like this with WordPress.com
Get started
search previous next tag category expand menu location phone mail time cart zoom edit close