Skip to content

Instantly share code, notes, and snippets.

@0xNaN
Forked from 140bytes/LICENSE.txt
Created October 22, 2012 21:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 0xNaN/3934250 to your computer and use it in GitHub Desktop.
Save 0xNaN/3934250 to your computer and use it in GitHub Desktop.
Conway's Game of Life

132 byte Conway's Game of Life

A tweet-sized Engine for the Conway's Game of Life.

// 132 byte Conway's Game of Life
function(
g, // matrix (ground of game)
x, // x of selected cell
y, // y of selected cell
w, // width of g
h // height of g
){
v=g[x][y]; // value of selected cell
s=0; // initializing "sum" variable
for( i=x-1 ; i<x+2 ; i++) // 3x3
for( k=y-1 ;k < y+2; k++) // loop
if( i>=0 && i<w && k >=0 && k<h) // check position
s+=g[i][k]; // count alive cells
return s==3 || v && s==4; // new state of cell. true = alive . false = die.
}
function(g,x,y,w,h){v=g[x][y];s=0;for(i=x-1;i<x+2;i++)for(k=y-1;k<y+2;k++)if(i>=0&&i<w&&k>=0&&k<h)s+=g[i][k];return s==3||v&&s==4;}
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": "conwayGameOfLife",
"description": "Engine for the Conway's Game of Life.",
"keywords": [
"Conway",
"Game",
"of",
"life"
]
}
<!DOCTYPE html>
<title>Foo</title>
<script>
<script>
var WIDTH=10;
var HEIGHT=10;
var sum;
var t = function(g,x,y,w,h){v=g[x][y];s=0;for(i=x-1;i<x+2;i++)for(k=y-1;k<y+2;k++)if(i>=0&&i<w&&k>=0&&k<h)s+=g[i][k];return s==3||v&&s==4;}
function main(){
space = new Array(HEIGHT);
or(i=0;i<HEIGHT;i++)
space[i]=new Array(WIDTH)
for(h=0;h<HEIGHT;h++){
for(w=0;w<WIDTH;w++){
space[h][w]=parseInt((Math.random() * 10)%2);
}
}
run(space);
}
function run(s){
print(s)
new_space = new Array(WIDTH);
for(h=0;h<HEIGHT;h++){
new_space[h] = new Array(HEIGHT);
for(w=0;w<WIDTH;w++){
new_space[h][w]=t(s,h,w,WIDTH,HEIGHT);
}
}
setTimeout("run(new_space)",100);
}
function print(matrix){
var text="<table ><tr>";
for(h=0;h<HEIGHT;h++){
for(w=0;w<WIDTH;w++){
text+=" ";
text+=(matrix[h][w] == 0)? "<td> &nbsp;</td>":"<td class='alive'>&nbsp;</td>" ;
}
text+="</tr><tr>";
}
document.getElementById("output").innerHTML = text+"</table>";
}
</script>
<style>
table {
border-collapse:collapse;
}
tr {
font-size:3px;
}
td {
border:1px solid black;
}
td.alive{
background:black;
}
</style>
<body onload="main()">
<div id="output">
<br>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment