Speed measure.
authorOleksandr Gavenko <gavenkoa@gmail.com>
Tue, 02 Sep 2014 18:19:09 +0300
changeset 1 8fb744f2df13
parent 0 2821cc5e0189
child 2 11da0a8fabf3
Speed measure.
2048.html
--- a/2048.html	Tue Sep 02 01:10:03 2014 +0300
+++ b/2048.html	Tue Sep 02 18:19:09 2014 +0300
@@ -28,7 +28,7 @@
 
   <h1>2048</h1>
 
-  <div class="score-area">Score: <span id="score">0</span>, Max: <span id="max">0</span></div>
+  <div class="score-area">Score: <span id="score">0</span>, Max: <span id="max">0</span>, Speed: <span id="speed">0</span> t/s, Turn: <span id="turn">0</span></div>
 
   <div id="message-area"></div>
 
@@ -282,15 +282,23 @@
     ui.score = {};
     var scoreDom = document.getElementById("score");
     var maxDom = document.getElementById("max");
+    var speedDom = document.getElementById("speed");
+    var turnDom = document.getElementById("turn");
     ui.score.clear = function(brd) {
       scoreDom.innerHTML = '0';
       maxDom.innerHTML = '0';
+      speedDom.innerHTML = '0';
+      turnDom.innerHTML = '0';
     }
     ui.score.update = function(brd) {
       var score = board.score(brd);
       scoreDom.innerHTML = '' + score.score;
       maxDom.innerHTML = '' + score.max;
     }
+    ui.score.speed = function(speed, turn) {
+      speedDom.innerHTML = '' + speed;
+      turnDom.innerHTML = '' + turn;
+    }
 
     function start() {
       ui.score.clear();
@@ -384,6 +392,8 @@
 
     function finish() {
       ui.message.clear();
+      var step = 0;
+      var tsFrom = new Date().getTime();
       while (!board.gameOver(board.current)) {
         var tmpBrd = board.create();
         board.copy(board.current, tmpBrd);
@@ -401,9 +411,12 @@
           ui.message.set("Wrong move!");
           return;
         }
+        step++; 
       }
+      var tsTo = new Date().getTime();
       ui.board.update(board.current);
       ui.score.update(board.current);
+      ui.score.speed(step*1000.0/(tsTo-tsFrom), step);
       ui.message.set("Game over!");
     }
     document.getElementById("finish").addEventListener("click", finish);