ui.board.set care about 'val' argument.
authorOleksandr Gavenko <gavenkoa@gmail.com>
Mon, 08 Sep 2014 00:14:21 +0300
changeset 15 0fa97e430a9e
parent 14 9b49e710f5a7
child 16 ab283d2534db
ui.board.set care about 'val' argument.
2048.html
--- a/2048.html	Sun Sep 07 23:30:11 2014 +0300
+++ b/2048.html	Mon Sep 08 00:14:21 2014 +0300
@@ -120,16 +120,24 @@
     var boardDom = document.getElementById("board");
     var ui = {};
     ui.board = {};
+    /* 'val' typesafe.  */
     ui.board.set = function(i, j, val) {
+      if (typeof val !== number)
+        val = 0;
       var dom = boardDom.querySelectorAll("tr")[i].querySelectorAll("td")[j];
-      dom.innerHTML = val;
-      var rgb = 'rgb(255,'+ (255-5*val-2*(val%2)) +','+ (255-10*val-5*((val-1)%2)) +')';
+      if (0 < val && val < 15) {
+        dom.innerHTML = val;
+        var rgb = 'rgb(255,'+ (255-5*val-2*(val%2)) +','+ (255-10*val-5*((val-1)%2)) +')';
+      } else {
+        dom.innerHTML = '';
+        var rgb = 'white';
+      }
       dom.style.backgroundColor = rgb;
     }
     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] >= 1) ? brd[i][j] : "");
+          ui.board.set(i, j, brd[i][j]);
         }
       }
     }