2048.html
author Oleksandr Gavenko <gavenkoa@gmail.com>
Tue, 02 Sep 2014 01:10:03 +0300
changeset 0 2821cc5e0189
child 1 8fb744f2df13
permissions -rw-r--r--
basic implementation
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
     1
<!DOCTYPE html>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
     2
<html>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
     3
<head>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
     4
  <title>2048 AI</title>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
     5
  <meta name="viewport" content="width=device-width; initial-scale=1.0"/>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
     6
  <meta charset="utf-8"/>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
     7
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
     8
  <style>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
     9
    body {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    10
      width: 100%;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    11
    }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    12
    h1, .score-area, .control-area, #message-area {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    13
      text-align: center;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    14
    }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    15
    #board {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    16
      margin: 10px auto;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    17
    }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    18
    #board td {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    19
      width: 40px;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    20
      height: 40px;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    21
      border: 1px solid red;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    22
      margin: 0;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    23
      text-align: center;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    24
    }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    25
  </style>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    26
</head>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    27
<body>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    28
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    29
  <h1>2048</h1>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    30
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    31
  <div class="score-area">Score: <span id="score">0</span>, Max: <span id="max">0</span></div>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    32
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    33
  <div id="message-area"></div>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    34
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    35
  <table id="board">
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    36
    <tr>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    37
      <td></td>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    38
      <td></td>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    39
      <td></td>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    40
      <td></td>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    41
    </tr>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    42
    <tr>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    43
      <td></td>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    44
      <td></td>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    45
      <td></td>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    46
      <td></td>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    47
    </tr>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    48
    <tr>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    49
      <td></td>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    50
      <td></td>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    51
      <td></td>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    52
      <td></td>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    53
    </tr>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    54
    <tr>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    55
      <td></td>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    56
      <td></td>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    57
      <td></td>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    58
      <td></td>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    59
    </tr>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    60
  </table>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    61
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    62
  <div class="control-area">
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    63
    <div>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    64
      <button id="start">Start</button>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    65
      <button id="step">Step</button>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    66
      <button id="loop">Loop</button>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    67
      <button id="finish">Finish</button>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    68
    </div>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    69
    <div>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    70
      <button id="left">left</button>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    71
      <button id="up">up</button>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    72
      <button id="down">down</button>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    73
      <button id="right">right</button>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    74
    </div>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    75
    <h1>AI</h1>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    76
    <div>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    77
      <button id="ai-random">random</button>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    78
      <button id="ai-next-max-score">next max score</button>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    79
      <button id="ai-next-max-value">next max value</button>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    80
    </div>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    81
  </div>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    82
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    83
  <script>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    84
    "use strict";
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    85
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    86
    var board = {};
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    87
    board.create = function() {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    88
      var brd = [];
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    89
      for (var i = 0; i < 4; i++) {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    90
        brd[i] = [];
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    91
        for (var j = 0; j < 4; j++) {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    92
          brd[i][j] = 0;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    93
        }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    94
      }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    95
      return brd;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    96
    }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    97
    board.copy = function(from, to) {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    98
      for (var i = 0; i < 4; i++) {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    99
        for (var j = 0; j < 4; j++) {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   100
          to[i][j] = from[i][j];
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   101
        }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   102
      }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   103
    }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   104
    board.freeCnt = function(brd) {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   105
      var cnt = 0;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   106
      for (var i = 0; i < 4; i++) {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   107
        for (var j = 0; j < 4; j++) {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   108
          if (brd[i][j] === 0)
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   109
            cnt++;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   110
        }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   111
      }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   112
      return cnt;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   113
    }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   114
    board.gameOver = function(brd) {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   115
      if (board.freeCnt(brd) > 0)
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   116
        return false;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   117
      for (var i = 0; i < 4; i++) {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   118
        for (var j = 0; j < 3; j++) {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   119
          if (brd[i][j] === brd[i][j+1])
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   120
            return false;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   121
        }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   122
      }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   123
      for (var j = 0; j < 4; j++) {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   124
        for (var i = 0; i < 3; i++) {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   125
          if (brd[i][j] === brd[i+1][j])
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   126
            return false;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   127
        }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   128
      }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   129
      return true;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   130
    }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   131
    board.random = function(brd) {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   132
      var cnt = board.freeCnt(brd);
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   133
      cnt = Math.floor(Math.random() * cnt)+1;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   134
      for (var i = 0; i < 4 && cnt > 0; i++) {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   135
        for (var j = 0; j < 4 && cnt > 0; j++) {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   136
          if (brd[i][j] !== 0)
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   137
            continue;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   138
          if (cnt === 1)
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   139
            brd[i][j] = 2;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   140
          cnt--;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   141
        }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   142
      }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   143
    }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   144
    /* http://www.reddit.com/r/2048/comments/214njx/highest_possible_score_for_2048_warning_math */
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   145
    board.score = function(brd) {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   146
      var score = 0;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   147
      var max = 0;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   148
      for (var i = 0; i < 4; i++) {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   149
        for (var j = 0; j < 4; j++) {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   150
          var val = brd[i][j];
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   151
          if (val > 2)
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   152
            score += Math.log2(val) * val;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   153
          if (max < val)
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   154
            max = val;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   155
        }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   156
      }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   157
      return {score: score, max: max};
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   158
    }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   159
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   160
    board.row = {};
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   161
    board.row.init = function() {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   162
      return {stack: [], curr: 0};
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   163
    }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   164
    board.row.push = function(state, val) {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   165
      if (val === 0)
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   166
        return;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   167
      if (state.curr === 0) {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   168
        state.curr = val;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   169
        return;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   170
      }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   171
      if (state.curr === val) {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   172
        state.stack.push(state.curr*2);
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   173
        state.curr = 0;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   174
      } else {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   175
        state.stack.push(state.curr);
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   176
        state.curr = val;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   177
      }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   178
    }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   179
    board.row.finish = function(state) {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   180
      if (state.curr !== 0)
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   181
        state.stack.push(state.curr);
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   182
    }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   183
    board.move = {};
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   184
    board.move.up = function(brd) {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   185
      var updated = false;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   186
      for (var j = 0; j < 4; j++) {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   187
        var state = board.row.init();
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   188
        for (var i = 0; i < 4; i++) {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   189
          board.row.push(state, brd[i][j]);
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   190
        }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   191
        board.row.finish(state);
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   192
        for (var i = 0; i < state.stack.length; i++) {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   193
          if (brd[i][j] !== state.stack[i])
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   194
            updated = true;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   195
          brd[i][j] = state.stack[i];
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   196
        }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   197
        for (; i < 4; i++) {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   198
          if (brd[i][j] !== 0)
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   199
            updated = true;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   200
          brd[i][j] = 0;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   201
        }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   202
      }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   203
      return updated;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   204
    };
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   205
    board.move.down = function(brd) {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   206
      var updated = false;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   207
      for (var j = 0; j < 4; j++) {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   208
        var state = board.row.init();
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   209
        for (var i = 3; i >= 0; i--) {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   210
          board.row.push(state, brd[i][j]);
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   211
        }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   212
        board.row.finish(state);
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   213
        for (var i = 0; i < state.stack.length; i++) {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   214
          if (brd[3-i][j] !== state.stack[i])
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   215
            updated = true;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   216
          brd[3-i][j] = state.stack[i];
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   217
        }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   218
        for (; i < 4; i++) {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   219
          if (brd[3-i][j] !== 0)
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   220
            updated = true;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   221
          brd[3-i][j] = 0;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   222
        }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   223
      }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   224
      return updated;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   225
    };
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   226
    board.move.left = function(brd) {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   227
      var updated = false;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   228
      for (var i = 0; i < 4; i++) {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   229
        var state = board.row.init();
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   230
        for (var j = 0; j < 4; j++) {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   231
          board.row.push(state, brd[i][j]);
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   232
        }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   233
        board.row.finish(state);
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   234
        for (var j = 0; j < state.stack.length; j++) {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   235
          if (brd[i][j] !== state.stack[j])
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   236
            updated = true;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   237
          brd[i][j] = state.stack[j];
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   238
        }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   239
        for (; j < 4; j++) {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   240
          if (brd[i][j] !== 0)
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   241
            updated = true;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   242
          brd[i][j] = 0;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   243
        }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   244
      }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   245
      return updated;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   246
    };
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   247
    board.move.right = function(brd) {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   248
      var updated = false;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   249
      for (var i = 0; i < 4; i++) {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   250
        var state = board.row.init();
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   251
        for (var j = 3; j >= 0; j--) {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   252
          board.row.push(state, brd[i][j]);
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   253
        }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   254
        board.row.finish(state);
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   255
        for (var j = 0; j < state.stack.length; j++) {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   256
          if (brd[i][3-j] !== state.stack[j])
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   257
            updated = true;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   258
          brd[i][3-j] = state.stack[j];
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   259
        }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   260
        for (; j < 4; j++) {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   261
          if (brd[i][3-j] !== 0)
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   262
            updated = true;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   263
          brd[i][3-j] = 0;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   264
        }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   265
      }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   266
      return updated;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   267
    };
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   268
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   269
    var boardDom = document.getElementById("board");
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   270
    var ui = {};
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   271
    ui.board = {};
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   272
    ui.board.set = function(i, j, val) {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   273
      boardDom.querySelectorAll("tr")[i].querySelectorAll("td")[j].innerHTML = val;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   274
    }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   275
    ui.board.update = function(brd) {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   276
      for (var i = 0; i < 4; i++) {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   277
        for (var j = 0; j < 4; j++) {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   278
          ui.board.set(i, j, (brd[i][j] >= 2) ? brd[i][j] : "");
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   279
        }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   280
      }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   281
    }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   282
    ui.score = {};
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   283
    var scoreDom = document.getElementById("score");
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   284
    var maxDom = document.getElementById("max");
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   285
    ui.score.clear = function(brd) {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   286
      scoreDom.innerHTML = '0';
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   287
      maxDom.innerHTML = '0';
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   288
    }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   289
    ui.score.update = function(brd) {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   290
      var score = board.score(brd);
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   291
      scoreDom.innerHTML = '' + score.score;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   292
      maxDom.innerHTML = '' + score.max;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   293
    }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   294
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   295
    function start() {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   296
      ui.score.clear();
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   297
      ui.message.clear();
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   298
      board.current = board.create();
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   299
      board.random(board.current);
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   300
      ui.board.update(board.current);
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   301
    }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   302
    document.getElementById("start").addEventListener("click", start);
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   303
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   304
    function up() {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   305
      var updated = board.move.up(board.current);
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   306
      if (updated) {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   307
        board.random(board.current);
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   308
        ui.board.update(board.current);
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   309
        ui.score.update(board.current);
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   310
      }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   311
    }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   312
    document.getElementById("up").addEventListener("click", up);
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   313
    function down() {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   314
      var updated = board.move.down(board.current);
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   315
      if (updated) {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   316
        board.random(board.current);
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   317
        ui.board.update(board.current);
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   318
        ui.score.update(board.current);
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   319
      }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   320
    }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   321
    document.getElementById("down").addEventListener("click", down);
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   322
    function left() {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   323
      var updated = board.move.left(board.current);
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   324
      if (updated) {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   325
        board.random(board.current);
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   326
        ui.board.update(board.current);
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   327
        ui.score.update(board.current);
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   328
      }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   329
    }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   330
    document.getElementById("left").addEventListener("click", left);
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   331
    function right() {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   332
      var updated = board.move.right(board.current);
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   333
      if (updated) {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   334
        board.random(board.current);
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   335
        ui.board.update(board.current);
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   336
        ui.score.update(board.current);
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   337
      }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   338
    }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   339
    document.getElementById("right").addEventListener("click", right);
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   340
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   341
    document.body.addEventListener("keydown", function(event) {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   342
      var key = event.keyCode || event.which;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   343
      switch (key) {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   344
          case 38: up(); break;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   345
          case 40: down(); break;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   346
          case 37: left(); break;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   347
          case 39: right(); break;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   348
      }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   349
      return false;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   350
    });
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   351
    
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   352
    ui.message = {};
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   353
    var messageDom = document.getElementById("message-area");
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   354
    ui.message.clear = function() {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   355
      messageDom.innerHTML = "";
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   356
    }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   357
    ui.message.set = function(msg) {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   358
      messageDom.innerHTML = msg;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   359
    }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   360
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   361
    function step() {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   362
      ui.message.clear();
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   363
      if (board.gameOver(board.current)) {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   364
        ui.message.set("Game over!");
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   365
        return;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   366
      }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   367
      var tmpBrd = board.create();
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   368
      board.copy(board.current, tmpBrd);
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   369
      var fn = ai.current(tmpBrd);
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   370
      if (typeof fn === 'undefined') {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   371
        ui.message.set("I don't know how to move!");
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   372
        return;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   373
      }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   374
      var updated = board.move[fn].call(null, board.current);
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   375
      if (updated) {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   376
        board.random(board.current);
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   377
        ui.board.update(board.current);
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   378
        ui.score.update(board.current);
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   379
      } else {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   380
        ui.message.set("Wrong move!");
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   381
      }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   382
    }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   383
    document.getElementById("step").addEventListener("click", step);
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   384
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   385
    function finish() {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   386
      ui.message.clear();
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   387
      while (!board.gameOver(board.current)) {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   388
        var tmpBrd = board.create();
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   389
        board.copy(board.current, tmpBrd);
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   390
        var fn = ai.current(tmpBrd);
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   391
        if (typeof fn === 'undefined') {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   392
          ui.message.set("I don't know how to move!");
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   393
          return;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   394
        }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   395
        var updated = board.move[fn].call(null, board.current);
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   396
        if (updated) {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   397
          board.random(board.current);
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   398
        } else {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   399
          ui.board.update(board.current);
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   400
          ui.score.update(board.current);
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   401
          ui.message.set("Wrong move!");
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   402
          return;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   403
        }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   404
      }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   405
      ui.board.update(board.current);
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   406
      ui.score.update(board.current);
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   407
      ui.message.set("Game over!");
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   408
    }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   409
    document.getElementById("finish").addEventListener("click", finish);
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   410
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   411
    var ai = {};
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   412
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   413
    ai.random = function(brd) {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   414
      var tmpBrd = board.create();
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   415
      do {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   416
        var action = ["up", "down", "left", "right"][Math.floor(Math.random()*4)];
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   417
        board.copy(brd, tmpBrd);
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   418
      } while (!board.move[action](tmpBrd));
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   419
      return action;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   420
    }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   421
    document.getElementById("ai-random").addEventListener("click", function() {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   422
      ai.current = ai.random;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   423
    });
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   424
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   425
    ai.nextMaxScore = function(brd) {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   426
      var tmpBrd = board.create();
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   427
      board.copy(brd, tmpBrd);
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   428
      var maxScore = -1;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   429
      var action;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   430
      if (board.move.up(tmpBrd)) {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   431
        maxScore = board.score(tmpBrd).score;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   432
        action = "up";
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   433
      }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   434
      board.copy(brd, tmpBrd);
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   435
      if (board.move.left(tmpBrd)) {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   436
        var score = board.score(tmpBrd).score;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   437
        if (maxScore < score) {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   438
          action = "left";
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   439
          maxScore = score;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   440
        }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   441
      }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   442
      board.copy(brd, tmpBrd);
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   443
      if (board.move.down(tmpBrd)) {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   444
        var score = board.score(tmpBrd).score;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   445
        if (maxScore < score) {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   446
          action = "down";
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   447
          maxScore = score;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   448
        }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   449
      }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   450
      board.copy(brd, tmpBrd);
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   451
      if (board.move.right(tmpBrd)) {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   452
        var score = board.score(tmpBrd).score;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   453
        if (maxScore < score) {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   454
          action = "right";
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   455
          maxScore = score;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   456
        }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   457
      }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   458
      return action;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   459
    }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   460
    document.getElementById("ai-next-max-score").addEventListener("click", function() {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   461
      ai.current = ai.nextMaxScore;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   462
    });
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   463
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   464
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   465
    ai.nextMaxValue = function(brd) {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   466
      var tmpBrd = board.create();
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   467
      board.copy(brd, tmpBrd);
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   468
      var maxMax = -1;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   469
      var action;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   470
      if (board.move.up(tmpBrd)) {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   471
        maxMax = board.score(tmpBrd).max;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   472
        action = "up";
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   473
      }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   474
      board.copy(brd, tmpBrd);
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   475
      if (board.move.left(tmpBrd)) {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   476
        var max = board.score(tmpBrd).max;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   477
        if (maxMax < max) {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   478
          action = "left";
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   479
          maxMax = max;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   480
        }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   481
      }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   482
      board.copy(brd, tmpBrd);
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   483
      if (board.move.down(tmpBrd)) {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   484
        var max = board.score(tmpBrd).max;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   485
        if (maxMax < max) {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   486
          action = "down";
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   487
          maxMax = max;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   488
        }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   489
      }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   490
      board.copy(brd, tmpBrd);
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   491
      if (board.move.right(tmpBrd)) {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   492
        var max = board.score(tmpBrd).max;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   493
        if (maxMax < max) {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   494
          action = "right";
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   495
          maxMax = max;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   496
        }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   497
      }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   498
      return action;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   499
    }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   500
    document.getElementById("ai-next-max-value").addEventListener("click", function() {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   501
      ai.current = ai.nextMaxValue;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   502
    });
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   503
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   504
  </script>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   505
  
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   506
</body>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   507
</html>