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