2048.html
changeset 190 d1b12b602915
parent 189 44cb6fbad74d
equal deleted inserted replaced
189:44cb6fbad74d 190:d1b12b602915
     1 <!DOCTYPE html>
     1 <!DOCTYPE html>
     2 <html>
     2 <html>
     3 <head>
     3 <head>
     4   <title>2048 AI</title>
     4   <title>2048 AI</title>
     5   <meta name="viewport" content="width=device-width; initial-scale=1.0"/>
     5   <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
     6   <meta charset="utf-8"/>
     6   <meta charset="utf-8"/>
     7 
     7 
     8   <script src="rule.js"></script>
     8   <script src="rule.js"></script>
     9   <script src="board.js"></script>
     9   <script src="board.js"></script>
    10   <script src="perf.js"></script>
    10   <script src="perf.js"></script>
   608       if (key in keyMoves) {
   608       if (key in keyMoves) {
   609         ui.action[keyMoves[key]](event);
   609         ui.action[keyMoves[key]](event);
   610       }
   610       }
   611     }, false);
   611     }, false);
   612 
   612 
       
   613     ui.swipe = {startX: 0, startY: 0, endX: 0, endY: 0};
       
   614     ui.swipe.direction = function(x, y) {
       
   615       var sensitivityThreshold = boardDom.width * boardDom.height / 10;
       
   616       if (x*x + y*y < sensitivityThreshold)
       
   617         return null;
       
   618       if (Math.abs(x) < Math.abs(y)) {
       
   619         if (y > 0)
       
   620           return "down";
       
   621         else
       
   622           return "up";
       
   623       } else {
       
   624         if (x > 0)
       
   625           return "right";
       
   626         else
       
   627           return "left";
       
   628       }
       
   629     }
       
   630     boardDom.addEventListener("touchstart", function(ev) {
       
   631       ui.swipe.startX = ev.touches[0].pageX;
       
   632       ui.swipe.startY = ev.touches[0].pageY;
       
   633       ev.preventDefault();
       
   634     }, false);
       
   635     boardDom.addEventListener("touchmove", function(ev) {
       
   636       ui.swipe.endX = ev.touches[0].pageX;
       
   637       ui.swipe.endY = ev.touches[0].pageY;
       
   638       ev.preventDefault();
       
   639     }, false);
       
   640     boardDom.addEventListener("touchend", function(ev) {
       
   641       var dir = ui.swipe.direction(ui.swipe.endX - ui.swipe.startX, ui.swipe.endY - ui.swipe.startY);
       
   642       if (dir) {
       
   643         ui.action[dir](event);
       
   644         ev.preventDefault();
       
   645       }
       
   646     }, false);
       
   647 
   613     document.getElementById("test").addEventListener("click", function() {
   648     document.getElementById("test").addEventListener("click", function() {
   614       event.preventDefault(); event.stopPropagation();
   649       event.preventDefault(); event.stopPropagation();
   615       board.move.replaceByBoardJS(ui.brdEngine);
   650       board.move.replaceByBoardJS(ui.brdEngine);
   616     }, false);
   651     }, false);
   617 
   652 
  1067         ui.board.update(ui.board.position);
  1102         ui.board.update(ui.board.position);
  1068         ui.score.update(ui.board.position, ui.board.turn);
  1103         ui.score.update(ui.board.position, ui.board.turn);
  1069         ui.game.checkGameOver();
  1104         ui.game.checkGameOver();
  1070       } catch (ex) { }
  1105       } catch (ex) { }
  1071     } else {
  1106     } else {
  1072       ui.action.start();
  1107       ui.action.reset();
  1073     }
  1108     }
  1074 
  1109 
  1075   </script>
  1110   </script>
  1076   
  1111   
  1077 </body>
  1112 </body>