首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何检查表中相邻的单元格以及它们是否应该着色

如何检查表中相邻的单元格以及它们是否应该着色
EN

Stack Overflow用户
提问于 2019-03-16 21:48:11
回答 2查看 328关注 0票数 0

你如何检查邻近的细胞?这些是细胞的规则。

  • 如果一个活的细胞少于两个活的邻居,它就会灭亡。
  • 如果一个活的细胞有两个或三个活的邻居,它就会继续生存。
  • 如果一个活的细胞有三个以上的活邻居,它就会灭亡。
  • 如果一个死细胞正好有三个活的邻居,它就会产生。

这是我到目前为止拥有的JS Bin代码

代码语言:javascript
复制
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
  }
}

EN

回答 2

Stack Overflow用户

发布于 2019-03-16 21:59:19

康威的“生命游戏”,曾经做过一次,这就是我是如何做到的:

代码语言:javascript
复制
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变成1false变成0

编辑:对代码的一些编辑:

首先,检查上面函数中的编辑,然后,下面是您的evolve函数应该是什么样子:

代码语言:javascript
复制
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();
}
票数 1
EN

Stack Overflow用户

发布于 2019-03-21 00:51:05

让我们把活邻居算进它自己的功能。在下面的一个例子中,我的循环方式有点不同,所以您不必包含所有的if语句。它从任何方向在一个距离内的任何单元格,不包括单元本身。

代码语言:javascript
复制
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()只关心您的规则:

代码语言:javascript
复制
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的执行。我这么说是因为我不认为左上角的细胞比右下角的细胞更有优先权。他们都应该同时得到他们的价值。因此,请考虑将您的进化功能更改为如下所示:

代码语言:javascript
复制
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();

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55201813

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档