细胞自动机检查邻居

时间:2016-11-03 14:03:52

标签: 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++){
            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单元格。在我的代码运行一次之后,所有单元都死了。

如果有人需要更多信息,请随时提问。提前谢谢!

3 个答案:

答案 0 :(得分:0)

有一些问题,但我不确定结果是如何一切都死了。

第一个问题:

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);

假设小区有1个活的邻居。然后第一个子句将其关闭然后第二个将其重新打开。所以&#34;死亡&#34;规则不起作用。解决方案:使用else if

第二个问题:

您在同一个地方检查所有内容。例如,字段是:

**.
*.*

我们检查单元格(0,0)然后字段是:     。*。     

然后在检查完所有第一行后,该字段为:     ...      然后每个人都死:) 解决方案:首先检查每个单元的邻居编号,并将其存储在每个单元中。只有在那之后才按照规则打开和关闭它们。

第三个问题:在字段边缘,一些邻居被检查两次。例如,单元格(0,0)已启用,我们检查单元格(0,1)的邻居。首先,我们尝试将(-1, 0)更改为(0,0)并添加到金额。稍后再次检查(0,0)为neihbour并再次加入金额。

答案 1 :(得分:0)

if(grid[r][c].isOn() == true && (r != row && c != col))

在这里,您只会考虑与您的中心单元格不在同一行和列上的邻居。结果,你考虑4个细胞而不是8个细胞。 你可能意味着这个:

if(grid[r][c].isOn() == true && (r != row || c != col)

答案 2 :(得分:-1)

  

所以我实施了一些改变,解决了你所谈论的问题   关于。虽然出现了另一个问题,现在却出现了问题   起跑线右侧的大型金字塔,慢慢增长   看似没有押韵或理由。我更新了我的代码

Cell是一类吗?因为您直接从网格分配nCell。如果通过引用执行此操作,还可以更改旧gridview中单元格的值。这样做会产生一种倾向于蔓延到网格右下角的模式。

编辑:刚刚意识到这是Java,上面的内容可能并非如此。如果是这样,请忽略这一点。

EDIT2:另外:

                 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);

这与1-5生存3出生规则无关。

相关问题