Skip to content

Instantly share code, notes, and snippets.

@hackingbeauty
Last active December 21, 2015 00:59
Show Gist options
  • Save hackingbeauty/6224558 to your computer and use it in GitHub Desktop.
Save hackingbeauty/6224558 to your computer and use it in GitHub Desktop.
Start of Game of Life challenge
var prompt = require('prompt');
GameOfLife = {
input: {},
grid: [],
init: function(){
this.prompt();
},
// Prompt for input
prompt: function(){
var self = this;
prompt.start();
prompt.get(['iterations', 'columns', 'rows'], function (err, result) {
if (err) {
return function(){
console.log(err);
return 1;
}
}
self.input = result;
self.setUpWorld();
self.initSimulations();
});
},
// Set up the world
setUpWorld: function(){
var self = this,
rows = self.input["rows"],
columns = self.input["columns"],
columnArr = [];
for(var i = 0; i < rows; i++){
columnArr = []; //reinitialize
for(var j = 0; j < columns; j++){
columnArr.push(self.populateCell());
}
self.grid.push(columnArr);
}
},
printGrid: function(){
var self=this;
for(var i = 0; i<self.grid.length; i++){
console.log(self.grid[i]);
}
console.log("===========");
},
// Initialize the simulations
initSimulations: function(){
var self = this;
self.printGrid();
for(var i = 0; i<self.input["iterations"]; i++){
self.gameOfLifeAlgo();
}
self.printGrid();
},
// Game of Life algorithm goes here!
gameOfLifeAlgo: function(){
var self = this,
row,
cell,
obj = {};
for(var i=0; i<self.grid.length; i++){
row = self.grid[i];
obj["prevArr"] = self.grid[i-1];
obj["nextArr"] = self.grid[i+1];
for(var j = 0; j< row.length; j++){
cell = row[j];
obj["prevItem"] = row[j-1];
obj["nextIem"] = row[j+1];
obj["cellPosition"] = j;
console.log("the cell is: ", cell);
console.log("the cell is now: ", self.deadOrAlive(cell,obj));
self.deadOrAlive(cell,obj)
}
}
},
// Returns either 0 to denote "dead" or 1 to denote "alive"
deadOrAlive: function(cell,obj){
var self = this,
grid = self.grid,
liveCount = 0;
// console.log("========");
// console.log("the grid is: ", this.grid);
// console.log("the cell is ", cell);
// console.log("the object is ", obj);
if(obj["prevItem"] === 1){ liveCount++; }
if(obj["nextItem"] === 1) {livecount++}
if(obj["prevArr"] !== undefined){
if(obj["prevArr"][obj["cellPosition"]-1] === 1) {liveCount++}
if(obj["prevArr"][obj["cellPosition"]] === 1) {liveCount++}
if(obj["prevArr"][obj["cellPosition"]+1] === 1) {liveCount++}
}
if(obj["nextArr"] !== undefined){
if(obj["nextArr"][obj["cellPosition"]-1] === 1) {liveCount++}
if(obj["nextArr"][obj["cellPosition"]] === 1) {liveCount++}
if(obj["nextArr"][obj["cellPosition"]+1] === 1) {liveCount++}
}
if(cell === 0){ // If cell is dead
// if it has 3 adjaced live squares, it comes alive
if(liveCount === 3){
return 1;
} else {
return 0;
}
}
if(cell === 1){ // If cell is alive
// A live square "dies" if it has less than two live neighbors, or more than three live neighbors
if(liveCount < 2 || liveCount > 3){
return 0;
} else {
return 1;
}
}
},
populateCell: function(){
return Math.floor(Math.random() * (1 - 0 + 1)) + 0;
}
}
GameOfLife.init();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment