X-Git-Url: https://disinclined.org/git/?a=blobdiff_plain;f=1d.js;fp=1d.js;h=cac53ea2afeb8a0544019d86f002bdf40a75e14f;hb=62b73ae19321e18709f44efd9d290ef0722b04d8;hp=0000000000000000000000000000000000000000;hpb=14a6e0b1a7490eacec21e3726bb8e6a243da1e91;p=cellular-automaton.git diff --git a/1d.js b/1d.js new file mode 100644 index 0000000..cac53ea --- /dev/null +++ b/1d.js @@ -0,0 +1,72 @@ +$(document).ready(function(){ + + var cells = 20; + var generations = 10; + + var state = { + '000' : '0', + '001' : '0', + '010' : '0', + '011' : '1', + '100' : '0', + '101' : '1', + '110' : '1', + '111' : '0', + } + + var canvas = document.getElementById('canvas') + var c = canvas.getContext('2d'); + + var cellWidth = Math.floor(canvas.width / cells); + var cellHeight = Math.floor(canvas.height / generations); + var generation = 0; + + function cell(x, y, h, l) { + this.x = x; + this.y = y; + this.h = h; + this.l = l; + this.state = 0; + this.kill = function() { + c.fillStyle = "rgb(200,0,0)"; + c.fillRect(this.x, this.y, this.h, this.l); + this.state = 0; + } + this.revive = function() { + c.fillStyle = "rgb(0,0,0)"; + c.fillRect(this.x, this.y, this.h, this.l); + this.state = 1; + } + } + + function populate_world() { + var world = []; + var x = 0; + var y = 0; + for (var i = 0; i < cells; i++) { + world.push(new cell(x, y, cellWidth, cellHeight)); + x += cellWidth; + Math.random() > .5 ? world[i].revive() : world[i].kill(); + } + return world; + } + + function generate() { + generation += 1; + var x = 0; + var y = generation * cellHeight; + var s = '0' + world.map(function(c) { return c.state }).join('') + '0'; + world = []; + for (var i = 0; i < cells; i++) { + world.push(new cell(x, y, cellWidth, cellHeight)); + x += cellWidth; + state[s.substr(i, 3)] == 1 ? world[i].revive() : world[i].kill(); + } + //if (generation == generations) { clearInterval(tick_id) } + } + + var world = populate_world(); + while (generations) { generate(); generations-- } + //var tick_id = setInterval(generate, 100); + +});