Skip to content

Instantly share code, notes, and snippets.

@xem xem/

forked from 140bytes/LICENSE.txt
Last active Aug 29, 2015
What would you like to do?

Mini Mini Game of life

A game of line engine where everything that can be passed as parameter, is.

Score: 81b.

// Choose an integer d.
// Prepare a square grid of size d * d. And a JS array representing its state with booleans.
// Prepare another JS array that will be edited by this function
// @param s: current state of the grid (array)
// @param n: next state of the grid (another array)
// @param i: number of cells in the grid (i = d * d)
// @param m: neighbours offsets (array filled with: [d + 1, d, d - 1, 1, -1, - d - 1, - d, - d + 1])
// @param j: placeholder
// @param k: placeholder
function(s, n, i, m, j, k){
i--; // For each cell
n[i] = k == 3 || s[i] && k == 2, // Set next state (live if it has 3 neighbours or if it lives and has 2 neighbours)
k = 0 // Reset the count of living neighbours
for(j in m){ // loop on neighbours offsets
k += s[i + m[j]] // count the living neighbours
function(d,g,c,e,f,b){for(;c--;g[c]=3==b||d[c]&&2==b,b=0)for(f in e)b+=d[c+e[f]]}

This comment has been minimized.

Copy link

atk commented Jun 11, 2014

Could 3==b||d[c]&&2==b be replaced with b==3-d[c]?

Update: no, because if it lives, 3 neighbours will also suffice, but we can do (b|d[c])==3 instead, saving 3 chars to your current solution.


This comment has been minimized.

Copy link
Owner Author

xem commented Jun 12, 2014

Thanks atk! I'll try to use your trick in the whole new version of

BTW, as our friends @aemkei and @subzey pointed out, it's nonsense to put as many things as possible in the parameters:



This comment has been minimized.

Copy link

atk commented Jun 13, 2014

That'll only work with true/false, not with 0/1, so it might not work with your current approach.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.