gpt4 book ai didi

java - 生活游戏规则无法正常运行

转载 作者:行者123 更新时间:2023-12-04 21:28:54 24 4
gpt4 key购买 nike

我的Java人生游戏应用程序具有以下逻辑代码。我有一个问题,就是规则不像默认的Conway的“生命游戏”规则那样工作。我在Wikipedia上阅读了它们,以下是它们;


任何具有少于两个活邻居的活细胞都会死亡,好像是由于人口不足造成的。
任何有两个或三个活邻居的活细胞都可以存活到下一代。
任何具有三个以上活邻居的活细胞都会死亡,就像人满为患一样。
具有正好三个活邻居的任何死细胞都变成活细胞,就像通过繁殖一样。


我试图在下面的代码中复制这些规则,但是它的行为不同于普通的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;
}
}


这是经过两代人后的样子。



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

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

最佳答案

问题在于您在评估需要更改的内容的同时更改了网格。每次更改单元格时,都会在该单元格边界附近的同一遍通过中影响所有将来测试的结果。

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

关于java - 生活游戏规则无法正常运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14142383/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com