简体   繁体   English

康威的人生游戏-算法不正确?

[英]Conway's Game Of Life - Incorrect Algorithm?

The algorithm I created (implementation of Conway's Game of Life's rules) doesn't match Conway's GoL. 我创建的算法(实施Conway的人生游戏规则)与Conway的GoL不匹配。 I've tried just about everything I can do, but it just doesn't match. 我已经尝试了几乎所有可以做的事情,但是并没有完成。

Additionally, if anyone knows how to make it into an infinite plane or perhaps wrap on itself, I'd be interested to see it implemented with my code! 此外,如果有人知道如何将其制作成无限大的平面,或者也许将其包裹起来,那么我很想看到它是用我的代码实现的!

Running JSFiddle: http://jsfiddle.net/jGkKF/2/ 运行JSFiddle: http : //jsfiddle.net/jGkKF/2/

Pertinent code: 相关代码:

Checking the surrounding cells for live cells: (Line 28) 检查周围的活细胞:(第28行)

var   x2 = x+1,   x3 = x-1,   y2 = y+1,   y3 = y-1;     // Math
if(tC[x][y3] !== undefined && tC[x][y3]) ne++;          // T
if(tC[x][y2] !== undefined && tC[x][y2]) ne++;          // TR
if(tC[x2] !== undefined) {
    if(tC[x2][y]) ne++;                                 // R
    if(tC[x2][y3] !== undefined && tC[x2][y3]) ne++;    // BR
    if(tC[x2][y2] !== undefined && tC[x2][y2]) ne++;    // B
}
if(tC[x3] !== undefined) {
    if(tC[x3][y]) ne++;                                 // BL
    if(tC[x3][y3] !== undefined && tC[x3][y3]) ne++;    // L
    if(tC[x3][y2] !== undefined && tC[x3][y2]) ne++;    // TL
}

And the algorithm: (Line 50) 和算法:(第50行)

if(cell && (ne < 2 || ne > 3)) cell = 0; // Over- or under- populated?
else if(!cell && ne == 3) cell = 1;      // Give life?

A few items: 一些项目:

  1. The game rules say to check all 8 surrounding squares. 游戏规则要求检查所有8个周围的正方形。 Based on your code, you seem to be only checking 6 of them. 根据您的代码,您似乎只检查了其中的6个。 Edit: OP is checking all 8. I still recommend point 2, it allows consistent handling of each cell being checked. 编辑:OP正在检查所有8。我仍然建议第二点,它允许对每个被检查的单元格进行一致的处理。
  2. The easiest way to hand it is to set up a list of cells that need to be check, then run a loop over that list to do the counting. 处理它的最简单方法是建立一个需要检查的单元格列表,然后在该列表上运行循环以进行计数。
  3. To set up a wrapping list, have bounds checking code when setting up the list of cells, eg 要设置包装列表,请在设置单元格列表时使用边界检查代码,例如
    • if cellIndex < 0 then cellIndex == maxIndex 如果cellIndex <0,则cellIndex == maxIndex
    • if cellIndex >= maxIndex then cellIndex = 0 如果cellIndex> = maxIndex,则cellIndex = 0
  4. You need to be wary of a "scan line" effect that can occur if you update the values representing "this" generation while trying to calculate the "next" generation. 您需要警惕如果在尝试计算“下一个”世代时更新代表“本”世代的值时可能发生的“扫描线”效应。 The two need to be stored independently. 两者需要独立存储。 Note if you are using arrays of ints, this can simply mean storing "this" generation as 0 and 1, and the "next" generation of live cells can be indicated by adding 8 etc. After you process the current generation, change everything >= 8 to 1, and any cell < 8 becomes 0. 请注意,如果您使用的是整数数组,这可能只是意味着将“这一代”存储为0和1,而活细胞的“下一代”可以通过加8等来表示。处理当前一代后,请更改所有内容> = 8到1,且任何<8的像元都变为0。

The statement 该声明

tC = cells;

does not make a copy of the cell array. 不会复制单元格数组。 It merely creates a second reference to the exact same array. 它仅创建对完全相同的数组的第二个引用。 Thus, later on, when you do this: 因此,稍后,当您执行此操作时:

cells[x][y] = cell; // Setting the cell

that's modifying the same array that the loop is looking at. 修改了循环正在查看的相同数组。

Also, in your loop to check neighbors, you've coded an exact comparison to undefined . 另外,在检查邻居的循环中,您已将精确的比较编码为undefined However, the rest of your code seems to fill empty cells in with 0 . 但是,您其余的代码似乎用0填充了空单元格。

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

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