我的 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;
}
}
这是运行几代后的样子;
它让我想起了“迷宫”规则集,这很奇怪。
我不相信我的aroundLife 计算器有问题,因为当实体周围有 8 个其他实体时,它会返回 8。问题是因为我循环 Y 然后 X 吗?