Skip to content

Instantly share code, notes, and snippets.

@flomincucci
Forked from aemkei/LICENSE.txt
Created February 13, 2012 16:49
Show Gist options
  • Save flomincucci/1818113 to your computer and use it in GitHub Desktop.
Save flomincucci/1818113 to your computer and use it in GitHub Desktop.
Game of Life - 140byt.es

Game of Life - 140byt.es

An implementation of Conway's Game of Life in less than 140 bytes of JavaScript.

See the demo: http://jsfiddle.net/aemkei/wdRcc/show/

Usage

life (
  input, // input array
  size // size (width and height) of stage
)

// returns the modificated input string

Author

Created by Martin Kleppe (@aemkei) at Ubilabs.

For more information

See the 140byt.es site for a showcase of entries (built itself using 140-byte entries!), and follow @140bytes on Twitter.

life = function(
input, // input array
size, // square stage size (width and height)
output, i, neighbours // placeholders
){
// cycle through cells
for (
output = [i = size*size];
i--;
output[i] =
// alive if it has 3 neighbours
neighbours == 3 ||
// stay alive if cell has 2 neighbours
(input[i] && neighbours == 2)
) {
neighbours =
// count neighbours
input[i-size-1] + input[i-size] + input[i-size+1] +
input[i -1] + input[i +1] +
input[i+size-1] + input[i+size] + input[i+size+1];
}
return output;
}
function(a,b,c,d,e){for(c=[d=b*b];d--;c[d]=e==3||a[d]&&e==2)e=a[d-b-1]+a[d-b]+a[d-b+1]+a[d-1]+a[d+1]+a[d+b-1]+a[d+b]+a[d+b+1];return c}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 YOUR_NAME_HERE <YOUR_URL_HERE>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
{
"name": "gameOfLife",
"description": "Implementation of Conway's Game of Live.",
"keywords": [
"conway",
"game",
"life",
"cellular",
"automaton"
]
}
<!DOCTYPE html>
<head>
<title>Game of Life</title>
<style type="text/css" media="screen">
#stage {
line-height:12px;
}
#stage div{
display: block;
float: left;
width: 10px;
height: 10px;
margin: 1px 1px 0 0;
background-color: #EEE;
}
#stage div.alive {
background-color: #000;
}
</style>
</head>
<body>
<div id="stage"></div>
<a href="https://gist.github.com/1134658">Source</a>
<script src="annotated.js" type="text/javascript" charset="utf-8"></script>
<script>
var array = [],
elems = {},
elem,
stage = document.getElementById("stage"),
size = 50,
x, y;
// create DOM elements
for (x = 0; x < size; x++){
for (y = 0; y < size; y++){
elem = document.createElement("div");
stage.appendChild(elem);
elems[x + "," + y] = elem;
array.push(Math.random() < 0.5);
}
stage.appendChild(document.createElement("br"));
}
// render stage
function play(){
var index = 0, color;
array = life(array, size);
for (x = 0; x < size; x++){
for (y = 0; y < size; y++){
elems[x + "," + y].className = array[index++] ? "alive" : "";
}
}
setTimeout(play, 10);
}
play();
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment