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 :

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 :

Here, the result is chaotic.
IV°) Case 7 states : (LRLRLRL)
We get the following result after some iterations :

V°) Case 30 states :
LRLRLRLRLRLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL
We get the following result after some iterations :

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 :

