2048.html
author Oleksandr Gavenko <gavenkoa@gmail.com>
Tue, 09 Sep 2014 18:45:26 +0300
changeset 26 fba85224a8f0
parent 25 2ef10a49a28f
child 27 8f96d09a4d94
permissions -rw-r--r--
Remove debug code.
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
10
70ece7f758a0 Move rule and AI engine to separate files.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 7
diff changeset
     8
  <script src="rule.js"></script>
7
d8106c8c0481 Rename methods and use sequential numbers instead of power of 2.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 4
diff changeset
     9
  <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
    10
  <script src="perf.js"></script>
10
70ece7f758a0 Move rule and AI engine to separate files.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 7
diff changeset
    11
  <script src="ai.js"></script>
7
d8106c8c0481 Rename methods and use sequential numbers instead of power of 2.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 4
diff changeset
    12
0
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    13
  <style>
4
732aef931a9e Report mockup.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 3
diff changeset
    14
    h1, div.area {
0
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    15
      text-align: center;
4
732aef931a9e Report mockup.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 3
diff changeset
    16
      margin: 10px auto;
0
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 {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    19
      margin: 10px auto;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    20
    }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    21
    #board td {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    22
      width: 40px;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    23
      height: 40px;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    24
      border: 1px solid red;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    25
      margin: 0;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    26
      text-align: center;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    27
    }
4
732aef931a9e Report mockup.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 3
diff changeset
    28
    .report > .name {
732aef931a9e Report mockup.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 3
diff changeset
    29
      font-weight: bold;
732aef931a9e Report mockup.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 3
diff changeset
    30
    }
25
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
    31
    div.ai {
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
    32
      border: 1px red solid;
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
    33
      margin-top: 20px;
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
    34
      padding: 2px;
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
    35
      position: relative;
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
    36
      background-color: cornsilk;
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
    37
    }
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
    38
    div.ai.enabled {
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
    39
      background-color: wheat;
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
    40
    }
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
    41
    div.ai > h5 {
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
    42
      margin: 2px 0;
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
    43
    }
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
    44
    button.ai {
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
    45
      position: absolute;
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
    46
      left: 1em;
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
    47
      top: -1em;
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
    48
    }
0
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    49
  </style>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    50
</head>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    51
<body>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    52
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    53
  <h1>2048</h1>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    54
11
2264f7a3f7e0 Swap score parameters.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 10
diff changeset
    55
  <div id="score-area" class="area">Score: <span id="score">0</span>, Max: <span id="max">0</span>, Turn: <span id="turn">0</span>, Speed: <span id="speed">0</span> t/s</div>
0
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    56
4
732aef931a9e Report mockup.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 3
diff changeset
    57
  <div id="message-area" class="area"></div>
0
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    58
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    59
  <table id="board">
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
      <td></td>
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
    </tr>
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
      <td></td>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    68
      <td></td>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    69
      <td></td>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    70
      <td></td>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    71
    </tr>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    72
    <tr>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    73
      <td></td>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    74
      <td></td>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    75
      <td></td>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    76
      <td></td>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    77
    </tr>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    78
    <tr>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    79
      <td></td>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    80
      <td></td>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    81
      <td></td>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    82
      <td></td>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    83
    </tr>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    84
  </table>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    85
4
732aef931a9e Report mockup.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 3
diff changeset
    86
  <div id="control-area" class="area">
0
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
      <button id="start">Start</button>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    89
      <button id="step">Step</button>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    90
      <button id="loop">Loop</button>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    91
      <button id="finish">Finish</button>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    92
    </div>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    93
    <div>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    94
      <button id="left">left</button>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    95
      <button id="up">up</button>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    96
      <button id="down">down</button>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    97
      <button id="right">right</button>
12
a9a44cfc3e08 Moves for 2d array board + test toolkit.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 11
diff changeset
    98
      <button id="test">test</button>
0
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    99
    </div>
25
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   100
    <h1>AIs</h1>
0
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   101
    <div>
25
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   102
      <div class="ai" id="ai-random">
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   103
        <button class="ai">enable</button>
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   104
        <h5>bling random</h5>
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   105
      </div>
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   106
      <div class="ai" id="ai-next-max-score">
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   107
        <button class="ai">enable</button>
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   108
        <h5>next merge makes max score</h5>
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   109
      </div>
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   110
      <div class="ai" id="ai-next-max-value">
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   111
        <button class="ai">enable</button>
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   112
        <h5>next merge makes max value</h5>
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   113
      </div>
26
fba85224a8f0 Remove debug code.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 25
diff changeset
   114
      <div class="ai" id="ai-deep-max-score">
25
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   115
        <button class="ai">enable</button>
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   116
        <h5>deep merges without simulation make max score</h5>
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   117
      </div>
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   118
      <div class="ai" id="ai-deep-max-score-corner">
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   119
        <button class="ai">enable</button>
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   120
        <h5>deep merges without simulation make max score + bonus if max value at corner/edge</h5>
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   121
      </div>
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   122
      <div class="ai" id="ai-expectimax">
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   123
        <button class="ai">enable</button>
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   124
        <h5>expectimax</h5>
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   125
      </div>
0
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   126
    </div>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   127
  </div>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   128
4
732aef931a9e Report mockup.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 3
diff changeset
   129
  <div id="report-area" class="area">
732aef931a9e Report mockup.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 3
diff changeset
   130
    <h1>Reports</h1>
732aef931a9e Report mockup.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 3
diff changeset
   131
    <div class="report" class="area">
732aef931a9e Report mockup.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 3
diff changeset
   132
      <div class="name">next max score</div>
732aef931a9e Report mockup.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 3
diff changeset
   133
      <table>
732aef931a9e Report mockup.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 3
diff changeset
   134
        <tr>
732aef931a9e Report mockup.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 3
diff changeset
   135
          <th>Max</th>
732aef931a9e Report mockup.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 3
diff changeset
   136
          <th>Count</th>
732aef931a9e Report mockup.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 3
diff changeset
   137
          <th>%</th>
732aef931a9e Report mockup.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 3
diff changeset
   138
        </tr>
732aef931a9e Report mockup.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 3
diff changeset
   139
        <tr>
732aef931a9e Report mockup.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 3
diff changeset
   140
          <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
   141
        </tr>
732aef931a9e Report mockup.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 3
diff changeset
   142
        <tr>
732aef931a9e Report mockup.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 3
diff changeset
   143
          <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
   144
        </tr>
732aef931a9e Report mockup.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 3
diff changeset
   145
        <tr>
732aef931a9e Report mockup.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 3
diff changeset
   146
          <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
   147
        </tr>
732aef931a9e Report mockup.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 3
diff changeset
   148
      </table>
732aef931a9e Report mockup.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 3
diff changeset
   149
    </div>
732aef931a9e Report mockup.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 3
diff changeset
   150
  </div>
732aef931a9e Report mockup.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 3
diff changeset
   151
0
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   152
  <script>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   153
    "use strict";
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   154
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   155
    var boardDom = document.getElementById("board");
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   156
    var ui = {};
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   157
    ui.board = {};
15
0fa97e430a9e ui.board.set care about 'val' argument.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 14
diff changeset
   158
    /* 'val' typesafe.  */
0
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   159
    ui.board.set = function(i, j, val) {
16
ab283d2534db Fix typo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 15
diff changeset
   160
      if (typeof val !== 'number')
15
0fa97e430a9e ui.board.set care about 'val' argument.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 14
diff changeset
   161
        val = 0;
13
b3bfa9d1b537 Add color to board depending on values..
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 12
diff changeset
   162
      var dom = boardDom.querySelectorAll("tr")[i].querySelectorAll("td")[j];
15
0fa97e430a9e ui.board.set care about 'val' argument.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 14
diff changeset
   163
      if (0 < val && val < 15) {
0fa97e430a9e ui.board.set care about 'val' argument.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 14
diff changeset
   164
        dom.innerHTML = val;
0fa97e430a9e ui.board.set care about 'val' argument.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 14
diff changeset
   165
        var rgb = 'rgb(255,'+ (255-5*val-2*(val%2)) +','+ (255-10*val-5*((val-1)%2)) +')';
0fa97e430a9e ui.board.set care about 'val' argument.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 14
diff changeset
   166
      } else {
0fa97e430a9e ui.board.set care about 'val' argument.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 14
diff changeset
   167
        dom.innerHTML = '';
0fa97e430a9e ui.board.set care about 'val' argument.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 14
diff changeset
   168
        var rgb = 'white';
0fa97e430a9e ui.board.set care about 'val' argument.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 14
diff changeset
   169
      }
13
b3bfa9d1b537 Add color to board depending on values..
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 12
diff changeset
   170
      dom.style.backgroundColor = rgb;
0
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   171
    }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   172
    ui.board.update = function(brd) {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   173
      for (var i = 0; i < 4; i++) {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   174
        for (var j = 0; j < 4; j++) {
15
0fa97e430a9e ui.board.set care about 'val' argument.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 14
diff changeset
   175
          ui.board.set(i, j, brd[i][j]);
0
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   176
        }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   177
      }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   178
    }
