2048.html
changeset 40 84e0c62adc08
parent 39 a75cc5aa5d19
child 41 359719178176
--- a/2048.html	Wed Sep 10 19:29:45 2014 +0300
+++ b/2048.html	Wed Sep 10 19:46:51 2014 +0300
@@ -338,7 +338,8 @@
     ////////////////////////////////////////////////////////////////
     // Game flow.
 
-    ui.game = {}
+    ui.game = {};
+    ui.game.dirs = ["up", "right", "down", "left"];
 
     var messageDom = document.getElementById("message-area");
     ui.game.clearMessage = function() {
@@ -359,7 +360,7 @@
     ui.game.checkMoveValid = function(move) {
       var tmpBrd = board.create();
       board.copy(board.current, tmpBrd);
-      if (!(move in {"left":0,"right":0,"up":0,"down":0})) {
+      if (ui.game.dirs.indexOf(move) === -1) {
         ui.game.setMessage("AI can't find move!");
         return false;
       }
@@ -385,50 +386,24 @@
     }
     document.getElementById("start").addEventListener("click", ui.action.start);
 
-    ui.action.up = function() {
-      ui.action.clearSuggestion();
-      var updated = board.move.up(board.current);
-      if (updated) {
-        board.putRandom(board.current);
-        ui.board.update(board.current);
-        ui.score.update(board.current);
-        ui.ai.current && ui.ai.current.cleanup();
-      }
-    }
-    document.getElementById("up").addEventListener("click", ui.action.up);
-    ui.action.down = function() {
-      ui.action.clearSuggestion();
-      var updated = board.move.down(board.current);
-      if (updated) {
-        board.putRandom(board.current);
-        ui.board.update(board.current);
-        ui.score.update(board.current);
-        ui.ai.current && ui.ai.current.cleanup();
-      }
+    for (var i = 0; i < ui.game.dirs.length; i++) {
+      var dir = ui.game.dirs[i];
+      ui.action[dir] = function(dir) {
+        return function() {
+          if (ui.game.checkGameOver())
+            return;
+          ui.action.clearSuggestion();
+          var updated = board.move[dir](board.current);
+          if (updated) {
+            board.putRandom(board.current);
+            ui.board.update(board.current);
+            ui.score.update(board.current);
+            ui.ai.current && ui.ai.current.cleanup();
+          }
+        }
+      } (dir);
+      document.getElementById(dir).addEventListener("click", ui.action[dir]);
     }
-    document.getElementById("down").addEventListener("click", ui.action.down);
-    ui.action.left = function() {
-      ui.action.clearSuggestion();
-      var updated = board.move.left(board.current);
-      if (updated) {
-        board.putRandom(board.current);
-        ui.board.update(board.current);
-        ui.score.update(board.current);
-        ui.ai.current && ui.ai.current.cleanup();
-      }
-    }
-    document.getElementById("left").addEventListener("click", ui.action.left);
-    ui.action.right = function() {
-      ui.action.clearSuggestion();
-      var updated = board.move.right(board.current);
-      if (updated) {
-        board.putRandom(board.current);
-        ui.board.update(board.current);
-        ui.score.update(board.current);
-        ui.ai.current && ui.ai.current.cleanup();
-      }
-    }
-    document.getElementById("right").addEventListener("click", ui.action.right);
 
     document.body.addEventListener("keydown", function(event) {
       if (document.activeElement.value)