Java 细胞自动机邻域检测
我有一个很简单的问题,但我似乎无法解决。我认为这是一个逻辑错误,与细胞自动机中邻居的检查有关。下面是我每秒运行一次的代码,用于增长和检查邻居:Java 细胞自动机邻域检测,java,logic,cellular-automata,Java,Logic,Cellular Automata,我有一个很简单的问题,但我似乎无法解决。我认为这是一个逻辑错误,与细胞自动机中邻居的检查有关。下面是我每秒运行一次的代码,用于增长和检查邻居: 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++){
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;
}
public void grow(){
单元格[][]下一个=新单元格[100][100];
对于(int row=0;row<(SIZE\u X/SIZE);row++){
对于(整数列=0;列<(大小/大小);列++){
单元格=网格[行][col];
Cell nCell=grid[row][col];//获取
如果(单元格!=null){
int amount=邻居(行,列);//找出有多少邻居处于活动状态/处于活动状态
if(cell.isOn()==true&&amount!=3)//如果当前单元格处于打开状态,但没有3个活动的邻居,则会将其关闭
nCell.onOff(假);
否则,如果(cell.isOn()==false&&&(amount>=1&&amount存在一些问题,但我不确定结果是什么
第一个问题:
if(cell.isOn() == true && amount != 3) // if the current cell is on but doesnt have 3 alive neighbors, it gets turned off
cell.onOff(false);
if(cell.isOn() == false && (amount >=1 && amount <= 5)) // if it is off and has 1-5 alive neighbors it gets turned on
cell.onOff(true);
我们检查单元格(0,0)
,然后字段为:
.*.
选中所有第一行后,字段为:
...
.
然后每个人都死了
解决方案:首先检查每个单元的邻居号码,并将其存储在每个单元中。然后根据规则打开和关闭邻居号码
第三个问题:在字段边上,一些邻居被检查了两次。例如,单元格(0,0)
打开,我们检查邻居的单元格(0,1)
。首先,我们尝试(-1,0)
,它被更改为(0,0)
,并添加到金额中。之后(0,0)
作为左邻居再次被检查,并再次添加到金额中
if(grid[r][c].isOn() == true && (r != row && c != col))
<>这里,你只考虑不在同一行和列的邻居而不是中心单元。因此,考虑4个单元格而不是8个单元格。
你可能是这个意思:
if(grid[r][c].isOn() == true && (r != row || c != col)
所以我实施了一些改变,解决了你所说的问题
关于。但是,另一个问题出现了,这就是它现在造成了一个问题
巨大的金字塔位于起始块的右侧,并缓慢增长
似乎没有任何理由和韵律。Iv更新了我的密码
单元格是一个类吗?因为您直接从网格分配nCell。如果通过引用进行分配,您还可以更改旧网格视图中单元格的值。这样做将创建有扩散到网格右下角趋势的模式
编辑:刚刚意识到这是Java,上面可能不是真的。如果是这样的话,请忽略这一点
编辑2:另外:
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);
if(cell.isOn()==true&&amount!=3)//如果当前单元格已打开,但没有3个活动的邻居,则会将其打开
关
nCell.onOff(假);
else if(cell.isOn()==false&&&(amount>=1&&amount)很抱歉回复太晚。因此,我实施了一些更改,解决了您提到的问题。但出现了另一个问题,即现在它在起始块的右侧形成了一个巨大的金字塔,并且缓慢增长,似乎没有韵律或原因。Iv更新了我的代码
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);