- 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();
+ function universe(blank) {
+ this.population = [];
+ this.generation = 0;
+ this.running = 0;
+ this.tickID = 0;
+
+ this.blank = blank || 0;
+
+ this.rows = 50;
+ this.columns = 50;
+
+ 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 * 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.start = function() {
+ this.tickID = setInterval(function(){tick(automaton)}, 100);
+ this.running = 1;
+ }
+
+ 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 < 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();
+ }
+ }