17
06ad7999b9f6 Add board editor.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 16
diff changeset
   179
    ui.board.handleEdit = function(tdDom, i, j) {
06ad7999b9f6 Add board editor.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 16
diff changeset
   180
      if (!tdDom.firstChild || tdDom.firstChild.nodeType === Node.TEXT_NODE) {
06ad7999b9f6 Add board editor.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 16
diff changeset
   181
        if (tdDom.firstChild)
06ad7999b9f6 Add board editor.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 16
diff changeset
   182
          tdDom.removeChild(tdDom.firstChild);
06ad7999b9f6 Add board editor.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 16
diff changeset
   183
        var inputDom = document.createElement('input');
06ad7999b9f6 Add board editor.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 16
diff changeset
   184
        inputDom.value = board.current[i][j];
06ad7999b9f6 Add board editor.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 16
diff changeset
   185
        inputDom.style.width = '1em';
06ad7999b9f6 Add board editor.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 16
diff changeset
   186
        tdDom.appendChild(inputDom);
06ad7999b9f6 Add board editor.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 16
diff changeset
   187
        inputDom.focus();
06ad7999b9f6 Add board editor.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 16
diff changeset
   188
        inputDom.select();
06ad7999b9f6 Add board editor.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 16
diff changeset
   189
        inputDom.addEventListener("blur", function(tdDom, i, j) {
06ad7999b9f6 Add board editor.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 16
diff changeset
   190
          return function(event) {
06ad7999b9f6 Add board editor.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 16
diff changeset
   191
            ui.board.handleEdit(tdDom, i, j);
06ad7999b9f6 Add board editor.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 16
diff changeset
   192
            return false;
06ad7999b9f6 Add board editor.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 16
diff changeset
   193
          }
06ad7999b9f6 Add board editor.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 16
diff changeset
   194
        }(tdDom, i, j));
06ad7999b9f6 Add board editor.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 16
diff changeset
   195
      } else {
06ad7999b9f6 Add board editor.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 16
diff changeset
   196
        var inputDom = tdDom.firstChild;
06ad7999b9f6 Add board editor.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 16
diff changeset
   197
        var val = parseInt(inputDom.value);
06ad7999b9f6 Add board editor.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 16
diff changeset
   198
        if (0 <= val && val < 15) {
06ad7999b9f6 Add board editor.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 16
diff changeset
   199
          board.current[i][j] = val;
06ad7999b9f6 Add board editor.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 16
diff changeset
   200
          ui.board.set(i, j, val);
06ad7999b9f6 Add board editor.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 16
diff changeset
   201
        } else {
06ad7999b9f6 Add board editor.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 16
diff changeset
   202
          ui.board.set(i, j, board.current[i][j]);
06ad7999b9f6 Add board editor.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 16
diff changeset
   203
        }
06ad7999b9f6 Add board editor.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 16
diff changeset
   204
      }
06ad7999b9f6 Add board editor.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 16
diff changeset
   205
    }
