Game of Life
by cillay
JavaScript
var Cell = function(left, top) {
var getNeighbors = function() {
var neighbors = [];
var center = this.position;
for (var x = center[0] - 1; x <= center[0] + 1; x++) {
for (var y = center[1] - 1; y <= center[1] + 1; y++) {
if (Number(x) == Number(center[0]) && Number(y) == Number(center[1])) {
continue;
}
neighbors.push(new Cell(x,y));
}
}
return neighbors;
}
return {
position: [left, top],
getNeighbors: getNeighbors
};
};
var Universe = function(cells) {
var cellComparer = function (a, b) {
return a.position[0] == b.position[1] && a.position[1] == b.position[1];
};
var isAlive = function(cell) {
return _(this.cells).filter(cell).length;
};
var numberOfLivingNeighbors = function(cell) { };
var tick = function() {
var result = [];
_(cells).each(function (cell) {
var neighbors = cell.getNeighbors();
_(neighbors).each(function (neighbor) {
if (neighbor.shouldLive()) {
result.push(neighbor);
}
});
});
return new Universe(result);
};
return {
cells: cells
}
};
var cell = new Cell(1,1);
var neighbors = cell.getNeighbors();
console.debug(neighbors);
_(cell).isEqual(new Cell(1,1)) || console.error("Equal cells aren't equal!");
neighbors.length == 8 || console.error("FAIL!");