Skip to content

Implementation

Dave Walker edited this page Feb 5, 2020 · 1 revision

Ant Movement

The direction in which the ant is facing is stored as an integer index, as follows:

Direction Index
0
1
2
3

The index is used to references cells in the following movement matrix that supply values used to calculate the change in position and the direction in which the ant should face next, based on the colour of the cell it is currently occupying.

Given a direction index, D, if the current cell is white, the values subscripted "w" are used to determine the change in X and Y and the new direction the ant should face in (ΔXw, ΔYw and Dn,w, respectively).

If the current cell is black, the values subscripted "b" are used.

D ΔXw ΔYw Dn,w ΔXb ΔYb Dn,b
0 -1 0 1 1 0 3
1 0 -1 2 0 1 0
2 1 0 3 -1 0 1
3 0 1 0 0 -1 2

The movement matrix, above, is implemented as a one-dimensional array and the cell colour is held as a numeric value, C, of 0 for white and 1 for black, and the ant’s movement at each step in the simulation is calculated as follows:

x = x + ( D × 6 ) + ( C × 3 )

y = y + ( D × 6 ) + ( C × 3 ) + 1

Dn = ( D × 6 ) + ( C × 3 ) + 2

The Ants Universe

The window onto the universe is considered square and is represented as a zero-based one-dimensional array of integer values, U, such that the array element corresponding to the ant’s current position can be calculated, viz:

Pant = ( y × w ) + x

Where “w” is the width of the window onto the universe and the ant’s x and y co-ordinates are measured from the origin, (0,0).

Ending the Simulation

Once started, the simulation continues until one of the following conditions is met:

  • The movement of the ant takes it outside the declared bounds of the universe
  • The maximum number of steps supplied to the simulator is reached
Clone this wiki locally