【问题标题】:Game of life rules not working properly生活游戏规则无法正常工作
【发布时间】:2012-12-18 00:29:21
【问题描述】:

我的 Java 生命游戏应用程序有以下逻辑代码。我的问题是这些规则不像默认的康威生命游戏规则。我在Wikipedia 上阅读了它们,它们如下:

  • 任何活细胞少于两个的活细胞都会死亡,好像是由于人口不足造成的。
  • 任何有两三个活邻居的活细胞都可以传给下一代。
  • 任何有超过三个活邻居的活细胞都会死亡,就像过度拥挤一样。
  • 任何只有三个活邻居的死细胞都会变成活细胞,就像通过繁殖一样。

我试图在下面的代码中复制这些规则,但它的行为与普通的康威生命游戏不同;

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

这是运行几代后的样子;

这让我想起了“迷宫”规则集,这很奇怪。

我不相信我的外围生命计算器有问题,因为当实体周围有 8 个其他实体时,它会返回 8。问题是因为我循环 Y 然后 X?

【问题讨论】:

标签: java math conways-game-of-life cellular-automata


【解决方案1】:

问题是您在评估需要更改的内容的同时修改网格。每次更改单元格时,都会影响在该单元格边界的同一通道中所有未来测试的结果。

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

【讨论】:

  • 那么有2个网格,生成完成后再同步?
  • 我希望每次看到这个错误都能得到一个镍币;哎呀,我可能是第一次自己做的。
  • @jackwilsdon 您没有复制数组,而是复制了数组引用。两个变量仍然指向同一个数组。
  • @jackwilsdon 克隆阵列应该足以解除阵列的关联。如果这不起作用,那么其他地方还有另一个问题。
  • @jackwilsdon 如果你仍然有问题,let's chat over here 而不是用 cmets 把这个答案弄得一团糟。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多