Rename methods and use sequential numbers instead of power of 2.
authorOleksandr Gavenko <gavenkoa@gmail.com>
Sat, 06 Sep 2014 22:58:53 +0300
changeset 7 d8106c8c0481
parent 6 eb31d2025a1d
child 8 395195ae7daf
Rename methods and use sequential numbers instead of power of 2.
2048.html
--- a/2048.html	Sat Sep 06 22:57:36 2014 +0300
+++ b/2048.html	Sat Sep 06 22:58:53 2014 +0300
@@ -5,6 +5,9 @@
   <meta name="viewport" content="width=device-width; initial-scale=1.0"/>
   <meta charset="utf-8"/>
 
+  <script src="board.js"></script>
+  <script src="perf.js"></script>
+
   <style>
     body {
       width: 100%;
@@ -155,7 +158,7 @@
       }
       return true;
     }
-    board.random = function(brd) {
+    board.putRandom = function(brd) {
       var cnt = board.freeCnt(brd);
       cnt = Math.floor(Math.random() * cnt)+1;
       for (var i = 0; i < 4 && cnt > 0; i++) {
@@ -163,15 +166,15 @@
           if (brd[i][j] !== 0)
             continue;
           if (cnt === 1)
-            brd[i][j] = 2;
+            brd[i][j] = (Math.random() > .9) ? 2 : 1;
           cnt--;
         }
       }
     }
     /* http://www.reddit.com/r/2048/comments/214njx/highest_possible_score_for_2048_warning_math */
-    var boardScoreTbl = {"0": 0};
+    var boardScoreTbl = [0];
     for (var i = 1, exp = 2; i < 16; i++, exp *= 2) {
-      boardScoreTbl[exp] = (i-1)*exp;
+      boardScoreTbl[i] = (i-1)*exp;
     }
     board.score = function(brd) {
       var score = 0;
@@ -184,7 +187,7 @@
             max = val;
         }
       }
-      return {score: score, max: max};
+      return {score: score, max: Math.pow(2, max)};
     }
 
     board.row = {};
@@ -199,7 +202,7 @@
         return;
       }
       if (state.curr === val) {
-        state.stack.push(state.curr*2);
+        state.stack.push(state.curr+1);
         state.curr = 0;
       } else {
         state.stack.push(state.curr);
@@ -305,7 +308,7 @@
     ui.board.update = function(brd) {
       for (var i = 0; i < 4; i++) {
         for (var j = 0; j < 4; j++) {
-          ui.board.set(i, j, (brd[i][j] >= 2) ? brd[i][j] : "");
+          ui.board.set(i, j, (brd[i][j] >= 1) ? brd[i][j] : "");
         }
       }
     }
@@ -334,7 +337,7 @@
       ui.score.clear();
       ui.message.clear();
       board.current = board.create();
-      board.random(board.current);
+      board.putRandom(board.current);
       ui.board.update(board.current);
     }
     document.getElementById("start").addEventListener("click", start);
@@ -342,7 +345,7 @@
     function up() {
       var updated = board.move.up(board.current);
       if (updated) {
-        board.random(board.current);
+        board.putRandom(board.current);
         ui.board.update(board.current);
         ui.score.update(board.current);
       }
@@ -351,7 +354,7 @@
     function down() {
       var updated = board.move.down(board.current);
       if (updated) {
-        board.random(board.current);
+        board.putRandom(board.current);
         ui.board.update(board.current);
         ui.score.update(board.current);
       }
@@ -360,7 +363,7 @@
     function left() {
       var updated = board.move.left(board.current);
       if (updated) {
-        board.random(board.current);
+        board.putRandom(board.current);
         ui.board.update(board.current);
         ui.score.update(board.current);
       }
@@ -369,7 +372,7 @@
     function right() {
       var updated = board.move.right(board.current);
       if (updated) {
-        board.random(board.current);
+        board.putRandom(board.current);
         ui.board.update(board.current);
         ui.score.update(board.current);
       }
@@ -411,7 +414,7 @@
       }
       var updated = board.move[fn].call(null, board.current);
       if (updated) {
-        board.random(board.current);
+        board.putRandom(board.current);
         ui.board.update(board.current);
         ui.score.update(board.current);
       } else {
@@ -434,7 +437,7 @@
         }
         var updated = board.move[fn].call(null, board.current);
         if (updated) {
-          board.random(board.current);
+          board.putRandom(board.current);
         } else {
           ui.board.update(board.current);
           ui.score.update(board.current);