{{ message }}

Instantly share code, notes, and snippets.

Last active Feb 1, 2018
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 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" ] }
 Demo

### 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 commented Apr 19, 2014

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