Building Conway's Life
With the JavaFX technology it is easy to build your own customized Conway's Game of Life with simple timeline and binding
Understanding the Code
The code shown in Figure 1 creates
a Conway's Game of Life. It's a zero game player. At each step in time, the following transitions occur:
1. Any live cell with fewer than two live neighbours dies, as if by needs caused by underpopulation.
2. Any live cell with more than three live neighbours dies, as if by overcrowding.
3. Any live cell with two or three live neighbours lives, unchanged, to the next generation.
4. Any tile with exactly three live neighbours cells will be populated with a living cell.
Source Code
function getLiveNeighborsCount(theCircleGroup: CircleGroup, ii: Integer): Integer {
var retVal: Integer = 0;
def prevRow = ii - nCells;
def nextRow = ii + nCells;
def left = ii mod nCells;
def right = (ii + 1) mod nCells;
def theSeq = theCircleGroup.content;
if (prevRow >= 0) {
if (left != 0) {
if ((theSeq[
prevRow - 1] as Circle).fill == liveColor) retVal++;
}
if ((theSeq[prevRow] as Circle).fill == liveColor) retVal++;
if (right != 0) {
if ((theSeq[
prevRow + 1] as Circle).fill == liveColor) retVal++;
}
}
if (left != 0) {
if ((theSeq[
ii - 1] as Circle).fill == liveColor) retVal++;
}
if (right != 0) {
if ((theSeq[
ii + 1] as Circle).fill == liveColor) retVal++;
}
if (nextRow < lastRectIndex) {
if (left != 0) {
if ((theSeq[
nextRow - 1] as Circle).fill == liveColor) retVal++;
}
if ((theSeq[nextRow] as Circle).fill == liveColor) retVal++;
if (right != 0) {
if ((theSeq[
nextRow + 1] as Circle).fill == liveColor) retVal++;
}
}
return retVal;
}
Customizing the Code
To further customize the Game of Life, change a few of the attributes.
Source Code
def screenWidth: Integer = 240; def screenHeight: Integer = 320; def interval = .3s; def diameter = 21; def gap = 2;
Vaibhav Choudhary

