Skip to content

Instantly share code, notes, and snippets.

@aemkei
Forked from 140bytes/LICENSE.txt
Created August 9, 2011 17:30
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save aemkei/1134658 to your computer and use it in GitHub Desktop.
Save aemkei/1134658 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>
@maettig
Copy link

maettig commented Feb 9, 2012

I came here because of this. With a simple change this allows non-square stages. a is the input array, b is the width of the stage.

function(a,b,c,d,e){for(c=[d=a.length];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}

I also thought about wrapping but I don't think there is an easy solution. Would need a lot of % and ||0.

@xem
Copy link

xem commented Sep 9, 2013

I just made a similar 140bytes project, because I didn't know this one existed.
But I managed to make it 7 bytes smaller!
https://gist.github.com/xem/6465061.
I'll try to make a correct wrapping with the remaining space.
Cheers!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment