生命游戏规则不正常

Jac*_*don 2 java math cellular-automata conways-game-of-life

我在Java中的Game of Life应用程序中有以下逻辑代码.我有一个问题,规则不像默认的康威的生命游戏规则.我已经在维基百科上阅读了它们,它们如下;

  • 任何活的邻居少于两个的活细胞都会死亡,好像是由人口不足造成的.
  • 任何有两三个活邻居的活细胞都会留在下一代.
  • 任何有三个以上活着的邻居的活细胞都会死亡,就像过度拥挤一样.
  • 具有正好三个活邻居的任何死细胞变成活细胞,就好像通过繁殖一样.

我试图在下面的代码中复制这些规则,但它的行为不同于正常的Conway'生命游戏;

int surroundingLife = 0;
if (lifeMap[cX+1][cY]) { //Right
    surroundingLife++;
}
if (lifeMap[cX-1][cY]) { // Left
    surroundingLife++;
}
if (lifeMap[cX][cY+1]) { // Above
    surroundingLife++;
}
if (lifeMap[cX][cY-1]) { // Below
    surroundingLife++;
}
if (lifeMap[cX-1][cY-1]) { // Bottom left
    surroundingLife++;
}
if (lifeMap[cX+1][cY+1]) { // Top Right
    surroundingLife++;
}
if (lifeMap[cX-1][cY+1]) { // Some other corner (I don't know which one)
    surroundingLife++;
}
if (lifeMap[cX+1][cY-1]) { // Yet another corner (I don't know which one)
    surroundingLife++;
}
if (running) {
    // Logic for life
    if (surroundingLife < 2 && lifeMap[cX][cY]) {// Rule 1. Any live cell with fewer than two live neighbours dies, as if caused by under-population.
        lifeMap[cX][cY] = false;
    } else if (surroundingLife == 2 && lifeMap[cX][cY]) { // Rule 2. Any live cell with two or three live neighbours lives on to the next generation.
        lifeMap[cX][cY] = true;
    } else if (surroundingLife == 3 && lifeMap[cX][cY]) { // Rule 3. Same as above
        lifeMap[cX][cY] = true;
    } else if (surroundingLife > 3 && lifeMap[cX][cY]) { // Rule 4. Any live cell with more than three live neighbours dies, as if by overcrowding.
        lifeMap[cX][cY] = false;
    } else if (surroundingLife == 3 && !lifeMap[cX][cY]) { // Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.
        lifeMap[cX][cY] = true;
    }
}   
Run Code Online (Sandbox Code Playgroud)

这就是它运行几代后的样子;

GameOfBugs,或BugsOfLife.

它让我想起了"迷宫"规则集,这很奇怪.

我不相信我的surroundLife计算器有问题,因为当实体有8个其他围绕它们时它返回8.问题是由于我循环通过Y然后X?

cdh*_*wie 9

问题是您在评估需要更改的内容时在同一个过程中修改网格.每次更改单元格时,都会影响与该单元格相邻的同一遍中所有未来测试的结果.

您需要制作网格的副本.始终从该副本测试(读取),并将更改(写入)应用于原始副本.

  • 我希望每次看到这个错误都能得到镍; 我可能是第一次自己做的. (3认同)
  • @jackwilsdon你没有制作数组的副本,你制作了数组引用的副本.两个变量仍然指向同一个数组. (3认同)
  • 那么有 2 个网格,并在生成完成后同步它们? (2认同)