我有一个非常简单的问题,但我似乎无法弄清楚。我认为这是与元胞自动机中的邻居检查有关的逻辑错误。这是我的代码,每秒运行一次,用于增长和检查邻居:
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;
}
我在我的元胞自动机中使用了一个简单的 12345/3(生存/出生)规则。
目前的问题是我有一个 100x100 的网格,中心有一个 10x10 的 ALIVE/ON 单元格空间。在我的代码运行一次后,所有的细胞都死了。
如果有人需要更多信息,请随时询问。提前致谢!