public
Last active — forked from 140bytes/LICENSE.txt

Game of Life - 140byt.es

  • Download Gist
LICENSE.txt
1 2 3 4 5 6 7 8 9 10 11 12 13
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.
README.md
Markdown

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.

annotated.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
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;
}
package.json
JSON
1 2 3 4 5 6 7 8 9 10 11 12 13
{
"name": "gameOfLife",
 
"description": "Implementation of Conway's Game of Live.",
 
"keywords": [
"conway",
"game",
"life",
"cellular",
"automaton"
]
}
test.html
HTML
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
<!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>

Glider crashes if

size = 10;
/* <...> */
array[15] = 1;
array[26] = 1;
array[34] = 1;
array[35] = 1;
array[36] = 1;

Try it with a larger stage:

size = 50;
array[5] = array[56] = array[104] = array[105] = array[106] = 1;

I suppose, there is small glitch with wrapping. input[i-1] gets the rightmost cell in previous row when tested cell is leftmost one.

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!

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=/*...*/;

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?

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.

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!

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.