06ad7999b9f6 Add board editor.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 16
diff changeset
   206
    ui.board.enableEdit = function() {
06ad7999b9f6 Add board editor.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 16
diff changeset
   207
      var trDoms = boardDom.querySelectorAll("tr");
06ad7999b9f6 Add board editor.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 16
diff changeset
   208
      for (var i = 0; i < 4; i++) {
06ad7999b9f6 Add board editor.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 16
diff changeset
   209
        var tdDoms = trDoms[i].querySelectorAll("td");
06ad7999b9f6 Add board editor.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 16
diff changeset
   210
        for (var j = 0; j < 4; j++) {
06ad7999b9f6 Add board editor.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 16
diff changeset
   211
          var tdDom = tdDoms[j];
06ad7999b9f6 Add board editor.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 16
diff changeset
   212
          tdDom.addEventListener("click", function(tdDom, i, j) {
06ad7999b9f6 Add board editor.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 16
diff changeset
   213
            return function(event) {
06ad7999b9f6 Add board editor.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 16
diff changeset
   214
              ui.board.handleEdit(tdDom, i, j);
06ad7999b9f6 Add board editor.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 16
diff changeset
   215
              return false;
06ad7999b9f6 Add board editor.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 16
diff changeset
   216
            }
06ad7999b9f6 Add board editor.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 16
diff changeset
   217
          }(tdDom, i, j));
06ad7999b9f6 Add board editor.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 16
diff changeset
   218
          tdDom.addEventListener("keyup", function(tdDom, i, j) {
06ad7999b9f6 Add board editor.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 16
diff changeset
   219
            return function(event) {
06ad7999b9f6 Add board editor.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 16
diff changeset
   220
              if (event.keyCode === 13)
06ad7999b9f6 Add board editor.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 16
diff changeset
   221
                ui.board.handleEdit(tdDom, i, j);
06ad7999b9f6 Add board editor.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 16
diff changeset
   222
            }
06ad7999b9f6 Add board editor.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 16
diff changeset
   223
          }(tdDom, i, j));
06ad7999b9f6 Add board editor.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 16
diff changeset
   224
        }
06ad7999b9f6 Add board editor.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 16
diff changeset
   225
      }
06ad7999b9f6 Add board editor.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 16
diff changeset
   226
    }
06ad7999b9f6 Add board editor.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 16
diff changeset
   227
    ui.board.enableEdit();
06ad7999b9f6 Add board editor.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 16
diff changeset
   228
0
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   229
    ui.score = {};
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   230
    var scoreDom = document.getElementById("score");
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   231
    var maxDom = document.getElementById("max");
1
8fb744f2df13 Speed measure.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 0
diff changeset
   232
    var speedDom = document.getElementById("speed");
8fb744f2df13 Speed measure.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 0
diff changeset
   233
    var turnDom = document.getElementById("turn");
0
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   234
    ui.score.clear = function(brd) {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   235
      scoreDom.innerHTML = '0';
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   236
      maxDom.innerHTML = '0';
1
8fb744f2df13 Speed measure.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 0
diff changeset
   237
      speedDom.innerHTML = '0';
8fb744f2df13 Speed measure.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 0
diff changeset
   238
      turnDom.innerHTML = '0';
0
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   239
    }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   240
    ui.score.update = function(brd) {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   241
      var score = board.score(brd);
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   242
      scoreDom.innerHTML = '' + score.score;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   243
      maxDom.innerHTML = '' + score.max;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   244
    }
1
8fb744f2df13 Speed measure.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 0
diff changeset
   245
    ui.score.speed = function(speed, turn) {
8fb744f2df13 Speed measure.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 0
diff changeset
   246
      speedDom.innerHTML = '' + speed;
8fb744f2df13 Speed measure.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 0
diff changeset
   247
      turnDom.innerHTML = '' + turn;
8fb744f2df13 Speed measure.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 0
diff changeset
   248
    }
