Update/store/restore game turn. Show max value according to value settings.
authorOleksandr Gavenko <gavenkoa@gmail.com>
Thu, 11 Sep 2014 02:16:57 +0300
changeset 46 4dadaff6eba4
parent 45 0bcdec3bdaa8
child 47 74b1f4d0f9a4
Update/store/restore game turn. Show max value according to value settings.
2048.html
rule.js
--- a/2048.html	Thu Sep 11 01:47:54 2014 +0300
+++ b/2048.html	Thu Sep 11 02:16:57 2014 +0300
@@ -329,10 +329,11 @@
       speedDom.innerHTML = '0';
       turnDom.innerHTML = '0';
     }
-    ui.score.update = function(brd) {
+    ui.score.update = function(brd, turn) {
       var score = board.score(brd);
       scoreDom.innerHTML = '' + score.score;
-      maxDom.innerHTML = '' + score.max;
+      maxDom.innerHTML = '' + ui.board.val2048(score.max);
+      turnDom.innerHTML = '' + turn;
     }
     ui.score.speed = function(speed, turn) {
       speedDom.innerHTML = '' + speed;
@@ -397,9 +398,11 @@
     }
     ui.game.finishStep = function() {
       board.putRandom(ui.board.position);
+      ui.board.turn++;
       ui.board.update(ui.board.position);
-      ui.score.update(ui.board.position);
+      ui.score.update(ui.board.position, ui.board.turn);
       localStorage.savedBoard = JSON.stringify(ui.board.position);
+      localStorage.savedTurn = ui.board.turn;
     }
 
     ////////////////////////////////////////////////////////////////
@@ -411,6 +414,7 @@
       ui.score.clear();
       ui.suggestion.clear();
       ui.game.clearMessage();
+      ui.board.turn = 0;
       ui.board.position = board.create();
       board.putRandom(ui.board.position);
       ui.board.update(ui.board.position);
@@ -592,7 +596,12 @@
     if (localStorage.savedBoard) {
       try {
         ui.board.position = JSON.parse(localStorage.savedBoard);
+        if (localStorage.savedTurn)
+          ui.board.turn = JSON.parse(localStorage.savedTurn);
+        else
+          ui.board.turn = 0;
         ui.board.update(ui.board.position);
+        ui.score.update(ui.board.position, ui.board.turn);
       } catch (ex) { }
     } else {
       ui.action.start();
--- a/rule.js	Thu Sep 11 01:47:54 2014 +0300
+++ b/rule.js	Thu Sep 11 02:16:57 2014 +0300
@@ -74,7 +74,7 @@
                 max = val;
         }
     }
-    return {score: score, max: Math.pow(2, max)};
+    return {score: score, max: max};
 }
 
 board.row = {};