Skip to content

Instantly share code, notes, and snippets.

@rozag
Last active August 29, 2015 14:18
Show Gist options
  • Save rozag/9fc6f977a01a81fe55bb to your computer and use it in GitHub Desktop.
Save rozag/9fc6f977a01a81fe55bb to your computer and use it in GitHub Desktop.
Conway's Game of Life
<!doctype html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>Game of Life</title>
</head>
<canvas></canvas>
<script src="main.js"></script>
</html>
"use strict";
var canvas = document.querySelector("canvas");
canvas.width = 1024; canvas.height = 512;
var context = canvas.getContext("2d");
function clearScreen() {
context.fillStyle = "white";
context.fillRect(0, 0, canvas.width, canvas.height);
}
var SCALE = 8;
var WORLD_HEIGHT = canvas.height / SCALE, WORLD_WIDTH = canvas.width / SCALE;
var world = [], buffer = [];
function initializeWorld() {
for (var y = 0; y < WORLD_HEIGHT; y++) {
var worldRow = [];
for (var x = 0; x < WORLD_WIDTH; x++) {
worldRow.push(false);
}
world.push(worldRow);
var bufferRow = [];
for (x = 0; x < WORLD_WIDTH; x++) {
bufferRow.push(false);
}
buffer.push(bufferRow);
}
}
function saveWorld() {
world.forEach(function (row, y) {
row.forEach(function (elt, x) {
buffer[y][x] = elt;
})
})
}
function clearWorld() {
world.forEach(function (row, y) {
row.forEach(function (elt, x) {
world[y][x] = false;
})
})
}
function getRandomArbitrary(min, max) { // [min; max]
return Math.round(Math.random() * (max - min) + min);
}
function generatePopulation() {
world.forEach(function (row, y) {
row.forEach(function (elt, x) {
var randomNumber = getRandomArbitrary(0, 3);
world[y][x] = randomNumber == 0;
})
})
}
function drawWorld() {
context.fillStyle = "blue";
world.forEach(function (row, y) {
row.forEach(function (elt, x) {
if (elt == true)
context.fillRect(x * SCALE, y * SCALE, SCALE, SCALE);
})
})
}
function countLiveNeighbours(y, x) {
var count = 0;
if (y - 1 >= 0 && x - 1 >= 0 && buffer[y - 1][x - 1]) count++;
if (y - 1 >= 0 && buffer[y - 1][x]) count++;
if (y - 1 >= 0 && x + 1 < WORLD_WIDTH && buffer[y - 1][x + 1]) count++;
if (x + 1 < WORLD_WIDTH && buffer[y][x + 1]) count++;
if (y + 1 < WORLD_HEIGHT && x + 1 < WORLD_WIDTH && buffer[y + 1][x + 1]) count++;
if (y + 1 < WORLD_HEIGHT && buffer[y + 1][x]) count++;
if (y + 1 < WORLD_HEIGHT && x - 1 >= 0 && buffer[y + 1][x - 1]) count++;
if (x - 1 >= 0 && buffer[y][x - 1]) count++;
return count;
}
function makeLife() {
world.forEach(function (row, y) {
row.forEach(function (elt, x) {
var liveNeighbours = countLiveNeighbours(y, x);
if ((liveNeighbours < 2 || liveNeighbours > 3) && buffer[y][x]) {
world[y][x] = false;
} else if ((liveNeighbours == 2 || liveNeighbours == 3) && buffer[y][x]) {
world[y][x] = true;
} else if (liveNeighbours == 3 && !buffer[y][x]) {
world[y][x] = true;
}
})
})
}
function makeStep() {
saveWorld();
clearWorld();
makeLife();
clearScreen();
drawWorld();
setTimeout(makeStep, 100);
}
initializeWorld();
generatePopulation();
makeStep();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment