added timer master
added timer

file:a/index.html -> file:b/index.html
--- a/index.html
+++ b/index.html
@@ -23,6 +23,7 @@
       <div class="scores-container">
         <div class="score-container">0</div>
         <div class="best-container">0</div>
+        <div class="timer-container" id="time-container">00:00</div>
       </div>
     </div>
 

--- a/js/game_manager.js
+++ b/js/game_manager.js
@@ -11,6 +11,10 @@
   this.inputManager.on("keepPlaying", this.keepPlaying.bind(this));
 
   this.setup();
+
+  // timer
+  this.timerMinutes = 0;
+  this.timerSeconds = 10;
 }
 
 // Restart the game
@@ -18,6 +22,9 @@
   this.storageManager.clearGameState();
   this.actuator.continueGame(); // Clear the game won/lost message
   this.setup();
+
+  // game is over on given timer in seconds
+  this.timer ();
 };
 
 // Keep playing after winning (allows going over 2048)
@@ -29,6 +36,7 @@
 // Return true if the game is lost, or has won and the user hasn't kept playing
 GameManager.prototype.isGameTerminated = function () {
   if (this.over || (this.won && !this.keepPlaying)) {
+    clearInterval(this.gameTimer);
     return true;
   } else {
     return false;
@@ -37,10 +45,11 @@
 
 // Set up the game
 GameManager.prototype.setup = function () {
+  clearInterval(this.gameTimer);
   var previousState = this.storageManager.getGameState();
 
   // Reload the game from a previous game if present
-  if (previousState) {
+  if (previousState && 1==2) {
     this.grid        = new Grid(previousState.grid.size,
                                 previousState.grid.cells); // Reload grid
     this.score       = previousState.score;
@@ -57,9 +66,12 @@
     // Add the initial tiles
     this.addStartTiles();
   }
+  this.initialTime = 0;
+  this.gameTimer = 0;
 
   // Update the actuator
   this.actuate();
+  this.timer ();
 };
 
 // Set up the initial tiles to start the game with
@@ -275,3 +287,38 @@
   return first.x === second.x && first.y === second.y;
 };
 
+// Timer for game to be over in 3 minutes
+GameManager.prototype.timer = function () {
+  this.initialTime = Date.now();
+  clearInterval(this.gameTimer);
+  var t = this;
+  this.gameTimer = setInterval(function(){t.checkTime();}, 100);
+};
+
+GameManager.prototype.checkTime = function (){
+  var timeDifference = Date.now() - this.initialTime;
+  var totalSeconds = Math.floor(timeDifference/1000);
+  var minutes = Math.floor(totalSeconds/60);
+  var seconds = totalSeconds - minutes * 60;
+  this.gameOver (minutes, seconds);
+  if (seconds < 10) seconds = "0"+seconds;
+  if (minutes < 10) minutes = "0"+minutes;
+  var formatted = minutes + ':' + seconds;
+  document.getElementById('time-container').innerHTML = '' + formatted;
+};
+
+GameManager.prototype.gameOver = function (minutes, seconds) {
+  var _secondsRun = minutes * 60 + seconds;
+  var _secondsPerGame = this.timerMinutes * 60 + this.timerSeconds;
+  if ( _secondsRun >= _secondsPerGame ) {
+    clearInterval(this.gameTimer);
+    this.over = true;
+    this.actuator.actuate(this.grid, {
+      score:      this.score,
+      over:       this.over,
+      won:        this.won,
+      bestScore:  this.storageManager.getBestScore(),
+      terminated: this.isGameTerminated()
+    });
+  }
+};

--- a/style/main.css
+++ b/style/main.css
@@ -50,7 +50,7 @@
   float: right;
   text-align: right; }
 
-.score-container, .best-container {
+.score-container, .best-container, .timer-container {
   position: relative;
   display: inline-block;
   background: #bbada0;
@@ -63,7 +63,7 @@
   color: white;
   margin-top: 8px;
   text-align: center; }
-  .score-container:after, .best-container:after {
+  .score-container:after, .best-container:after, .timer-container:after {
     position: absolute;
     width: 100%;
     top: 10px;
@@ -94,6 +94,9 @@
 
 .best-container:after {
   content: "Best"; }
+
+.timer-container:after {
+  content: "Timer"; }
 
 p {
   margin-top: 0;

comments