# HG changeset patch # User Eugen Sawin # Date 1325773961 -3600 # Node ID 72b5c635d6df6ef78e449cb39e1885efbcd69462 # Parent 1e4ba37124e376374926fc04e49967c635c28766 Fixed game of life. diff -r 1e4ba37124e3 -r 72b5c635d6df factory/v2012/script.js --- a/factory/v2012/script.js Thu Jan 05 03:22:15 2012 +0100 +++ b/factory/v2012/script.js Thu Jan 05 15:32:41 2012 +0100 @@ -13,7 +13,7 @@ }, function() { $(this).css("cursor", "auto"); }); - $("#sim").mousemove(function(event) { simulation.mouse_moved(mouse_pos(event, "#sim"));}); + $("#sim").mousemove(function(event) { simulation.mouse_moved(mouse_pos(event, "#sim")); }); var onEachFrame; if (window.webkitRequestAnimationFrame) { onEachFrame = function(cb) { @@ -583,7 +583,7 @@ } Simulation.prototype.draw = function() { var now = $.now(); - while (this.redraw.length) { + if (this.redraw.length) { var cell = this.redraw.pop(); this.context.fillStyle = cell.value == 1 ? green.str() : background.str(); this.context.fillRect(cell.canvas_x, cell.canvas_y, @@ -604,25 +604,24 @@ this.redraw.push(cell); } } - if (this.last_update + 300 > now) { + if (this.last_update + 1000 > now) { return; } var changed = new Array(); - if (this.cell_queue.length) { + var next_cell_queue = new Array(); + while (this.cell_queue.length) { var cell = this.cell_queue.pop(); - if (changed[cell.hash()]) { - console.log(cell) - console.log(changed[cell.hash()]); - return; + if (changed[cell.hash()]) { + continue; } var d = cell.density(); if (d == 3 && cell.value == 0) { - changed[cell.hash()] = cell; - this.cell_queue.concat(cell.neighbours()); + changed[cell.hash()] = cell; + next_cell_queue = next_cell_queue.concat(cell.neighbours()); } else if (cell.value == 1) { if (d < 2 || d > 3) { changed[cell.hash()] = cell; - this.cell_queue.concat(cell.neighbours()); + next_cell_queue = next_cell_queue.concat(cell.neighbours()); } } } @@ -631,6 +630,7 @@ cell.set_value(1 - cell.value); this.redraw.push(cell); } + this.cell_queue = this.cell_queue.concat(next_cell_queue); this.last_update = now; } Simulation.prototype.mouse_moved = function(pos) { diff -r 1e4ba37124e3 -r 72b5c635d6df script.js --- a/script.js Thu Jan 05 03:22:15 2012 +0100 +++ b/script.js Thu Jan 05 15:32:41 2012 +0100 @@ -13,7 +13,7 @@ }, function() { $(this).css("cursor", "auto"); }); - $("#sim").mousemove(function(event) { simulation.mouse_moved(mouse_pos(event, "#sim"));}); + $("#sim").mousemove(function(event) { simulation.mouse_moved(mouse_pos(event, "#sim")); }); var onEachFrame; if (window.webkitRequestAnimationFrame) { onEachFrame = function(cb) { @@ -583,7 +583,7 @@ } Simulation.prototype.draw = function() { var now = $.now(); - while (this.redraw.length) { + if (this.redraw.length) { var cell = this.redraw.pop(); this.context.fillStyle = cell.value == 1 ? green.str() : background.str(); this.context.fillRect(cell.canvas_x, cell.canvas_y, @@ -604,25 +604,24 @@ this.redraw.push(cell); } } - if (this.last_update + 300 > now) { + if (this.last_update + 1000 > now) { return; } var changed = new Array(); - if (this.cell_queue.length) { + var next_cell_queue = new Array(); + while (this.cell_queue.length) { var cell = this.cell_queue.pop(); - if (changed[cell.hash()]) { - console.log(cell) - console.log(changed[cell.hash()]); - return; + if (changed[cell.hash()]) { + continue; } var d = cell.density(); if (d == 3 && cell.value == 0) { - changed[cell.hash()] = cell; - this.cell_queue.concat(cell.neighbours()); + changed[cell.hash()] = cell; + next_cell_queue = next_cell_queue.concat(cell.neighbours()); } else if (cell.value == 1) { if (d < 2 || d > 3) { changed[cell.hash()] = cell; - this.cell_queue.concat(cell.neighbours()); + next_cell_queue = next_cell_queue.concat(cell.neighbours()); } } } @@ -631,6 +630,7 @@ cell.set_value(1 - cell.value); this.redraw.push(cell); } + this.cell_queue = this.cell_queue.concat(next_cell_queue); this.last_update = now; } Simulation.prototype.mouse_moved = function(pos) {