你如何检查邻近的细胞?这些是细胞的规则。
这是我到目前为止拥有的JS Bin代码
function shouldBeAlive(row, col) {
if (model[i][j] === true) {
// check if it should stay alive or whether it should die
}
else {
// check whether it should stay dead or come alive
}
}
发布于 2019-03-16 21:59:19
康威的“生命游戏”,曾经做过一次,这就是我是如何做到的:
function shouldBeAlive(row, col) {
var neighbors_alive = 0;
// first, calculate number of alive neighbors
// 1. check if there is a top neighbor
if (row > 0) {
neighbors_alive += model[row - 1][col];
}
// 2. check if there is a bottom neighbor
if (row < model.length - 1) {
neighbors_alive += model[row + 1][col];
}
// 3. check if there is a left neighbor
if (col > 0) {
neighbors_alive += model[row][col - 1];
}
// 4. check if there is a right neighbor
if (col < model[row].length - 1) {
neighbors_alive += model[row][col + 1];
}
// 5. check if there is a top-right neighbor
if (row > 0 && col < model[row].length - 1) {
neighbors_alive += model[row - 1][col + 1];
}
// 6. check if there is a top-left neighbor
if (row > 0 && col > 0) {
neighbors_alive += model[row - 1][col - 1];
}
// 7. check if there is a bottom-right neighbor
if (row < model.length - 1 && col < model[row].length - 1) {
neighbors_alive += model[row + 1][col + 1];
}
// 8. check if there is a bottom-left neighbor
if (row < model.length - 1 && col > 0) {
neighbors_alive += model[row + 1][col - 1];
}
if (model[row][col] === true) {
// check if it should stay alive or whether it should die
if (neighbors_alive < 2 || neighbors_alive > 3) {
return false;
}
return true;
}
else {
// check whether it should stay dead or come alive
if (neighbors_alive === 2) {
return true;
}
return false;
}
}
注意:当将布尔值添加到整数时,它的值会自动转换,true变成1,false变成0。
编辑:对代码的一些编辑:
首先,检查上面函数中的编辑,然后,下面是您的evolve函数应该是什么样子:
function evolve() {
for (var i = 0; i < model.length; i++) {
for (var j = 0; j < model[i].length; j++) {
model[i][j] = shouldBeAlive(i, j); // before, it was shouldBeAlive()
}
}
paintGrid();
}发布于 2019-03-21 00:51:05
让我们把活邻居算进它自己的功能。在下面的一个例子中,我的循环方式有点不同,所以您不必包含所有的if语句。它从任何方向在一个距离内的任何单元格,不包括单元本身。
function getLiveNeighborCount (row, col) {
let result = 0;
let rLow = row == 0 ? 0 : row - 1;
let rHigh = row == model.length -1 ? row : row + 1;
for (let r = rLow; r <= rHigh; r++) {
let cLow = col == 0 ? 0 : col - 1;
let cHigh = col == model[r].length -1 ? col : col + 1;
for (let c = cLow; c <= cHigh; c++)
if (r != 0 || c != 0)
result += model[r][c];
}
return result;
}现在,shouldBeAlive()只关心您的规则:
function shouldBeAlive (row, col) {
var alive = model[row][col];
var lnc = getLiveNeighborCount(row, col);
return (
!alive && lnc != 3 ? false
: lnc == 2 || lnc == 3 ? true
: false
);
}顺便说一下。您可能希望在所有“应”计算发生之前阻止shouldBeAlive的执行。我这么说是因为我不认为左上角的细胞比右下角的细胞更有优先权。他们都应该同时得到他们的价值。因此,请考虑将您的进化功能更改为如下所示:
function evolve() {
let anticipated = [];
for (let r = 0; r < model.length; r++) {
anticipated[r] = [];
for (let c = 0; c < model[r].length; c++)
anticipated[r][c] = shouldBeAlive(r,c);
}
model = anticipated;
paintGrid();
}https://stackoverflow.com/questions/55201813
复制相似问题