Skip to content

Instantly share code, notes, and snippets.

@xem
Forked from 140bytes/LICENSE.txt
Last active November 20, 2023 13:51
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xem/6465061 to your computer and use it in GitHub Desktop.
Save xem/6465061 to your computer and use it in GitHub Desktop.
Mini Game of Life
/* The Game of Life function */
// @param s: current state of the grid
// @param d: size of the grid (d*d)
// @param n: placeholder
// @param k: placeholder
// @param m: placeholder
// @param i: placeholder
// @param j: placeholder
function(s, d, n, k, m, i, j){
for(
n = [], // Initialize next state
m = [d + 1, d, d - 1, 1], // Initialize the upper half of the neighbours indexes
i = d * d; // For each cell
i--;
n[i] = k == 3 || s[i] && k == 2, // Set next state (live if it has 3 neighbours or lives and has 2 neighbours)
k = 0 // Reset the count of living neighbours
)
for(j in m) // for each neighbour position
k += s[i + m[j]] + s[i - m[j]] // count the living neighbours
return(n) // return the next state
}
function(f,c,d,h){for(var g=[],e=[c+1,c,c-1,1],b=c*c;b--;g[b]=3==d||f[b]&&2==d,d=0)for(h in e)d+=f[b+e[h]]+f[b-e[h]];return g}
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": "miniGameOfLife",
"description": "The smallest Game of Life engine of all time",
"keywords": [
"game",
"life"
]
}
<a href="http://xem.github.io/miniGameOfLife">Demo</a>
@atk
Copy link

atk commented Sep 16, 2013

Could that work?

g=function(f,c,g,d,e,b,h){g=[];e=[c+1,c,c-1,1];for(b in f){d=0;for(h in e)d+=f[b+e[h]]+f[b-e[h]];g[b]=3==d||f[b]&&2==d}return g}

@xem
Copy link
Author

xem commented Apr 19, 2014

Sadly, no. But I don't know why exactly.

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