var Life = new Object();
var Grid = new Object();
/**
*
* Why is half of the stuff attached to a Life object and the
* other half to a Grid object?: No good reason. Just to
* indicate which stuff is related to life cycle and which
* to grid operations.
*
* Controls below @line:302
*
*/
///// GRID ///////////////////////////////////////////////////
Grid.init = function (m, n) {
var i, j, column, array = [];
for (i = 0; i < m; i++) {
column = [];
for (j = 0; j < n; j++) {
column[j] = Life.DEAD;
}
array[i] = column;
}
return array;
};
Grid.drawOnCanvas = function () {
var i, j, column;
for (i = 0; i < Life.grid.length; i++) {
column = Life.grid[i];
for (j = 0; j < column.length; j++) {
if (column[j] == Life.ALIVE) {
Life.context.fillStyle = Life.COLOR_ALIVE;
} else {
Life.context.fillStyle = Life.COLOR_DEAD;
}
Life.context.fillRect(Life.CELL_GAP + i * (Life.CELL_SIZE + 2 * Life.CELL_GAP), Life.CELL_GAP + j * (Life.CELL_SIZE + 2 * Life.CELL_GAP), Life.CELL_SIZE, Life.CELL_SIZE);
}
}
};
Grid.clone = function (from, to) {
for (var i = 0; i < from.length; i++) {
to[i] = from[i].slice(0); //cloning the array...
}
};
Grid.shiftDown = function () {
var i, j, column, changes = 0;
for (i = 0; i < Life.grid.length; i++) {
column = Life.grid[i];
j = Life.pile[i];
while (Life.grid[i][j] == Life.ALIVE) {
j--;
}
Life.pile[i] = j;
j--; //skip the first empty cell
while (j >= 0) {
if (Life.grid[i][j] == Life.ALIVE) {
Life.grid[i][j] = Life.DEAD;
Life.grid[i][j + 1] = Life.ALIVE;
changes++;
}
j--;
}
}
if (changes === 0) {
Life.state = Life.IDLE;
clearInterval(Life.interval);
...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.