0
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   249
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   250
    function start() {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   251
      ui.score.clear();
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   252
      ui.message.clear();
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   253
      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
   254
      board.putRandom(board.current);
0
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   255
      ui.board.update(board.current);
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   256
    }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   257
    document.getElementById("start").addEventListener("click", start);
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   258
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   259
    function up() {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   260
      var updated = board.move.up(board.current);
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   261
      if (updated) {
7
d8106c8c0481 Rename methods and use sequential numbers instead of power of 2.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 4
diff changeset
   262
        board.putRandom(board.current);
0
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   263
        ui.board.update(board.current);
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   264
        ui.score.update(board.current);
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   265
      }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   266
    }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   267
    document.getElementById("up").addEventListener("click", up);
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   268
    function down() {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   269
      var updated = board.move.down(board.current);
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   270
      if (updated) {
7
d8106c8c0481 Rename methods and use sequential numbers instead of power of 2.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 4
diff changeset
   271
        board.putRandom(board.current);
0
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   272
        ui.board.update(board.current);
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   273
        ui.score.update(board.current);
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   274
      }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   275
    }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   276
    document.getElementById("down").addEventListener("click", down);
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   277
    function left() {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   278
      var updated = board.move.left(board.current);
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   279
      if (updated) {
7
d8106c8c0481 Rename methods and use sequential numbers instead of power of 2.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 4
diff changeset
   280
        board.putRandom(board.current);
0
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   281
        ui.board.update(board.current);
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   282
        ui.score.update(board.current);
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   283
      }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   284
    }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   285
    document.getElementById("left").addEventListener("click", left);
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   286
    function right() {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   287
      var updated = board.move.right(board.current);
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   288
      if (updated) {
7
d8106c8c0481 Rename methods and use sequential numbers instead of power of 2.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 4
diff changeset
   289
        board.putRandom(board.current);
0
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   290
        ui.board.update(board.current);
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   291
        ui.score.update(board.current);
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   292
      }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   293
    }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   294
    document.getElementById("right").addEventListener("click", right);
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   295
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   296
    document.body.addEventListener("keydown", function(event) {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   297
      var key = event.keyCode || event.which;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   298
      switch (key) {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   299
          case 38: up(); break;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   300
          case 40: down(); break;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   301
          case 37: left(); break;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   302
          case 39: right(); break;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   303
      }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   304
      return false;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   305
    });
12
a9a44cfc3e08 Moves for 2d array board + test toolkit.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 11
diff changeset
   306
a9a44cfc3e08 Moves for 2d array board + test toolkit.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 11
diff changeset
   307
    document.getElementById("test").addEventListener("click", function() {
a9a44cfc3e08 Moves for 2d array board + test toolkit.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 11
diff changeset
   308
      board.move.replaceByBoardJS(ui.brdEngine);
a9a44cfc3e08 Moves for 2d array board + test toolkit.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 11
diff changeset
   309
    });
0
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   310
    
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   311
    ui.message = {};
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   312
    var messageDom = document.getElementById("message-area");
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   313
    ui.message.clear = function() {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   314
      messageDom.innerHTML = "";
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   315
    }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   316
    ui.message.set = function(msg) {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   317
      messageDom.innerHTML = msg;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   318
    }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   319
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   320
    function step() {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   321
      ui.message.clear();
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   322
      if (board.gameOver(board.current)) {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   323
        ui.message.set("Game over!");
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   324
        return;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   325
      }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   326
      var tmpBrd = board.create();
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   327
      board.copy(board.current, tmpBrd);
25
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   328
      var move = ui.ai.current.analyse(tmpBrd);
10
70ece7f758a0 Move rule and AI engine to separate files.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 7
diff changeset
   329
      if (typeof move === 'undefined') {
0
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   330
        ui.message.set("I don't know how to move!");
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   331
        return;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   332
      }
10
70ece7f758a0 Move rule and AI engine to separate files.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 7
diff changeset
   333
      var updated = board.move[move].call(null, board.current);
0
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   334
      if (updated) {
7
d8106c8c0481 Rename methods and use sequential numbers instead of power of 2.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 4
diff changeset
   335
        board.putRandom(board.current);
0
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   336
        ui.board.update(board.current);
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   337
        ui.score.update(board.current);
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   338
      } else {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   339
        ui.message.set("Wrong move!");
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   340
      }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   341
    }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   342
    document.getElementById("step").addEventListener("click", step);
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   343
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   344
    function finish() {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   345
      ui.message.clear();
1
8fb744f2df13 Speed measure.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 0
diff changeset
   346
      var step = 0;
8fb744f2df13 Speed measure.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 0
diff changeset
   347
      var tsFrom = new Date().getTime();
0
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   348
      while (!board.gameOver(board.current)) {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   349
        var tmpBrd = board.create();
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   350
        board.copy(board.current, tmpBrd);
25
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   351
        var move = ui.ai.current.analyse(tmpBrd);
10
70ece7f758a0 Move rule and AI engine to separate files.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 7
diff changeset
   352
        if (typeof move === 'undefined') {
0
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   353
          ui.message.set("I don't know how to move!");
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   354
          return;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   355
        }
10
70ece7f758a0 Move rule and AI engine to separate files.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 7
diff changeset
   356
        var updated = board.move[move].call(null, board.current);
0
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   357
        if (updated) {
7
d8106c8c0481 Rename methods and use sequential numbers instead of power of 2.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 4
diff changeset
   358
          board.putRandom(board.current);
0
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   359
        } else {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   360
          ui.board.update(board.current);
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   361
          ui.score.update(board.current);
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   362
          ui.message.set("Wrong move!");
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   363
          return;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   364
        }
1
8fb744f2df13 Speed measure.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 0
diff changeset
   365
        step++; 
0
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   366
      }
1
8fb744f2df13 Speed measure.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 0
diff changeset
   367
      var tsTo = new Date().getTime();
0
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   368
      ui.board.update(board.current);
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   369
      ui.score.update(board.current);
1
8fb744f2df13 Speed measure.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 0
diff changeset
   370
      ui.score.speed(step*1000.0/(tsTo-tsFrom), step);
0
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   371
      ui.message.set("Game over!");
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   372
    }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   373
    document.getElementById("finish").addEventListener("click", finish);
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   374
10
70ece7f758a0 Move rule and AI engine to separate files.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 7
diff changeset
   375
    ////////////////////////////////////////////////////////////////
