Skip to content

Instantly share code, notes, and snippets.

@aemkei
Forked from 140bytes/LICENSE.txt
Created August 9, 2011 17:30
Show Gist options
  • 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>
@aemkei
Copy link
Author

aemkei commented Aug 15, 2011

I know: The wrapping is not perfect - but flattening the two dimensional array did the trick to get it into 140 bytes. So, feel free to tweak the code!

@subzey
Copy link

subzey commented Aug 15, 2011

Let's get some extra bytes to deal with it! :)

We may rearrange the loop

for(d=c=[];d++<b*b;)e=/*...*/,c[d]=/*...*/;

into

for(c=[d=b*b];d--;c[d]=/*...*/)e=/*...*/;

@aemkei
Copy link
Author

aemkei commented Aug 15, 2011

Cool! Now we have 5 bytes left to fix the wrapping.
How about using the modulo % operator or shifting the string to check the bounds?

@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