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){ | |
for( | |
; | |
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]]} |
Thanks atk! I'll try to use your trick in the whole new version of http://xem.github.io/miniGameOfLife
BTW, as our friends @aemkei and @subzey pointed out, it's nonsense to put as many things as possible in the parameters:
https://twitter.com/MaximeEuziere/status/474633537262333952
:)
That'll only work with true/false, not with 0/1, so it might not work with your current approach.
Could
3==b||d[c]&&2==b
be replaced withb==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.