1. Pedro Ferreira
  2. game_of_life

Commits

Pedro Ferreira  committed b375329

- add a way to reset the game
- fix, not clearing the shape when switching between alive/dead

  • Participants
  • Parent commits 57d4b54
  • Branches default

Comments (0)

Files changed (3)

File index.html

View file
     <canvas id="canvas"></canvas>
 
     <div id="start">Start</div>
+    <div id="reset">Reset</div>
 
 </body>
 </html>

File scripts/main.js

View file
 G.CANVAS = document.querySelector( '#canvas' );
 G.STAGE = new createjs.Stage( G.CANVAS );
 
-G.CANVAS.width = 800;
-G.CANVAS.height = 600;
+G.CANVAS.width = 600;
+G.CANVAS.height = 450;
 
 G.INIT_PHASE = true;
 
         }
     };
 
+var reset = document.querySelector( '#reset' );
+
+reset.onclick = function()
+    {
+    clearGame();
+    };
+
+
 
 createjs.Ticker.on( 'tick', tick );
 };
 }
 
 
+function clearGame()
+{
+G.INIT_PHASE = true;
+
+for (var column = 0 ; column < G.COLUMNS ; column++)
+    {
+    for (var line = 0 ; line < G.LINES ; line++)
+        {
+        var square = G.MAP[ column ][ line ];
+
+        square.setAlive( false );
+        square.addClickEvent();
+        }
+    }
+}
+
+
 function howManyAliveNeighbors( square )
 {
 var howMany = 0;

File scripts/square.js

View file
 
 function Square( column, line )
 {
-var _this = this;
 var shape = new createjs.Shape();
 
-this.click_f = shape.on( 'click', function() { _this.setAlive( !_this.isAlive ) }, this );
-
 this.shape = shape;
 this.column = column;
 this.line = line;
 
+this.addClickEvent();
 this.setAlive( false );
 this.setPosition( column, line );
 
 this.shape.off( 'click', this.click_f );
 };
 
+Square.prototype.addClickEvent = function()
+{
+var _this = this;
+
+this.click_f = this.shape.on( 'click', function() { _this.setAlive( !_this.isAlive ) }, this );
+};
+
 
 Square.prototype.setAlive = function( yes )
 {
 var g = this.shape.graphics;
 
+g.clear();
+
 if ( yes )
     {
     g.beginFill( 'black' );