Reputation: 55
The algorithm I created (implementation of Conway's Game of Life's rules) doesn't match Conway's 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/
Pertinent code:
Checking the surrounding cells for live cells: (Line 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)
if(cell && (ne < 2 || ne > 3)) cell = 0; // Over- or under- populated?
else if(!cell && ne == 3) cell = 1; // Give life?
Upvotes: 0
Views: 264
Reputation: 356
A few items:
Upvotes: 1
Reputation: 413717
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
. However, the rest of your code seems to fill empty cells in with 0
.
Upvotes: 0