I have a pretty simple problem but I cant seem to figure it out. I believe it is a logic error having to do with the checking of neighbors in cellular automata. Here is my code that runs once a second for growing and checking neighbors:
public void grow(){
Cell[][] next = new Cell[100][100];
for(int row = 0; row < (SIZE_X/SIZE); row++){
for(int col = 0; col < (SIZE_Y/SIZE); col++){
Cell cell = grid[row][col];
Cell nCell = grid[row][col]; // gets
if(cell != null){
int amount = neighbors(row, col); // find out how many neighbors are ALIVE/ON
if(cell.isOn() == true && amount != 3) // if the current cell is on but doesnt have 3 alive neighbors, it gets turned off
nCell.onOff(false);
else if(cell.isOn() == false && (amount >= 1 && amount <= 4)) // if it is off and has 1-5 alive neighbors it gets turned on
nCell.onOff(true);
next[row][col] = nCell;
}
}
}
grid = next;
}
public int neighbors(int row, int col){ // checks the amount of neighbors that are ALIVE/ON
int amount = 0;
for(int r = row-1; r <= row+1; r++){ // stepping through a 3x3 area of the grid, which surrounds the selected block
for(int c = col-1; c <= col+1; c++){
// clamp
if((r > 0 && r < 99) && (c > 0 && c < 99)){
if(grid[r][c].isOn() == true && (r != row && c != col)) // checks if the current neighbor is ALIVE/ON
amount++; // if it is then add one to the count
}
}
}
return amount;
}
Im using a simple 12345/3(Survival/Birth) rule in my Cellular Automata.
The problem currently is I have a 100x100 grid with a 10x10 space of ALIVE/ON cells in the center. After my code runs once, all the cells die.
If anyone needs more information, feel free to ask. Thanks in advance!