Watch Encyclopedia / website · watchencyclopedia.org / exp / life.js
| 1 | WE.mount('life', function (S) { |
| 2 | var C = S.C, ctx = S.ctx, cols = 0, rws = 0, g, age, gen = 0, acc = 0, cs = 10, paint = 1, G = {}; |
| 3 | var PAT = { glider: ['.O.', '..O', 'OOO'], pulsar: ['..OOO...OOO..', '.............', 'O....O.O....O', 'O....O.O....O', 'O....O.O....O', '..OOO...OOO..', '.............', '..OOO...OOO..', 'O....O.O....O', 'O....O.O....O', 'O....O.O....O', '.............', '..OOO...OOO..'], rpent: ['.OO', 'OO.', '.O.'], |
| 4 | gun: ['........................O...........', '......................O.O...........', '............OO......OO............OO', '...........O...O....OO............OO', 'OO........O.....O...OO..............', 'OO........O...O.OO....O.O...........', '..........O.....O.......O...........', '...........O...O....................', '............OO......................'] }; |
| 5 | var play = S.toggle('Run', true), spd = S.slider('Speed', 1, 40, 12, 1, function (v) { return v + ' steps a second'; }); |
| 6 | S.button('One step', function () { play.set(false); step(); }); |
| 7 | var pat = S.select('Start with', [['gun', 'Glider gun'], ['glider', 'Glider'], ['pulsar', 'Pulsar'], ['rpent', 'Five cells'], ['rand', 'Random'], ['clear', 'Empty']], 'gun', load); |
| 8 | function init() { var w = S.W, h = S.H; cs = S.small() ? 8 : 11; var c = Math.floor(w / cs), r = Math.floor(h / cs); if (c === cols && r === rws) return; cols = c; rws = r; g = new Uint8Array(cols * rws); age = new Uint8Array(cols * rws); load(pat.v); } |
| 9 | function load(v) { if (!cols) return; g.fill(0); age.fill(0); gen = 0; if (v === 'rand') { for (var i = 0; i < g.length; i++) g[i] = Math.random() < .28 ? 1 : 0; return; } if (v === 'clear') { play.set(false); return; } var P = PAT[v], ox = v === 'gun' ? 2 : Math.floor((cols - P[0].length) / 2), oy = v === 'gun' ? 2 : Math.floor((rws - P.length) / 2); P.forEach(function (row, y) { for (var x = 0; x < row.length; x++) if (row[x] === 'O' && x + ox < cols && y + oy < rws) g[(y + oy) * cols + x + ox] = 1; }); play.set(true); } |
| 10 | function step() { var n = new Uint8Array(g.length); for (var y = 0; y < rws; y++) for (var x = 0; x < cols; x++) { var c = 0; for (var dy = -1; dy <= 1; dy++) for (var dx = -1; dx <= 1; dx++) { if (!dx && !dy) continue; var xx = (x + dx + cols) % cols, yy = (y + dy + rws) % rws; c += g[yy * cols + xx]; } var i = y * cols + x, a = g[i]; n[i] = a ? (c === 2 || c === 3 ? 1 : 0) : (c === 3 ? 1 : 0); age[i] = n[i] ? Math.min(250, age[i] + 1) : 0; } g = n; gen++; } |
| 11 | function at(x, y) { var c = Math.floor(x / cs), r = Math.floor(y / cs); return c >= 0 && c < cols && r >= 0 && r < rws ? r * cols + c : -1; } |
| 12 | S.pointer({ cursor: 'crosshair', down: function (x, y) { var i = at(x, y); if (i < 0) return; paint = g[i] ? 0 : 1; g[i] = paint; age[i] = 0; }, move: function (x, y, d) { if (!d) return; var i = at(x, y); if (i >= 0) { g[i] = paint; if (!paint) age[i] = 0; } } }); |
| 13 | S.onresize = init; init(); |
| 14 | S.loop(function (dt) { |
| 15 | if (!cols) { init(); return; } |
| 16 | if (play.v && !S.ptr.down) { acc += dt * spd.v; var k = 0; while (acc >= 1 && k++ < 4) { acc--; step(); } if (acc > 4) acc = 0; } |
| 17 | S.clear(); var live = 0, ox = (S.W - cols * cs) / 2, oy = (S.H - rws * cs) / 2; |
| 18 | for (var i = 0; i < g.length; i++) if (g[i]) { live++; var a = age[i]; ctx.fillStyle = a < 2 ? C.ink : a < 8 ? C.acc : '#a57a3e'; ctx.fillRect(ox + (i % cols) * cs + .5, oy + Math.floor(i / cols) * cs + .5, cs - 1, cs - 1); } |
| 19 | S.rr(8, 8, 210, 28, 14, 'rgba(13,15,19,.8)'); S.txt('Step ' + gen + ' · ' + live + ' alive', 20, 22, C.ink, 12.5, 'left', 600); |
| 20 | S.say('Each square looks at its eight neighbors. A live square with <b>2 or 3</b> live neighbors stays alive. An empty square with <b>exactly 3</b> comes alive. Everything else dies or stays empty. That is the whole rule. <b>Draw on the grid</b> to add your own cells.'); |
| 21 | }); |
| 22 | }); |