I°) Cohabitation between ants …
So far, we took care of only one ant among a wide world with some changing rules : 2 states, 3 states, 7 states, 30 states…
But what about the cohabitation between the ants? Some strategies are available ;
- The ants don’t interact between them, their actions don’t influence their other ones. It is the strategy N ants, N worlds.
- The ants interact between them. It is the strategy N ants, 1 world.


Strategy N ants, N world.
II°) Code – Strategy N ants, N world.
We will start to modify the way we are coding by creating some type of intelligent data structure which will correspond to the ants and their properties. We will use the Object Oriented Programmation (OOP) by creating a class Ant.
II-1°) Class Ant
In this class, we will take back the main parameters linked to the grid and the ant’s motions.
// **************************************************************************
// PARAMETERS
// **************************************************************************
int[][] grid;
int x;
int y;
color col;
int dir;
int ANT_UP = 0;
int ANT_RIGHT = 1;
int ANT_DOWN = 2;
int ANT_LEFT = 3;
Now, we need to create a constructor, which will be usefull to instanciate our ants. Right now, what i want is : the position of the ant on the grid and his color.
// **************************************************************************
// CONSTRUCTOR
// **************************************************************************
Ant(int x_, int y_, color col_) {
x = x_;
y = y_;
col = col_;
grid = new int[w][h];
grid[x][y] = 1;
}
Finally, we need to add the methods for displaying and moving the ant.
// **************************************************************************
// METHODS
// **************************************************************************
// *******************
void update() {
this.displayGridAndColor();
for (int i = 0; i < nbrCycle; i++) {
int state = grid[x][y];
if (state == 0) {
this.turnRight();
grid[x][y] = 1;
} else {
this.turnLeft();
grid[x][y] = 0;
}
this.moveForward();
}
}
// *******************
void displayGridAndColor() {
stroke(180);
strokeWeight(1);
beginShape(QUAD);
fill(255);
for (int i =0; i < w; i++) {
for (int j =0; j < h; j++) {
if (grid[i][j] == 1) {
fill(col);
circle(i*rows + rows/2, j*cols + cols/2, rows);
}
}
}
endShape();
}
// *******************
void moveForward() {
if (dir == ANT_UP) {
y--;
}
if (dir == ANT_RIGHT) {
x++;
}
if (dir == ANT_DOWN) {
y++;
}
if (dir == ANT_LEFT) {
x--;
}
if (x > w-1) {
x = 0;
} else if (x < 0) {
x = w-1;
}
if (y > h-1) {
y = 0;
} else if (y < 0) {
y = h-1;
}
}
// *******************
void turnRight() {
dir++;
if (dir > ANT_LEFT) {
dir = ANT_UP;
}
}
// *******************
void turnLeft() {
dir--;
if (dir < ANT_UP) {
dir = ANT_LEFT;
}
}
}
II-3°) Main Part
In the main part, we need to add some elements.
In the first place, let’s add the instantiations of the ants : ants1, ants2 and ants3 and their respectives positions, colors.
// Grid Parameters
int w, h;
int rows = 5; // rows size of one square from the grid
int cols = 5; // cols size of one square from the grid
int nbrCycle = 100;
// Ants parameters
int x1, y1;
int x2, y2;
int x3, y3;
Ant ant1, ant2, ant3;
In the setup() part, let’s add :
void setup() {
//size(800, 800);
fullScreen();
w = width/rows;
h = height/cols;
// Start position
x1 = (int)random(0, w);
y1 = (int)random(0, h);
x2 = (int)random(0, w);
y2 = (int)random(0, h);
x3 = (int)random(0, w);
y3 = (int)random(0, h);
ant1 = new Ant(x1, y1, color(255, 0 , 0));
ant2 = new Ant(x2, y2, color(0 , 255, 0));
ant3 = new Ant(x3, y3, color(0 , 0 , 255));
}
And finally, in the draw() part, let’s add the methods for updating the ants at each frame.
void draw() {
background(255);
ant1.update();
ant2.update();
ant3.update();
}
III°) Results – Strategy N ants, N worlds
IV°) Code – Strategy N ants, 1 world
In this strategy, we wish that the ants share the same grid. So, we need to take back the parameter grid[][] from the class Ant. Additionally, the displaying methods will change slightly.
Our class Ant becomes :
class Ant {
// *****************************************************************************
// PARAMETERS
// *****************************************************************************
// Grid parameters
int x;
int y;
color col;
// Motions parameters
int dir;
int ANT_UP = 0;
int ANT_RIGHT = 1;
int ANT_DOWN = 2;
int ANT_LEFT = 3;
// *****************************************************************************
// CONSTRUCTOR
// *****************************************************************************
Ant(int x_, int y_, color col_) {
x = x_;
y = y_;
this.col = col_;
grid[x][y] = 1;
}
// *****************************************************************************
// METHODS
// *****************************************************************************
// ***********************
void update() {
int state = grid[x][y];
if (state == 0) {
this.turnRight();
grid[x][y] = 1;
} else if (state == 1) {
this.turnLeft();
grid[x][y] = 0;
}
this.moveForward();
}
// ***********************
void moveForward() {
if (dir == ANT_UP) {
y--;
}
if (dir == ANT_RIGHT) {
x++;
}
if (dir == ANT_DOWN) {
y++;
}
if (dir == ANT_LEFT) {
x--;
}
if (x > w-1) {
x = 0;
} else if (x < 0) {
x = w-1;
}
if (y > h-1) {
y = 0;
} else if (y < 0) {
y = h-1;
}
}
// ***********************
void turnRight() {
dir++;
if (dir > ANT_LEFT) {
dir = ANT_UP;
}
}
// ***********************
void turnLeft() {
dir--;
if (dir < ANT_UP) {
dir = ANT_LEFT;
}
}
}
And our main part becomes :
// ****************************************************************************
// PARAMETERS
// ****************************************************************************
// Grid parameter
int w, h;
int rows = 5; // rows size of one square from the grid
int cols = 5; // cols size of one square from the grid
int[][] grid;
// Nbr of mouvement a n ant is allow to do during one frame.
int nbrCycle = 25;
// Ant's parameters
Ant ant1, ant2, ant3;
// ****************************************************************************
// SETUP
// ****************************************************************************
void setup() {
//size(800, 800);
fullScreen();
surface.hideCursor();
w = width/rows;
h = height/cols;
grid = new int[w][h];
ant1 = new Ant(w/4, 3*h/4 - 25, color(255, 0, 0));
ant2 = new Ant(3*w/4, 3*h/4, color(0, 255, 0));
ant3 = new Ant(1*w/4, 3*h/4, color(0, 0, 255));
}
// ****************************************************************************
// DRAW
// ****************************************************************************
void draw() {
background(255);
// Update position of the ants, then display it.
for (int i = 0; i < nbrCycle; i++) {
ant1.update();
ant2.update();
ant3.update();
}
// All ants share a common "2D universe". Their interactions then got
// the same action on this universe.
displayGridAndColor(color(139, 0, 0));
fill(255);
noStroke();
rect(0, 0, 400, 100);
textSize(40);
fill(0);
text("Nbr Iterations = " + nbrCycle*frameCount, 20, 60);
}
// ****************************************************************************
// FUNCTIONS
// ****************************************************************************
// ***********************
void displayGridAndColor(color col) {
stroke(180);
strokeWeight(1);
beginShape(QUAD);
for (int i =0; i < w; i++) {
for (int j =0; j < h; j++) {
if (grid[i][j] == 1) {
fill(col);
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();
}
