190 Agent-Based Modeling and Simulation with Swarm
}
}else{
if(num==3){
next_is_alive = true;
}else{
next_is_alive = false;
}
}
}
On the other hand, w riting lattice plane and rules in one class using
“ConwayLife2dImpl” is also pos sible. Let us look at the stepRule method
inside ConwayWorld.java. Here, all coordinates (x, y),
sum += this.getValueAtX$Y(xm1, ym1); // down left
sum += this.getValueAtX$Y(x,ym1); // down
sum += this.getValueAtX$Y(xp1,ym1); // lower right
sum += this.getValueAtX$Y(xm1,y); // left
sum += this.getValueAtX$Y(xp1, y); // right
sum += this.getValueAtX$Y(xm1, yp1); // upper left
sum += this.getValueAtX$Y(x, yp1); // up
sum += this.getValueAtX$Y(xp1, yp1); // upper right
count the number of lives (1) in cells in the eight neighbors. However,
xm1 = (x + xsize - 1) % xsize; // left
xp1 = (x + 1) % xsize; // right
ym1 = (y + ysize - 1) % ysize; // down
yp1 = (y + 1) % ysize; // up
Dividing by xsize, ysize and obtaining the remainder is because it is con-
sidered to be connecting the vertical and horizontal (torus structure) of the
lattice plane. Moreover, the part below calculates the next sta te:
if(this.getValueAtX$Y(x,y)==1) // am I alive (1) ?
newState = (sum==2 || sum==3) ? 1 : 0;
else // if I am dead (0)
newState = (sum==3) ? 1 :0;
Let us try to implement the cellular automaton explained in Section 7.1
by applying Bug (Fig. 7.8). This sample pro gram is a 2 sta te 3 neighbor one-
dimensional automaton, and upda te rules are according to odd parity. In other
words, it takes the majority state in the thr e e cells as the next state. In fact,
a process such as the one given below is performed by laying Bug (object) in
the last line of a two-dimensional FoodSpace grid:
Step1 Bug looks at the state of the bait pla c e d in its neighborhood.