Make board engine selectable.
authorOleksandr Gavenko <gavenkoa@gmail.com>
Tue, 23 Sep 2014 00:16:57 +0300
changeset 113 fc058d27e829
parent 112 4bb8ed8448bc
child 114 1c3fdde0d481
Make board engine selectable.
2048.html
--- a/2048.html	Tue Sep 23 00:00:57 2014 +0300
+++ b/2048.html	Tue Sep 23 00:16:57 2014 +0300
@@ -143,6 +143,13 @@
     <div class="control">
       <button id="start">Start</button>
       <div class="option"><input type="checkbox" id="2048" checked> 2048</div>
+      <div class="option">
+        <select id="board-engine">
+          <option value="BoardObj">BoardObj</option>
+          <option value="BoardArr2d">BoardArr2d</option>
+          <option value="BoardArr">BoardArr</option>
+        </select> engine
+      </div>
     </div>
     <br>
     <div class="control">
@@ -335,6 +342,12 @@
         return Math.pow(2, val);
       return val;
     }
+    var boardEngineDom = document.getElementById("board-engine");
+    boardEngineDom.addEventListener("change", function() {
+      var engine = boardEngineDom.value;
+      ui.brdEngine = window[engine];
+      localStorage.brdEngine = engine;
+    });
     /* 'val' typesafe.  */
     ui.board.set = function(i, j, val) {
       if (typeof val !== 'number')
@@ -909,8 +922,12 @@
     ////////////////////////////////////////////////////////////////
     // Initialise game.
 
-    ui.brdEngine = BoardArr2d; // TODO make user selectable
-    // ui.brdEngine = BoardObj; // TODO make user selectable
+    if (localStorage.brdEngine) {
+      boardEngineDom.value = localStorage.brdEngine;
+      ui.brdEngine = window[localStorage.brdEngine];
+    } else {
+      ui.brdEngine = BoardArr2d;
+    }
 
     if (localStorage.untilScore) {
       var scoreLimit = parseInt(localStorage.untilScore);