I have to write a Conways game of life simulation for a programming module in college. The program works in the fact that in correctly calculates number of neighbors each iteration. How it should work is:
Current State Neighbors Next State
Alive 2 Alive
Alive 3 Alive
Alive <2 Dead
Alive >3 Dead
Dead 3 Alive
Every time a cells state is changed its 8 surrounding cells neighbor field is increment or decremented.
public static Cell[][] updateGrid(Cell[][] theMatrix){
Cell[][] copy = new Cell[DIMENSIONX][DIMENSIONY];
for(int x = 0; x < DIMENSIONX; x++){
for(int y = 0; y < DIMENSIONY; y++ ){
copy[x][y] = theMatrix[x][y];
}
}
int increment;
for(int x = 0; x < DIMENSIONX; x++){
for(int y = 0; y < DIMENSIONY; y++ ){
//Underpopulation
if((copy[x][y].alive == false)&&(copy[x][y].neighbours == 3)){
theMatrix[x][y].alive = true;
increment = 1;
theMatrix = addNeighbours(theMatrix, increment, x,y);
}
//Over Population
else if((copy[x][y].alive==true)&&(copy[x][y].neighbours > 3)){
theMatrix[x][y].alive = false;
increment = -1;
theMatrix = addNeighbours(theMatrix, increment, x,y);
}
}
}
return theMatrix;
}
Thanks for taking the time to have a look guys! ~Paul