70ece7f758a0 Move rule and AI engine to separate files.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 7
diff changeset
   376
    // Register AIs.
0
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   377
25
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   378
    ui.ai = {};
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   379
    ui.ai.current = null;
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   380
    ui.ai.algList = {
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   381
      "ai-random": function() {
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   382
        return new ai.random(ui.brdEngine);
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   383
      },
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   384
      "ai-next-max-score": function() {
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   385
        return new ai.nextMaxScore(ui.brdEngine);
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   386
      },
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   387
      "ai-next-max-value": function() {
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   388
        return new ai.nextMaxValue(ui.brdEngine);
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   389
      },
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   390
      "ai-deep-max-score": function() {
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   391
        return new ai.deepMaxScore(ui.brdEngine);
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   392
      },
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   393
      "ai-deep-max-score-corner": function() {
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   394
        return new ai.deepMaxScoreCorner(ui.brdEngine);
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   395
      },
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   396
      "ai-expectimax": function() {
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   397
        return new ai.expectimax(ui.brdEngine);
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   398
      },
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   399
      // "": function() {
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   400
      //   return new ai.(ui.brdEngine);
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   401
      // },
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   402
    };
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   403
    ui.ai.domList = document.querySelectorAll('div.ai');
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   404
    for (var i = 0; i < ui.ai.domList.length; i++) {
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   405
      ui.ai.domList[i].querySelectorAll('button.ai')[0].addEventListener("click", function (event) {
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   406
        ui.ai.enable(event.target.parentNode);
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   407
      });
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   408
    }
0
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   409
25
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   410
    ui.ai.moveToTop = function(aiDom) {
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   411
      for (var i = 0; i < ui.ai.domList.length; i++) {
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   412
        ui.ai.domList[i].classList.remove('enabled');
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   413
      }
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   414
      var rootDom = aiDom.parentNode;
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   415
      rootDom.removeChild(aiDom);
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   416
      rootDom.insertBefore(aiDom, rootDom.firstChild);
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   417
      aiDom.classList.add('enabled');
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   418
    }
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   419
    ui.ai.enable = function(aiDom) {
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   420
      if (ui.ai.current)
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   421
        ui.ai.current.cleanup();
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   422
      var ai = ui.ai.algList[aiDom.id];
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   423
      ui.ai.moveToTop(aiDom);
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   424
      ui.ai.current = ai();
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   425
    }
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   426
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   427
    ui.brdEngine = BoardArr2d; // TODO make user selectable
0
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   428
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   429
  </script>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   430
  
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   431
</body>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   432
</html>