简体   繁体   English

生命游戏“细胞”没有正确更新

[英]game of life "cells" not updating correctly

So i am trying to code conway's game of life in javaScript but something isn't working.所以我试图在 javaScript 中编写康威的生活游戏,但有些东西不起作用。 If i but three cells in a row they should just flip directions but instead it makes a 2 by 2 square.如果我连续三个单元格,它们应该只是翻转方向,而是形成一个 2 x 2 的正方形。 I looked around and found that you need to wait to update position until the whole "board" is scanned.我环顾四周,发现您需要等待更新 position 直到扫描整个“板”。 But i atleast think I'm already doing that.但我至少认为我已经在这样做了。 So I genuinely have no clue what is wrong.所以我真的不知道出了什么问题。

I call the uppdateGridValues function to run a generation我调用updateGridValues function 运行一代

 function getCellValue(row, col) { try { return arry[row][col]; } catch (e) { return 0; } } function countNeighbours(row, col) { let neighbours = 0; neighbours += getCellValue(parseInt(row - 1), parseInt(col - 1)); neighbours += getCellValue(parseInt(row - 1), parseInt(col)); neighbours += getCellValue(parseInt(row - 1), parseInt(col + 1)); neighbours += getCellValue(parseInt(row), parseInt(col - 1)); neighbours += getCellValue(parseInt(row), parseInt(col + 1)); neighbours += getCellValue(parseInt(row + 1), parseInt(col - 1)); neighbours += getCellValue(parseInt(row + 1), parseInt(col)); neighbours += getCellValue(parseInt(row + 1), parseInt(col + 1)); return neighbours; } function uppdateCell(i, j) { const total = countNeighbours(i, j); if (arry[i][j] === 0 && total == 3) { return 1; } else if (total > 4 || total < 3) { return 0; } else { return arry[i][j]; } } function uppdateGridValues() { for (var i = 0; i < cellsInRow; i++) { for (var j = 0; j < cellsInCollum; j++) { let newState = uppdateCell(i, j); arryOld[i][j] = newState; } } arry = arryOld; arryOld = arry; }

Create Array创建数组

let arry = [];
let arryOld = [];

for(var i = 0; i < cellsInRow; i++){
    
    arry[i] = [];
    
    for(var j = 0; j < cellsInCollum; j++){
        
        arry[i][j] = 0;
    }
}

arryOld = arry;

I do sometimes get NaN when writing out the total variable in the uppdateCell function.在 updateCell function 中写出总变量时,我有时会得到 NaN。 I don't know why because i am suppose to return a 0 if the value passed to getCellValue from countNeighbours is outside the array size.我不知道为什么,因为如果从 countNeighbours 传递给 getCellValue 的值超出数组大小,我想返回 0。

You can't use try/catch to detect reading outside the array.您不能使用try/catch来检测数组外的读数。 Access outside the array just returns undefined , it doesn't raise an exception.数组外的访问只返回undefined ,它不会引发异常。

You can use ||您可以使用|| to return a default if the value isn't set (note that this works in this particular case because there are no other "falsey" values that are valid to return, it can't always be used like this).如果未设置该值,则返回默认值(请注意,这在这种特殊情况下有效,因为没有其他有效的“错误”值可以返回,它不能总是这样使用)。

function getCellValue(row, col) {
  return arry[row][col] || 0;
}

Another problem is that另一个问题是

arryOld = arry;

doesn't make a copy of the array, both variables refer to the same array.不会复制数组,两个变量都引用同一个数组。 Use this to make a deep copy of the array.使用它来制作数组的深层副本。

arryOld = arry.map(row => [...row]);

My guess at the issue我对这个问题的猜测

I'm reading through, and I've found something which may be causing the error: your calls of parseInt() in countNeighbors() .我正在通读,我发现了一些可能导致错误的东西:您在countNeighbors()中调用parseInt() ) 。

From the MDN JavaScript Docs on parseInt() :来自parseInt()上的 MDN JavaScript 文档

"The parseInt() function parses a string argument and returns an integer... or NaN . (if the input is invalid in a few different ways)" parseInt() function 解析字符串参数并返回 integer... 或NaN 。(如果输入在几种不同的方式中无效)”

Basically, parseInt() is expecting string input and youre giving it integer input.基本上, parseInt()期待字符串输入,你给它 integer 输入。 I'd reccomend removing the calls to parseInt() from countNeighbors() like this:我建议从countNeighbors()中删除对parseInt()的调用,如下所示:

function countNeighbours(row, col) {
  let neighbours = 0;

  neighbours += getCellValue((row - 1), (col - 1));
  neighbours += getCellValue((row - 1), (col));
  neighbours += getCellValue((row - 1), (col + 1));

  neighbours += getCellValue((row), (col - 1));
  neighbours += getCellValue((row), (col + 1));

  neighbours += getCellValue((row + 1), (col - 1));
  neighbours += getCellValue((row + 1), (col));
  neighbours += getCellValue((row + 1), (col + 1));

  return neighbours;
} 

Some other suggestions其他一些建议

While I was reading thru this code, I thought of a few suggestions on things you might be able to do it.当我阅读这段代码时,我想到了一些关于你可以做的事情的建议。 (These are totally optional tho. None of these things could be causing your code to break.) (这些完全是可选的。这些都不会导致您的代码中断。)

  1. arry as a global variable here works, but global variables make code harder to manage and understand. arry作为全局变量在这里有效,但全局变量使代码更难管理和理解。 I would make this a local variable of a main function, and pass it into all of your other functions.我会将其设为主要 function 的局部变量,并将其传递给您的所有其他函数。
  2. arry should be renamed to something more descriptive; arry应该重命名为更具描述性的名称; possibly gameboard or grid .可能是gameboardgrid
  3. countNeighbors() can be shortened and simplified using a nested for, similar to what you have in updateGridValues() . countNeighbors()可以使用嵌套的 for 来缩短和简化,类似于updateGridValues()中的内容。 Here's an example of how that could be done:这是一个如何做到这一点的例子:
function countNeighbours(row, col) {
  let neighbourTotal = 0;
  for (let y = -1; y <= 1; y++) {
    for (let x = -1; x <= 1; x++) {
      neighbourTotal += getCellValue(row + y, col + x);
    }
  }
 
  return neighbourTotal;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM