created populate function to seed from serialize
[cellular-automaton.git] / 2d.js
diff --git a/2d.js b/2d.js
index 46ccc75..2e731c9 100644 (file)
--- a/2d.js
+++ b/2d.js
@@ -1,13 +1,8 @@
 $(document).ready(function(){
     
-    var canvas = document.getElementById('2d-automaton')
+    var canvas = $('#2d-automaton')[0];
     var c = canvas.getContext('2d');
 
-    var rows = 50;
-    var columns = 50;
-
-    var cellWidth = canvas.width / rows;
-    var cellHeight = canvas.height / columns;
 
     function cell(x, y, h, l) {
         this.x = x;
@@ -25,60 +20,82 @@ $(document).ready(function(){
             c.fillRect(this.x, this.y, this.h, this.l);
             this.state = 1;
         }
-        this.redraw = function() {
-            c.fillStyle = this.state == 1 ? "rgb(0,0,0)" : "rgb(255,255,255)";
-            c.fillRect(this.x, this.y, this.h, this.l);
-        }
-        this.highlight = function() {
-            c.fillStyle = "rgb(0,0,100)";
-            c.fillRect(this.x, this.y, this.h, this.l);
+        this.toggle = function() {
+            this.state ? this.kill() : this.revive();
         }
     }
 
-    function universe() {
+    function universe(blank) {
         this.population = [];
         this.generation = 0;
+        this.running = 0;
+        this.tickID = 0;
+
+        this.blank = blank || 0;
+        
+        this.rows = 50;
+        this.columns = 50;
 
-        for (var i = 0; i < rows; i++) {
+        this.cellWidth = canvas.width / this.rows;
+        this.cellHeight = canvas.height / this.columns;
+
+        for (var i = 0; i < this.rows; i++) {
             var world = [];
             var x = 0;
-            var y = i * cellHeight;
-            for (var ii = 0; ii < columns; ii++) {
-                world.push(new cell(x, y, cellWidth, cellHeight));
-                x += cellWidth;
-                Math.random() > .5 ? world[ii].revive() : world[ii].kill();
+            var y = i * this.cellHeight;
+            for (var ii = 0; ii < this.columns; ii++) {
+                world.push(new cell(x, y, this.cellWidth, this.cellHeight));
+                x += this.cellWidth;
+                if (this.blank) { world[ii].kill(); }
+                else { Math.random() > .5 ? world[ii].revive() : world[ii].kill(); }
             }
             this.population.push(world);
         }
 
-        this.print = function() {
-            for (var i = 0; i < rows; i++) {
-                var s = '';
-                for (var ii = 0; ii < columns; ii++) {
-                    s += this.population[i][ii];
-                }
-            }
+        this.start = function() {
+            this.tickID = setInterval(function(){tick(automaton)}, 100);
+            this.running = 1;
         }
 
-        this.highlight = function() {
-            for (var i = 0; i < rows; i++) {
-                for (var ii = 0; ii < columns; ii++) {
-                    if (this.population[i][ii].state == 1) {
-                        this.population[i][ii].highlight();
-                    }
-                }
-            }
+        this.stop = function() {
+            clearInterval(this.tickID);
+            this.running = 0;
+        }
+
+        this.toggle = function() {
+            this.running ? this.stop() : this.start();
         }
 
         this.redraw = function() {
-            for (var i = 0; i < rows; i++) {
-                for (var ii = 0; ii < columns; ii++) {
+            for (var i = 0; i < this.rows; i++) {
+                for (var ii = 0; ii < this.columns; ii++) {
                     if (this.population[i][ii].state == 1) {
                         this.population[i][ii].redraw();
                     }
                 }
             }
         }
+
+        this.serialize = function() {
+            var serial = [];
+            for (var i = 0; i < this.rows; i++) {
+                var row = [];
+                for (var ii = 0; ii < this.columns; ii++) {
+                    row[ii] = this.population[i][ii].state;
+                }
+                serial.push(row);
+            }
+            return serial;
+        }
+
+        this.populate = function(seed) {
+            for (var i = 0; i < this.rows; i++) {
+                for (var ii = 0; ii < this.columns; ii++) {
+                    seed[i][ii] ? this.population[i][ii].revive()
+                                : this.population[i][ii].kill();
+                }
+            }
+        }
     }
 
     function tick(automaton) {
@@ -121,33 +138,41 @@ $(document).ready(function(){
     }
 
     var automaton = new universe;
-    var seed = $.extend(true, {}, automaton);
-    var tickID = 0;
 
     $('#controls #start-automaton').click(function(e){
-        tickID = setInterval(function(){tick(automaton)}, 100);
+        automaton.start();
         $('#controls #start-automaton').hide();
         $('#controls #stop-automaton').show();
-        $('#controls #highlight-seed').show();
     });
 
     $('#controls #stop-automaton').click(function(e){
-        clearInterval(tickID);
+        automaton.stop();
         $('#controls #stop-automaton').hide();
         $('#controls #start-automaton').show();
     });
 
-    $('#controls #reset-automaton').click(function(e){
-        clearInterval(tickID);
+    $('#controls #reseed-automaton').click(function(e){
+        automaton.stop();
         $('#controls #stop-automaton').hide();
         $('#controls #start-automaton').show();
-        automaton = $.extend(true, {}, seed);
-        $('#generation')[0].innerHTML = automaton.generation;
-        automaton.redraw();
+        automaton = new universe();
     });
 
-    $('#controls #highlight-seed').click(function(e){
-        seed.highlight();
+    $('#controls #custom-seed-automaton').click(function(e){
+        automaton.stop();
+        $('#controls #stop-automaton').hide();
+        $('#controls #start-automaton').show();
+        automaton = new universe(1);
+    });
+
+    $('#2d-automaton').click(function(e) {
+        if (!automaton.running) {
+            var x = e.pageX - $('#2d-automaton').offset().left;
+            var y = e.pageY - $('#2d-automaton').offset().top;
+            var row = Math.floor(y / automaton.cellHeight);
+            var column = Math.floor(x / automaton.cellWidth);
+            automaton.population[row][column].toggle();
+        }
     });
 
 });