[英]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: 一些项目:
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.