Instantly share code, notes, and snippets.

Game of Life - 140byt.es

Game of Life - 140byt.es

An implementation of Conway's Game of Life in only 90 bytes of JavaScript.

See the demo: http://output.jsbin.com/zebuxi

Usage

``````life (
input,  // input array
target, // target array
width   // width of stage
)
``````

Author

See the 140byt.es site for a showcase of entries (built itself using 140-byte entries!), and follow @140bytes on Twitter.

 ( // Using ECMA6 arrow functions. IE, Safari & Opera probably won't work. a, // array of old state b, // array to fill with new state c, // width of stage d // placeholder )=> // No {} braces are nessessary due to single statement. a.map( // Map this function over the whole old state. ( e, // placeholder f // linear index of cell )=>{ for( d = 9, // Start our neighborhood walk at 9. e = 0; // Start sum of neighbors at 0. d--; // Walk backwards until and including 0. b[f] = // Set cell after loop execution (only the last loop matters). e == 3 | // The sum includes self so the rules change a bit, any rusult of 3 or, e == 4 & // a result of 4 and, a[f] // self is true results in an alive cell, otherwise dead. ) e += // each loop accumulate to sum ~~a[ // Index old state and return 0 if undefined. f + // Index plus, d%3 - 1 + // convert linear walk value to horizontal component plus, ~~( // round down, d/3 // walk converted to vertical component, )*c - c // and scale to width. ] } )
 (a,b,c,d)=>a.map((e,f)=>{for(d=9,e=0;d--;b[f]=e==3|e==4&a[f])e+=~~a[f+d%3-1+~~(d/3)*c-c]})
 DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE Version 2, December 2004 Copyright (C) 2015 Alexei Barnes 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": "gameOfLife", "description": "Even Smaller ECMA6 Implementation of Conway's Game of Live.", "keywords": [ "ecma6", "game", "life", "cellular", "automaton" ] }
 Game of Life
Owner Author

Alexei-B commented Aug 12, 2015

 Worked on the classic game of life and got a 92 byte result with the same functionality as previous iterations, as well as a 118 byte result with wrapping issues fixed, which can be seen here: http://output.jsbin.com/zebuxi `(a,b,c,d)=>{a.map((e,f)=>{for(d=9,e=0;d--;b[f]=e==3|e==4&a[f])e+=f%c|d%3&&(f+1)%c|(d+1)%3&&~~a[f+d%3-1+~~(d/3)*c-c]})}` I decided to use arrow functions when I noticed they were implemented on chrome and firefox, which saved quite a few bytes in combination with Array.prototype.map. I also implemented a flattened walk loop instead of a precalculated walk as most other implementations I've seen, which saved bytes even without the arrow functions. I encourage you to check out the javascript as I wrote it because I left a history of changes by copy pasting into a new line every iteration: http://jsbin.com/zebuxi/edit?js Thanks to @aemkei I've really enjoyed reading the code you've written for 140bytes and on your website, a real inspiration.