Skip to content

Instantly share code, notes, and snippets.

@micahrj
Created October 11, 2012 05:38
Show Gist options
  • Save micahrj/3870420 to your computer and use it in GitHub Desktop.
Save micahrj/3870420 to your computer and use it in GitHub Desktop.
var width = 5;
var height = 5;
var board = [[]];
for (var y = 0; y < height; y++) {
board[y] = [];
for (var x = 0; x < width; x++) {
board[y][x] = { state: "dead", color: "gray" };
}
}
function step() {
var buffer = board.slice().map(function(row) { return row.slice(); });
for (var y = 0; y < height; y++) {
for (var x = 0; x < width; x++) {
switch (board[y][x].state) {
case "alive":
buffer[y][x] = { state: "dying", color: board[y][x].color };
break;
case "dying":
buffer[y][x] = { state: "dead", color: "gray" };
break;
case "dead":
var neighbors = [];
for (var neighborY = y - 1; neighborY <= y + 1; neighborY++) {
if (neighborY >= 0 && neighborY < board.length) {
for (var neighborX = x - 1; neighborX <= x + 1; neighborX++) {
if ( !(neighborX == x && neighborY == y) && neighborX >= 0 && neighborX < board[neighborY].length) {
if (board[neighborY][neighborX].state == "alive") {
neighbors.push(board[neighborY][neighborX]);
}
}
}
}
}
if (neighbors.length == 2) {
buffer[y][x] = { state: "alive", color: "gray" };
if (neighbors[0].color == neighbors[1].color) {
buffer[y][x].color = neighbors[0].color;
}
}
break;
}
}
}
board = buffer;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment