Skip to content

Instantly share code, notes, and snippets.

@coderek
Created February 13, 2014 22:22
Show Gist options
  • Save coderek/8985172 to your computer and use it in GitHub Desktop.
Save coderek/8985172 to your computer and use it in GitHub Desktop.
Game of Life JavaScript Implementation
.grid {
}
.cell {
background: black;
float: left;
box-shadow: inset 0 0 1px 0 white;
}
.cell.live {
background: seagreen;
}
div {
margin: 0;
padding: 0;
color: red;
text-align: center;
font-size: 80%;
}
<html>
<head>
<link rel="stylesheet" href="game.css"/>
</head>
<body>
<script src="game.js"></script>
</body>
</html>
var Game = {};
var log = console.log.bind(console);
var logcell = function (cell) {
var prefix = "cell["+cell.x+", "+cell.y+"]";
return console.log.bind(console, prefix);
};
var Cell = function (x, y, status, cell_width) {
this.x = x;
this.y = y;
this.status = status;
this.el = document.createElement("div");
this.el.className = "cell";
this.el.style.height = cell_width + "px";
this.el.style.width = cell_width + "px";
this.next_status = -1;
if (this.status == 1) {
this.el.className += " live";
}
}
Cell.prototype = {
set_next_status: function() {
var neighbors = this.get_neighbors();
var live_count = 0;
neighbors.forEach(function(n) {
if (n.status == 1) {live_count ++;}
});
if (this.status == 0 ) {
if (live_count == 3) {
this.next_status = 1;
}
} else if (live_count < 2 || live_count > 3) {
this.next_status = 0;
}
// this.el.textContent = this.status+ "|"+this.next_status;
return;
},
goto_next_status: function () {
if (this.status == this.next_status || this.next_status == -1) {
this.next_status = -1;
return;
}
this.status = this.next_status;
this.next_status = -1;
this.update_style();
return;
},
update_style: function () {
if (this.status == 1) {
this.el.className = "cell live";
} else {
this.el.className = "cell";
}
return;
},
get_neighbors: function () {
var coords = [
[this.x - 1, this.y -1],
[this.x - 1, this.y ],
[this.x - 1, this.y +1],
[this.x , this.y - 1],
[this.x , this.y + 1],
[this.x + 1, this.y - 1],
[this.x + 1, this.y ],
[this.x + 1, this.y + 1]
];
coords = coords.filter(function (co) {
return 0 <= co[0] && co[0] < Game.config.width && 0 <= co[1] && co[1] < Game.config.height;
});
return coords.map(function (co) {
return Game.cells[co[0] * Game.config.width + co[1]];
});
},
toString: function() {
return "[" + this.x + ", " + this.y + "]" + " : "+ this.status;
}
}
var set_up = function () {
log("set up game")
var config = Game.config = {
width: 20,
height: 20,
seed: [21,22,41,42, 24, 25, 26]
};
var cell_width = 30;
Game.cells = [];
Game.grid = document.createElement("div");
Game.grid.className = "grid";
Game.grid.style.width = config.width * cell_width + "px";
Game.grid.style.height = config.height * cell_width + "px";
for (var i=0;i<config.width;i++) {
for (var j=0;j<config.height;j++) {
var status = ~~(config.seed.indexOf(j + i * config.width) != -1);
var cell = new Cell(i, j, status, cell_width);
Game.cells.push(cell);
Game.grid.appendChild(cell.el);
}
}
document.body.appendChild(Game.grid);
};
var tick = Game.tick = function () {
for (var i=0;i<Game.config.width;i++) {
for (var j=0;j<Game.config.height;j++) {
var cell = Game.cells[i * Game.config.width + j];
cell.set_next_status();
}
}
for (var i=0;i<Game.config.width;i++) {
for (var j=0;j<Game.config.height;j++) {
var cell = Game.cells[i * Game.config.width + j];
cell.goto_next_status();
}
}
};
Game.start = function () {
log("start game")
set_up();
setInterval(tick, 100);
};
Game.start();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment