2048.html
author Oleksandr Gavenko <gavenkoa@gmail.com>
Sat, 20 Sep 2014 09:44:08 +0300
changeset 98 eb4f89e2a001
parent 97 b3cadad6ef14
child 99 df4736e659f2
permissions -rw-r--r--
Clean statistics button.
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>
27
8f96d09a4d94 Add Blind cycle AI. Add example of setting passing.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 26
diff changeset
    14
    body {
8f96d09a4d94 Add Blind cycle AI. Add example of setting passing.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 26
diff changeset
    15
      font-size: 11pt;
8f96d09a4d94 Add Blind cycle AI. Add example of setting passing.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 26
diff changeset
    16
    }
4
732aef931a9e Report mockup.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 3
diff changeset
    17
    h1, div.area {
0
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    18
      text-align: center;
4
732aef931a9e Report mockup.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 3
diff changeset
    19
      margin: 10px auto;
0
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 {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    22
      margin: 10px auto;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    23
    }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    24
    #board td {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    25
      width: 40px;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    26
      height: 40px;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    27
      border: 1px solid red;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    28
      margin: 0;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    29
      text-align: center;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    30
    }
33
f1bbe12e2c3d Highlight score area.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 32
diff changeset
    31
    div.ai-control, div.move-control, div.score-conrol {
30
1d19768ca71d Highlight AI controls.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 27
diff changeset
    32
      border: 1px red solid;
1d19768ca71d Highlight AI controls.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 27
diff changeset
    33
      display: inline-block;
1d19768ca71d Highlight AI controls.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 27
diff changeset
    34
      margin: 10px 0 2px 0;
31
9de51a02b039 Visualise buttons.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 30
diff changeset
    35
      background-color: cornsilk;
9de51a02b039 Visualise buttons.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 30
diff changeset
    36
    }
33
f1bbe12e2c3d Highlight score area.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 32
diff changeset
    37
    div.move-control, div.score-conrol {
f1bbe12e2c3d Highlight score area.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 32
diff changeset
    38
      padding: 3px;
31
9de51a02b039 Visualise buttons.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 30
diff changeset
    39
    }
34
16b711a81a16 Show AI suggestion.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 33
diff changeset
    40
    div.move-control button.suggestion {
16b711a81a16 Show AI suggestion.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 33
diff changeset
    41
      background-color: cyan;
16b711a81a16 Show AI suggestion.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 33
diff changeset
    42
    }
31
9de51a02b039 Visualise buttons.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 30
diff changeset
    43
    div.ai-control {
30
1d19768ca71d Highlight AI controls.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 27
diff changeset
    44
      padding: 2px 2px 2px 1em;
1d19768ca71d Highlight AI controls.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 27
diff changeset
    45
    }
1d19768ca71d Highlight AI controls.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 27
diff changeset
    46
    div.ai-control:before {
1d19768ca71d Highlight AI controls.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 27
diff changeset
    47
      content: "AI: ";
1d19768ca71d Highlight AI controls.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 27
diff changeset
    48
      margin-left: -.5em;
1d19768ca71d Highlight AI controls.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 27
diff changeset
    49
      padding: 0;
1d19768ca71d Highlight AI controls.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 27
diff changeset
    50
    }
71
aa0240db128a Show statistic reports.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 70
diff changeset
    51
    div.ai, div.settings, div#reports {
25
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
    52
      border: 1px red solid;
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
    53
      margin-top: 20px;
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
    54
      padding: 2px;
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
    55
      position: relative;
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
    56
      background-color: cornsilk;
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
    57
    }
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
    58
    div.ai.enabled {
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
    59
      background-color: wheat;
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
    60
    }
75
ab74c80beffd Add score, turn and speed statistic selection.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 74
diff changeset
    61
    div.ai > h5 {
27
8f96d09a4d94 Add Blind cycle AI. Add example of setting passing.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 26
diff changeset
    62
      margin: 2px 0 2px 6em;
25
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
    63
    }
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
    64
    button.ai {
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
    65
      position: absolute;
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
    66
      left: 1em;
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
    67
      top: -1em;
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
    68
    }
98
eb4f89e2a001 Clean statistics button.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 97
diff changeset
    69
    button#statistic, button#statistic-clean {
62
a8e4d8a808c1 Add statistic area.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 61
diff changeset
    70
      display: inline-block;
a8e4d8a808c1 Add statistic area.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 61
diff changeset
    71
      float: left;
a8e4d8a808c1 Add statistic area.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 61
diff changeset
    72
      margin: 3px 4px;
a8e4d8a808c1 Add statistic area.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 61
diff changeset
    73
    }
71
aa0240db128a Show statistic reports.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 70
diff changeset
    74
    div.ai > div.option, div.setting, div.report {
27
8f96d09a4d94 Add Blind cycle AI. Add example of setting passing.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 26
diff changeset
    75
      display: inline-block;
8f96d09a4d94 Add Blind cycle AI. Add example of setting passing.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 26
diff changeset
    76
      float: left;
8f96d09a4d94 Add Blind cycle AI. Add example of setting passing.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 26
diff changeset
    77
      margin: 1px 4px;
36
a18fc2601ce8 Add blind eight random AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 35
diff changeset
    78
      padding: 2px;
a18fc2601ce8 Add blind eight random AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 35
diff changeset
    79
      border: 1px solid tan;
a18fc2601ce8 Add blind eight random AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 35
diff changeset
    80
      border-radius: 4px;
a18fc2601ce8 Add blind eight random AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 35
diff changeset
    81
    }
94
8bbd7570f437 Iterate by AI until score or max value occur.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 88
diff changeset
    82
    div.ai > div.option > input.positive, div.settings > div.setting > input, div.ai-control input.positive {
36
a18fc2601ce8 Add blind eight random AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 35
diff changeset
    83
      text-align: right;
a18fc2601ce8 Add blind eight random AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 35
diff changeset
    84
      max-width: 4em;
61
eed17c93d420 Fix: Unknown property 'margin-rght'. Declaration dropped.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 59
diff changeset
    85
      margin-right: 2px;
27
8f96d09a4d94 Add Blind cycle AI. Add example of setting passing.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 26
diff changeset
    86
    }
8f96d09a4d94 Add Blind cycle AI. Add example of setting passing.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 26
diff changeset
    87
    .clearfix {
8f96d09a4d94 Add Blind cycle AI. Add example of setting passing.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 26
diff changeset
    88
      clear: both;
8f96d09a4d94 Add Blind cycle AI. Add example of setting passing.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 26
diff changeset
    89
    }
31
9de51a02b039 Visualise buttons.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 30
diff changeset
    90
    #test {
9de51a02b039 Visualise buttons.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 30
diff changeset
    91
      display: none;
9de51a02b039 Visualise buttons.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 30
diff changeset
    92
    }
75
ab74c80beffd Add score, turn and speed statistic selection.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 74
diff changeset
    93
    div.report > h5 {
ab74c80beffd Add score, turn and speed statistic selection.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 74
diff changeset
    94
      margin: 2px 0;
ab74c80beffd Add score, turn and speed statistic selection.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 74
diff changeset
    95
    }
71
aa0240db128a Show statistic reports.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 70
diff changeset
    96
    table.report-by-maxval {
aa0240db128a Show statistic reports.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 70
diff changeset
    97
      border-collapse: collapse;
aa0240db128a Show statistic reports.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 70
diff changeset
    98
      margin: 2px;
73
a50828ac954d Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 72
diff changeset
    99
      font-size: 10pt;
71
aa0240db128a Show statistic reports.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 70
diff changeset
   100
    }
aa0240db128a Show statistic reports.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 70
diff changeset
   101
    table.report-by-maxval > tr > th {
aa0240db128a Show statistic reports.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 70
diff changeset
   102
      border: 2px solid green;
aa0240db128a Show statistic reports.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 70
diff changeset
   103
      padding: 2px;
aa0240db128a Show statistic reports.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 70
diff changeset
   104
    }
aa0240db128a Show statistic reports.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 70
diff changeset
   105
    table.report-by-maxval > tr > td {
aa0240db128a Show statistic reports.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 70
diff changeset
   106
      border: 1px solid green;
aa0240db128a Show statistic reports.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 70
diff changeset
   107
      padding: 2px;
aa0240db128a Show statistic reports.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 70
diff changeset
   108
    }
74
93cb48b73b39 Enable config for expectimax. Fix weight function for expectimax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 73
diff changeset
   109
    table.report-by-maxval > tr > td:nth-child(3) {
93cb48b73b39 Enable config for expectimax. Fix weight function for expectimax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 73
diff changeset
   110
      background-color: yellow;
93cb48b73b39 Enable config for expectimax. Fix weight function for expectimax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 73
diff changeset
   111
    }
0
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   112
  </style>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   113
</head>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   114
<body>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   115
88
97acd9f4288b Fix navigation header.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 86
diff changeset
   116
  <div class="area">[ <a href="README.html">About</a> | <a href="CHANGES.html">Changes</a> | <a href="HACKING.html">Hacking</a> | <a href="AUTHORS.html">Authors</a> ]</div>
77
c2bad6178c65 Add project docs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 75
diff changeset
   117
0
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   118
  <h1>2048</h1>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   119
33
f1bbe12e2c3d Highlight score area.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 32
diff changeset
   120
  <div id="score-area" class="area">
f1bbe12e2c3d Highlight score area.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 32
diff changeset
   121
    <div class="score-conrol">
f1bbe12e2c3d Highlight score area.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 32
diff changeset
   122
      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
f1bbe12e2c3d Highlight score area.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 32
diff changeset
   123
    </div>
f1bbe12e2c3d Highlight score area.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 32
diff changeset
   124
  </div>
0
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   125
4
732aef931a9e Report mockup.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 3
diff changeset
   126
  <div id="message-area" class="area"></div>
0
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   127
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   128
  <table id="board">
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   129
    <tr>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   130
      <td></td>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   131
      <td></td>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   132
      <td></td>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   133
      <td></td>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   134
    </tr>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   135
    <tr>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   136
      <td></td>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   137
      <td></td>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   138
      <td></td>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   139
      <td></td>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   140
    </tr>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   141
    <tr>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   142
      <td></td>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   143
      <td></td>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   144
      <td></td>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   145
      <td></td>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   146
    </tr>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   147
    <tr>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   148
      <td></td>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   149
      <td></td>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   150
      <td></td>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   151
      <td></td>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   152
    </tr>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   153
  </table>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   154
4
732aef931a9e Report mockup.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 3
diff changeset
   155
  <div id="control-area" class="area">
0
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   156
    <div>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   157
      <button id="start">Start</button>
38
d476efde7242 2 modes of value showing: 0,1,...,11 and 0,2,4,...,2048.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 37
diff changeset
   158
      <span><input type="checkbox" id="2048" checked> 2048</span>
30
1d19768ca71d Highlight AI controls.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 27
diff changeset
   159
    </div>
1d19768ca71d Highlight AI controls.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 27
diff changeset
   160
    <div class="ai-control">
1d19768ca71d Highlight AI controls.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 27
diff changeset
   161
      <button id="suggest">Suggest</button>
0
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   162
      <button id="step">Step</button>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   163
      <button id="finish">Finish</button>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   164
    </div>
94
8bbd7570f437 Iterate by AI until score or max value occur.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 88
diff changeset
   165
    <div class="ai-control">
8bbd7570f437 Iterate by AI until score or max value occur.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 88
diff changeset
   166
      <button id="until">Continue</button>
8bbd7570f437 Iterate by AI until score or max value occur.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 88
diff changeset
   167
      until <input type="text" class="positive" id="until-score" value="10000"> score
8bbd7570f437 Iterate by AI until score or max value occur.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 88
diff changeset
   168
      and <input type="text" class="positive" id="until-max-value" value="9"> max value
8bbd7570f437 Iterate by AI until score or max value occur.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 88
diff changeset
   169
    </div>
31
9de51a02b039 Visualise buttons.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 30
diff changeset
   170
    <div class="clearfix"></div>
9de51a02b039 Visualise buttons.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 30
diff changeset
   171
    <div class="move-control">
9de51a02b039 Visualise buttons.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 30
diff changeset
   172
      <table>
9de51a02b039 Visualise buttons.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 30
diff changeset
   173
        <tr>
63
251c39718d67 Replace words with arrows to show directions.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 62
diff changeset
   174
          <td></td><td><button id="up">↑</button></td><td></td>
31
9de51a02b039 Visualise buttons.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 30
diff changeset
   175
        </tr>
9de51a02b039 Visualise buttons.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 30
diff changeset
   176
        <tr>
63
251c39718d67 Replace words with arrows to show directions.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 62
diff changeset
   177
          <td><button id="left">←</button></td><td><button id="down">↓</button></td><td><button id="right">→</button></td>
31
9de51a02b039 Visualise buttons.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 30
diff changeset
   178
        </tr>
9de51a02b039 Visualise buttons.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 30
diff changeset
   179
      </table>
12
a9a44cfc3e08 Moves for 2d array board + test toolkit.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 11
diff changeset
   180
      <button id="test">test</button>
0
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   181
    </div>
25
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   182
    <h1>AIs</h1>
0
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   183
    <div>
35
3d56325d7002 Rename AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 34
diff changeset
   184
      <div class="ai" id="ai-blind-random">
25
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   185
        <button class="ai">enable</button>
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   186
        <h5>bling random</h5>
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   187
      </div>
36
a18fc2601ce8 Add blind eight random AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 35
diff changeset
   188
      <div class="ai" id="ai-blind-weight-random">
a18fc2601ce8 Add blind eight random AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 35
diff changeset
   189
        <button class="ai">enable</button>
a18fc2601ce8 Add blind eight random AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 35
diff changeset
   190
        <h5>bling weight random</h5>
a18fc2601ce8 Add blind eight random AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 35
diff changeset
   191
        <div class="option">
55
8ba9dc89be87 Automatically extract config from DOM.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 53
diff changeset
   192
          <input type="text" name="left" class="positive" pattern="[0-9]*[.]?[0-9]*" value="1"/> left weight
36
a18fc2601ce8 Add blind eight random AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 35
diff changeset
   193
        </div>
a18fc2601ce8 Add blind eight random AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 35
diff changeset
   194
        <div class="option">
55
8ba9dc89be87 Automatically extract config from DOM.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 53
diff changeset
   195
          <input type="text" name="right" class="positive" pattern="[0-9]*[.]?[0-9]*" value="1"/> right weight
36
a18fc2601ce8 Add blind eight random AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 35
diff changeset
   196
        </div>
a18fc2601ce8 Add blind eight random AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 35
diff changeset
   197
        <div class="option">
55
8ba9dc89be87 Automatically extract config from DOM.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 53
diff changeset
   198
          <input type="text" name="up" class="positive" pattern="[0-9]*[.]?[0-9]*" value="1"/> up weight
36
a18fc2601ce8 Add blind eight random AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 35
diff changeset
   199
        </div>
a18fc2601ce8 Add blind eight random AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 35
diff changeset
   200
        <div class="option">
55
8ba9dc89be87 Automatically extract config from DOM.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 53
diff changeset
   201
          <input type="text" name="down" class="positive" pattern="[0-9]*[.]?[0-9]*" value="1"/> down weight
36
a18fc2601ce8 Add blind eight random AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 35
diff changeset
   202
        </div>
a18fc2601ce8 Add blind eight random AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 35
diff changeset
   203
        <div class="clearfix"></div>
a18fc2601ce8 Add blind eight random AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 35
diff changeset
   204
      </div>
27
8f96d09a4d94 Add Blind cycle AI. Add example of setting passing.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 26
diff changeset
   205
      <div class="ai" id="ai-blind-cycle">
25
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   206
        <button class="ai">enable</button>
27
8f96d09a4d94 Add Blind cycle AI. Add example of setting passing.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 26
diff changeset
   207
        <h5>blind cycle</h5>
8f96d09a4d94 Add Blind cycle AI. Add example of setting passing.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 26
diff changeset
   208
        <div class="option">
8f96d09a4d94 Add Blind cycle AI. Add example of setting passing.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 26
diff changeset
   209
          <input type="checkbox" name="clockwise"/> clockwise
8f96d09a4d94 Add Blind cycle AI. Add example of setting passing.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 26
diff changeset
   210
        </div>
8f96d09a4d94 Add Blind cycle AI. Add example of setting passing.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 26
diff changeset
   211
        <div class="option">
8f96d09a4d94 Add Blind cycle AI. Add example of setting passing.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 26
diff changeset
   212
          <input type="checkbox" name="whilePossible"/> max move in one direction
8f96d09a4d94 Add Blind cycle AI. Add example of setting passing.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 26
diff changeset
   213
        </div>
8f96d09a4d94 Add Blind cycle AI. Add example of setting passing.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 26
diff changeset
   214
        <div class="clearfix"></div>
25
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   215
      </div>
53
ee53cd2cb69a Join all one step deep AIs into one with linear weight function.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 52
diff changeset
   216
      <div class="ai" id="ai-one-step-deep">
25
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   217
        <button class="ai">enable</button>
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   218
        <h5>next merge makes max value</h5>
53
ee53cd2cb69a Join all one step deep AIs into one with linear weight function.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 52
diff changeset
   219
        <div class="option">
55
8ba9dc89be87 Automatically extract config from DOM.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 53
diff changeset
   220
          <input type="text" name="scoreCoef" class="positive" pattern="[0-9]*[.]?[0-9]*" value="1"/> score weight
53
ee53cd2cb69a Join all one step deep AIs into one with linear weight function.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 52
diff changeset
   221
        </div>
ee53cd2cb69a Join all one step deep AIs into one with linear weight function.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 52
diff changeset
   222
        <div class="option">
55
8ba9dc89be87 Automatically extract config from DOM.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 53
diff changeset
   223
          <input type="text" name="maxValCoef" class="positive" pattern="[0-9]*[.]?[0-9]*" value="0"/> max value weight
53
ee53cd2cb69a Join all one step deep AIs into one with linear weight function.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 52
diff changeset
   224
        </div>
ee53cd2cb69a Join all one step deep AIs into one with linear weight function.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 52
diff changeset
   225
        <div class="option">
55
8ba9dc89be87 Automatically extract config from DOM.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 53
diff changeset
   226
          <input type="text" name="cornerBonus" class="positive" pattern="[0-9]*[.]?[0-9]*" value="100"/> max value at corner bonus
53
ee53cd2cb69a Join all one step deep AIs into one with linear weight function.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 52
diff changeset
   227
        </div>
ee53cd2cb69a Join all one step deep AIs into one with linear weight function.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 52
diff changeset
   228
        <div class="option">
55
8ba9dc89be87 Automatically extract config from DOM.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 53
diff changeset
   229
          <input type="text" name="edgeBonus" class="positive" pattern="[0-9]*[.]?[0-9]*" value="0"/> max value at edge bonus
53
ee53cd2cb69a Join all one step deep AIs into one with linear weight function.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 52
diff changeset
   230
        </div>
ee53cd2cb69a Join all one step deep AIs into one with linear weight function.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 52
diff changeset
   231
        <div class="option">
55
8ba9dc89be87 Automatically extract config from DOM.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 53
diff changeset
   232
          <input type="text" name="freeBonus" class="positive" pattern="[0-9]*[.]?[0-9]*" value="10"/> free cell coefficient
53
ee53cd2cb69a Join all one step deep AIs into one with linear weight function.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 52
diff changeset
   233
        </div>
ee53cd2cb69a Join all one step deep AIs into one with linear weight function.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 52
diff changeset
   234
        <div class="clearfix"></div>
25
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   235
      </div>
26
fba85224a8f0 Remove debug code.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 25
diff changeset
   236
      <div class="ai" id="ai-deep-max-score">
25
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   237
        <button class="ai">enable</button>
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   238
        <h5>deep merges without simulation make max score</h5>
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   239
      </div>
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   240
      <div class="ai" id="ai-deep-max-score-corner">
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   241
        <button class="ai">enable</button>
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   242
        <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
   243
      </div>
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   244
      <div class="ai" id="ai-expectimax">
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   245
        <button class="ai">enable</button>
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   246
        <h5>expectimax</h5>
74
93cb48b73b39 Enable config for expectimax. Fix weight function for expectimax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 73
diff changeset
   247
        <div class="option">
93cb48b73b39 Enable config for expectimax. Fix weight function for expectimax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 73
diff changeset
   248
          <input type="text" name="depth" class="positive" pattern="[0-9]*" value="3"/> recursion depth
93cb48b73b39 Enable config for expectimax. Fix weight function for expectimax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 73
diff changeset
   249
        </div>
93cb48b73b39 Enable config for expectimax. Fix weight function for expectimax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 73
diff changeset
   250
        <div class="option">
93cb48b73b39 Enable config for expectimax. Fix weight function for expectimax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 73
diff changeset
   251
          <input type="text" name="balance" class="positive" pattern="[0-9]*[.]?[0-9]*" value=".9"/> probability of 2
93cb48b73b39 Enable config for expectimax. Fix weight function for expectimax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 73
diff changeset
   252
        </div>
93cb48b73b39 Enable config for expectimax. Fix weight function for expectimax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 73
diff changeset
   253
        <div class="option">
93cb48b73b39 Enable config for expectimax. Fix weight function for expectimax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 73
diff changeset
   254
          <input type="text" name="scoreCoef" class="positive" pattern="[0-9]*[.]?[0-9]*" value="1"/> score weight
93cb48b73b39 Enable config for expectimax. Fix weight function for expectimax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 73
diff changeset
   255
        </div>
93cb48b73b39 Enable config for expectimax. Fix weight function for expectimax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 73
diff changeset
   256
        <div class="option">
93cb48b73b39 Enable config for expectimax. Fix weight function for expectimax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 73
diff changeset
   257
          <input type="text" name="maxValCoef" class="positive" pattern="[0-9]*[.]?[0-9]*" value="0"/> max value weight
93cb48b73b39 Enable config for expectimax. Fix weight function for expectimax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 73
diff changeset
   258
        </div>
93cb48b73b39 Enable config for expectimax. Fix weight function for expectimax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 73
diff changeset
   259
        <div class="option">
93cb48b73b39 Enable config for expectimax. Fix weight function for expectimax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 73
diff changeset
   260
          <input type="text" name="cornerBonus" class="positive" pattern="[0-9]*[.]?[0-9]*" value="100"/> max value at corner bonus
93cb48b73b39 Enable config for expectimax. Fix weight function for expectimax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 73
diff changeset
   261
        </div>
93cb48b73b39 Enable config for expectimax. Fix weight function for expectimax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 73
diff changeset
   262
        <div class="option">
93cb48b73b39 Enable config for expectimax. Fix weight function for expectimax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 73
diff changeset
   263
          <input type="text" name="edgeBonus" class="positive" pattern="[0-9]*[.]?[0-9]*" value="0"/> max value at edge bonus
93cb48b73b39 Enable config for expectimax. Fix weight function for expectimax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 73
diff changeset
   264
        </div>
93cb48b73b39 Enable config for expectimax. Fix weight function for expectimax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 73
diff changeset
   265
        <div class="option">
93cb48b73b39 Enable config for expectimax. Fix weight function for expectimax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 73
diff changeset
   266
          <input type="text" name="freeBonus" class="positive" pattern="[0-9]*[.]?[0-9]*" value="10"/> free cell coefficient
93cb48b73b39 Enable config for expectimax. Fix weight function for expectimax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 73
diff changeset
   267
        </div>
93cb48b73b39 Enable config for expectimax. Fix weight function for expectimax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 73
diff changeset
   268
        <div class="clearfix"></div>
25
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   269
      </div>
0
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   270
    </div>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   271
  </div>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   272
4
732aef931a9e Report mockup.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 3
diff changeset
   273
  <div id="report-area" class="area">
732aef931a9e Report mockup.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 3
diff changeset
   274
    <h1>Reports</h1>
62
a8e4d8a808c1 Add statistic area.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 61
diff changeset
   275
    <div class="settings">
70
01076b56feae Fix histogram calculation.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 69
diff changeset
   276
      <div class="setting"><input type="text" id="stat-count-limit" value="100"> times</div>
64
96e997158562 Gather game statistic.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 63
diff changeset
   277
      <div class="setting">limit to <input type="text" id="stat-time-limit" value="10"> sec</div>
62
a8e4d8a808c1 Add statistic area.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 61
diff changeset
   278
      <button id="statistic">Start</button>
98
eb4f89e2a001 Clean statistics button.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 97
diff changeset
   279
      <button id="statistic-clean">Clean</button>
75
ab74c80beffd Add score, turn and speed statistic selection.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 74
diff changeset
   280
      <div class="setting"><input type="checkbox" id="report-score"/> score</div>
ab74c80beffd Add score, turn and speed statistic selection.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 74
diff changeset
   281
      <div class="setting"><input type="checkbox" id="report-turn"/> turn</div>
ab74c80beffd Add score, turn and speed statistic selection.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 74
diff changeset
   282
      <div class="setting"><input type="checkbox" id="report-speed"/> speed</div>
62
a8e4d8a808c1 Add statistic area.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 61
diff changeset
   283
      <div class="clearfix"></div>
a8e4d8a808c1 Add statistic area.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 61
diff changeset
   284
    </div>
71
aa0240db128a Show statistic reports.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 70
diff changeset
   285
    <div id="reports">
aa0240db128a Show statistic reports.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 70
diff changeset
   286
      <div class="clearfix"></div>
4
732aef931a9e Report mockup.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 3
diff changeset
   287
    </div>
732aef931a9e Report mockup.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 3
diff changeset
   288
  </div>
732aef931a9e Report mockup.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 3
diff changeset
   289
0
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   290
  <script>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   291
    "use strict";
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   292
73
a50828ac954d Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 72
diff changeset
   293
    var ui = {};
a50828ac954d Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 72
diff changeset
   294
a50828ac954d Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 72
diff changeset
   295
    ////////////////////////////////////////////////////////////////
a50828ac954d Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 72
diff changeset
   296
    // UI widgets.
a50828ac954d Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 72
diff changeset
   297
a50828ac954d Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 72
diff changeset
   298
    ui.dom = {};
98
eb4f89e2a001 Clean statistics button.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 97
diff changeset
   299
    ui.dom.clearfix = function() {
eb4f89e2a001 Clean statistics button.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 97
diff changeset
   300
      var divDom = document.createElement('div');
eb4f89e2a001 Clean statistics button.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 97
diff changeset
   301
      divDom.classList.add("clearfix");
eb4f89e2a001 Clean statistics button.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 97
diff changeset
   302
      return divDom;
eb4f89e2a001 Clean statistics button.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 97
diff changeset
   303
    }
73
a50828ac954d Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 72
diff changeset
   304
    ui.dom.table = function(tbl, cols, cfg) {
a50828ac954d Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 72
diff changeset
   305
      var tableDom = document.createElement('table');
a50828ac954d Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 72
diff changeset
   306
      if (typeof cfg.tableClass === 'string')
a50828ac954d Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 72
diff changeset
   307
        tableDom.classList.add(cfg.tableClass);
a50828ac954d Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 72
diff changeset
   308
      var trDom = document.createElement('tr');
a50828ac954d Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 72
diff changeset
   309
      for (var i = 0; i < cols.length; i++) {
a50828ac954d Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 72
diff changeset
   310
        var thDom = document.createElement('td');
a50828ac954d Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 72
diff changeset
   311
        thDom.appendChild(document.createTextNode(cols[i]));
a50828ac954d Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 72
diff changeset
   312
        trDom.appendChild(thDom);
a50828ac954d Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 72
diff changeset
   313
      }
a50828ac954d Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 72
diff changeset
   314
      tableDom.appendChild(trDom);
a50828ac954d Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 72
diff changeset
   315
      for (var i = 0; i < tbl.length; i++) {
a50828ac954d Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 72
diff changeset
   316
        var row = tbl[i];
a50828ac954d Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 72
diff changeset
   317
        var trDom = document.createElement('tr');
a50828ac954d Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 72
diff changeset
   318
        for (var j = 0; j < row.length; j++) {
a50828ac954d Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 72
diff changeset
   319
          var tdDom = document.createElement('td');
a50828ac954d Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 72
diff changeset
   320
          tdDom.appendChild(document.createTextNode(row[j]));
a50828ac954d Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 72
diff changeset
   321
          trDom.appendChild(tdDom);
a50828ac954d Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 72
diff changeset
   322
        }
a50828ac954d Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 72
diff changeset
   323
        tableDom.appendChild(trDom);
a50828ac954d Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 72
diff changeset
   324
      }
a50828ac954d Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 72
diff changeset
   325
      return tableDom;
a50828ac954d Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 72
diff changeset
   326
    };
a50828ac954d Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 72
diff changeset
   327
a50828ac954d Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 72
diff changeset
   328
    ////////////////////////////////////////////////////////////////
a50828ac954d Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 72
diff changeset
   329
    // Board.
a50828ac954d Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 72
diff changeset
   330
0
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   331
    var boardDom = document.getElementById("board");
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   332
    ui.board = {};
38
d476efde7242 2 modes of value showing: 0,1,...,11 and 0,2,4,...,2048.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 37
diff changeset
   333
    ui.board.val2048Dom = document.getElementById('2048');
d476efde7242 2 modes of value showing: 0,1,...,11 and 0,2,4,...,2048.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 37
diff changeset
   334
    ui.board.val2048Dom.addEventListener("click", function(event) {
65
b8028f3e30fb Use preventDefault()/stopPropagation(). Add false to addEventListener().
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 64
diff changeset
   335
      event.preventDefault(); event.stopPropagation();
45
0bcdec3bdaa8 Rename.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 44
diff changeset
   336
      ui.board.update(ui.board.position);
43
8b1577211149 Save val2048 preference.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 42
diff changeset
   337
      localStorage.val2048 = ui.board.val2048Dom.checked;
38
d476efde7242 2 modes of value showing: 0,1,...,11 and 0,2,4,...,2048.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 37
diff changeset
   338
      event.target.blur();
65
b8028f3e30fb Use preventDefault()/stopPropagation(). Add false to addEventListener().
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 64
diff changeset
   339
    }, false);
38
d476efde7242 2 modes of value showing: 0,1,...,11 and 0,2,4,...,2048.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 37
diff changeset
   340
    ui.board.val2048 = function(val) {
d476efde7242 2 modes of value showing: 0,1,...,11 and 0,2,4,...,2048.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 37
diff changeset
   341
      if (ui.board.val2048Dom.checked)
d476efde7242 2 modes of value showing: 0,1,...,11 and 0,2,4,...,2048.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 37
diff changeset
   342
        return Math.pow(2, val);
d476efde7242 2 modes of value showing: 0,1,...,11 and 0,2,4,...,2048.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 37
diff changeset
   343
      return val;
d476efde7242 2 modes of value showing: 0,1,...,11 and 0,2,4,...,2048.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 37
diff changeset
   344
    }
15
0fa97e430a9e ui.board.set care about 'val' argument.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 14
diff changeset
   345
    /* 'val' typesafe.  */
0
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   346
    ui.board.set = function(i, j, val) {
16
ab283d2534db Fix typo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 15
diff changeset
   347
      if (typeof val !== 'number')
15
0fa97e430a9e ui.board.set care about 'val' argument.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 14
diff changeset
   348
        val = 0;
13
b3bfa9d1b537 Add color to board depending on values..
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 12
diff changeset
   349
      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
   350
      if (0 < val && val < 15) {
38
d476efde7242 2 modes of value showing: 0,1,...,11 and 0,2,4,...,2048.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 37
diff changeset
   351
        dom.innerHTML = ui.board.val2048(val);
15
0fa97e430a9e ui.board.set care about 'val' argument.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 14
diff changeset
   352
        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
   353
      } else {
0fa97e430a9e ui.board.set care about 'val' argument.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 14
diff changeset
   354
        dom.innerHTML = '';
0fa97e430a9e ui.board.set care about 'val' argument.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 14
diff changeset
   355
        var rgb = 'white';
0fa97e430a9e ui.board.set care about 'val' argument.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 14
diff changeset
   356
      }
13
b3bfa9d1b537 Add color to board depending on values..
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 12
diff changeset
   357
      dom.style.backgroundColor = rgb;
0
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   358
    }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   359
    ui.board.update = function(brd) {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   360
      for (var i = 0; i < 4; i++) {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   361
        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
   362
          ui.board.set(i, j, brd[i][j]);
0
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   363
        }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   364
      }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   365
    }
17
06ad7999b9f6 Add board editor.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 16
diff changeset
   366
    ui.board.handleEdit = function(tdDom, i, j) {
06ad7999b9f6 Add board editor.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 16
diff changeset
   367
      if (!tdDom.firstChild || tdDom.firstChild.nodeType === Node.TEXT_NODE) {
06ad7999b9f6 Add board editor.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 16
diff changeset
   368
        if (tdDom.firstChild)
06ad7999b9f6 Add board editor.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 16
diff changeset
   369
          tdDom.removeChild(tdDom.firstChild);
06ad7999b9f6 Add board editor.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 16
diff changeset
   370
        var inputDom = document.createElement('input');
45
0bcdec3bdaa8 Rename.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 44
diff changeset
   371
        inputDom.value = ui.board.position[i][j];
17
06ad7999b9f6 Add board editor.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 16
diff changeset
   372
        inputDom.style.width = '1em';
06ad7999b9f6 Add board editor.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 16
diff changeset
   373
        tdDom.appendChild(inputDom);
06ad7999b9f6 Add board editor.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 16
diff changeset
   374
        inputDom.focus();
06ad7999b9f6 Add board editor.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 16
diff changeset
   375
        inputDom.select();
06ad7999b9f6 Add board editor.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 16
diff changeset
   376
        inputDom.addEventListener("blur", function(tdDom, i, j) {
06ad7999b9f6 Add board editor.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 16
diff changeset
   377
          return function(event) {
65
b8028f3e30fb Use preventDefault()/stopPropagation(). Add false to addEventListener().
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 64
diff changeset
   378
            event.preventDefault(); event.stopPropagation();
17
06ad7999b9f6 Add board editor.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 16
diff changeset
   379
            ui.board.handleEdit(tdDom, i, j);
06ad7999b9f6 Add board editor.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 16
diff changeset
   380
          }
65
b8028f3e30fb Use preventDefault()/stopPropagation(). Add false to addEventListener().
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 64
diff changeset
   381
        }(tdDom, i, j), false);
17
06ad7999b9f6 Add board editor.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 16
diff changeset
   382
      } else {
06ad7999b9f6 Add board editor.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 16
diff changeset
   383
        var inputDom = tdDom.firstChild;
06ad7999b9f6 Add board editor.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 16
diff changeset
   384
        var val = parseInt(inputDom.value);
06ad7999b9f6 Add board editor.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 16
diff changeset
   385
        if (0 <= val && val < 15) {
45
0bcdec3bdaa8 Rename.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 44
diff changeset
   386
          ui.board.position[i][j] = val;
17
06ad7999b9f6 Add board editor.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 16
diff changeset
   387
          ui.board.set(i, j, val);
06ad7999b9f6 Add board editor.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 16
diff changeset
   388
        } else {
45
0bcdec3bdaa8 Rename.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 44
diff changeset
   389
          ui.board.set(i, j, ui.board.position[i][j]);
17
06ad7999b9f6 Add board editor.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 16
diff changeset
   390
        }
06ad7999b9f6 Add board editor.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 16
diff changeset
   391
      }
06ad7999b9f6 Add board editor.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 16
diff changeset
   392
    }
06ad7999b9f6 Add board editor.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 16
diff changeset
   393
    ui.board.enableEdit = function() {
06ad7999b9f6 Add board editor.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 16
diff changeset
   394
      var trDoms = boardDom.querySelectorAll("tr");
06ad7999b9f6 Add board editor.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 16
diff changeset
   395
      for (var i = 0; i < 4; i++) {
06ad7999b9f6 Add board editor.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 16
diff changeset
   396
        var tdDoms = trDoms[i].querySelectorAll("td");
06ad7999b9f6 Add board editor.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 16
diff changeset
   397
        for (var j = 0; j < 4; j++) {
06ad7999b9f6 Add board editor.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 16
diff changeset
   398
          var tdDom = tdDoms[j];
06ad7999b9f6 Add board editor.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 16
diff changeset
   399
          tdDom.addEventListener("click", function(tdDom, i, j) {
06ad7999b9f6 Add board editor.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 16
diff changeset
   400
            return function(event) {
65
b8028f3e30fb Use preventDefault()/stopPropagation(). Add false to addEventListener().
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 64
diff changeset
   401
              event.preventDefault(); event.stopPropagation();
17
06ad7999b9f6 Add board editor.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 16
diff changeset
   402
              ui.board.handleEdit(tdDom, i, j);
06ad7999b9f6 Add board editor.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 16
diff changeset
   403
            }
65
b8028f3e30fb Use preventDefault()/stopPropagation(). Add false to addEventListener().
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 64
diff changeset
   404
          }(tdDom, i, j), false);
17
06ad7999b9f6 Add board editor.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 16
diff changeset
   405
          tdDom.addEventListener("keyup", function(tdDom, i, j) {
06ad7999b9f6 Add board editor.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 16
diff changeset
   406
            return function(event) {
65
b8028f3e30fb Use preventDefault()/stopPropagation(). Add false to addEventListener().
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 64
diff changeset
   407
              event.preventDefault(); event.stopPropagation();
17
06ad7999b9f6 Add board editor.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 16
diff changeset
   408
              if (event.keyCode === 13)
06ad7999b9f6 Add board editor.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 16
diff changeset
   409
                ui.board.handleEdit(tdDom, i, j);
06ad7999b9f6 Add board editor.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 16
diff changeset
   410
            }
65
b8028f3e30fb Use preventDefault()/stopPropagation(). Add false to addEventListener().
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 64
diff changeset
   411
          }(tdDom, i, j), false);
17
06ad7999b9f6 Add board editor.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 16
diff changeset
   412
        }
06ad7999b9f6 Add board editor.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 16
diff changeset
   413
      }
06ad7999b9f6 Add board editor.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 16
diff changeset
   414
    }
06ad7999b9f6 Add board editor.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 16
diff changeset
   415
    ui.board.enableEdit();
06ad7999b9f6 Add board editor.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 16
diff changeset
   416
41
359719178176 Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 40
diff changeset
   417
    ////////////////////////////////////////////////////////////////
359719178176 Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 40
diff changeset
   418
    // Score statistic.
359719178176 Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 40
diff changeset
   419
0
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   420
    ui.score = {};
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   421
    var scoreDom = document.getElementById("score");
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   422
    var maxDom = document.getElementById("max");
1
8fb744f2df13 Speed measure.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 0
diff changeset
   423
    var speedDom = document.getElementById("speed");
8fb744f2df13 Speed measure.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 0
diff changeset
   424
    var turnDom = document.getElementById("turn");
0
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   425
    ui.score.clear = function(brd) {
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   426
      scoreDom.innerHTML = '0';
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   427
      maxDom.innerHTML = '0';
1
8fb744f2df13 Speed measure.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 0
diff changeset
   428
      speedDom.innerHTML = '0';
8fb744f2df13 Speed measure.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 0
diff changeset
   429
      turnDom.innerHTML = '0';
0
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   430
    }
46
4dadaff6eba4 Update/store/restore game turn. Show max value according to value settings.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 45
diff changeset
   431
    ui.score.update = function(brd, turn) {
0
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   432
      var score = board.score(brd);
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   433
      scoreDom.innerHTML = '' + score.score;
46
4dadaff6eba4 Update/store/restore game turn. Show max value according to value settings.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 45
diff changeset
   434
      maxDom.innerHTML = '' + ui.board.val2048(score.max);
4dadaff6eba4 Update/store/restore game turn. Show max value according to value settings.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 45
diff changeset
   435
      turnDom.innerHTML = '' + turn;
0
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   436
    }
97
b3cadad6ef14 Fix: turn value sometimes cleared or incorect.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 96
diff changeset
   437
    ui.score.speed = function(speed) {
1
8fb744f2df13 Speed measure.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 0
diff changeset
   438
      speedDom.innerHTML = '' + speed;
8fb744f2df13 Speed measure.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 0
diff changeset
   439
    }
0
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   440
39
a75cc5aa5d19 Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 38
diff changeset
   441
    ////////////////////////////////////////////////////////////////
41
359719178176 Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 40
diff changeset
   442
    // Suggestion UI.
359719178176 Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 40
diff changeset
   443
359719178176 Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 40
diff changeset
   444
    ui.suggestion = {};
359719178176 Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 40
diff changeset
   445
    ui.suggestion.btns = document.querySelectorAll('div.move-control button');
359719178176 Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 40
diff changeset
   446
    ui.suggestion.clear = function() {
359719178176 Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 40
diff changeset
   447
      for (var i = 0; i < ui.suggestion.btns.length; i++)
359719178176 Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 40
diff changeset
   448
        ui.suggestion.btns[i].classList.remove('suggestion');
359719178176 Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 40
diff changeset
   449
    }
359719178176 Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 40
diff changeset
   450
    ui.suggestion.set = function(dir) {
359719178176 Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 40
diff changeset
   451
      document.getElementById(dir).classList.add('suggestion');
359719178176 Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 40
diff changeset
   452
    }
359719178176 Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 40
diff changeset
   453
359719178176 Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 40
diff changeset
   454
    ////////////////////////////////////////////////////////////////
39
a75cc5aa5d19 Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 38
diff changeset
   455
    // Game flow.
a75cc5aa5d19 Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 38
diff changeset
   456
40
84e0c62adc08 Generalise code.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 39
diff changeset
   457
    ui.game = {};
84e0c62adc08 Generalise code.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 39
diff changeset
   458
    ui.game.dirs = ["up", "right", "down", "left"];
39
a75cc5aa5d19 Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 38
diff changeset
   459
a75cc5aa5d19 Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 38
diff changeset
   460
    var messageDom = document.getElementById("message-area");
a75cc5aa5d19 Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 38
diff changeset
   461
    ui.game.clearMessage = function() {
a75cc5aa5d19 Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 38
diff changeset
   462
      messageDom.innerHTML = "";
a75cc5aa5d19 Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 38
diff changeset
   463
    }
a75cc5aa5d19 Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 38
diff changeset
   464
    ui.game.setMessage = function(msg) {
a75cc5aa5d19 Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 38
diff changeset
   465
      messageDom.innerHTML = msg;
a75cc5aa5d19 Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 38
diff changeset
   466
    }
a75cc5aa5d19 Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 38
diff changeset
   467
a75cc5aa5d19 Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 38
diff changeset
   468
    ui.game.checkGameOver = function() {
45
0bcdec3bdaa8 Rename.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 44
diff changeset
   469
      if (board.gameOver(ui.board.position)) {
39
a75cc5aa5d19 Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 38
diff changeset
   470
        ui.game.setMessage("Game over!");
a75cc5aa5d19 Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 38
diff changeset
   471
        return true;
a75cc5aa5d19 Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 38
diff changeset
   472
      } else {
a75cc5aa5d19 Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 38
diff changeset
   473
        return false;
a75cc5aa5d19 Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 38
diff changeset
   474
      }
a75cc5aa5d19 Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 38
diff changeset
   475
    }
a75cc5aa5d19 Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 38
diff changeset
   476
    ui.game.checkMoveValid = function(move) {
a75cc5aa5d19 Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 38
diff changeset
   477
      var tmpBrd = board.create();
45
0bcdec3bdaa8 Rename.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 44
diff changeset
   478
      board.copy(ui.board.position, tmpBrd);
40
84e0c62adc08 Generalise code.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 39
diff changeset
   479
      if (ui.game.dirs.indexOf(move) === -1) {
39
a75cc5aa5d19 Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 38
diff changeset
   480
        ui.game.setMessage("AI can't find move!");
a75cc5aa5d19 Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 38
diff changeset
   481
        return false;
a75cc5aa5d19 Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 38
diff changeset
   482
      }
a75cc5aa5d19 Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 38
diff changeset
   483
      if ( ! board.move[move].call(null, tmpBrd)) {
a75cc5aa5d19 Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 38
diff changeset
   484
        ui.game.setMessage("AI move "+move+" is ivalid!");
a75cc5aa5d19 Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 38
diff changeset
   485
        return false;
a75cc5aa5d19 Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 38
diff changeset
   486
      }
a75cc5aa5d19 Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 38
diff changeset
   487
      return true;
a75cc5aa5d19 Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 38
diff changeset
   488
    }
a75cc5aa5d19 Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 38
diff changeset
   489
41
359719178176 Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 40
diff changeset
   490
    ui.game.beginStep = function() {
359719178176 Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 40
diff changeset
   491
      ui.suggestion.clear();
359719178176 Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 40
diff changeset
   492
      ui.game.clearMessage();
359719178176 Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 40
diff changeset
   493
      if (ui.game.checkGameOver())
359719178176 Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 40
diff changeset
   494
        return false;
359719178176 Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 40
diff changeset
   495
      return true;
359719178176 Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 40
diff changeset
   496
    }
94
8bbd7570f437 Iterate by AI until score or max value occur.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 88
diff changeset
   497
    ui.game.refresh = function() {
45
0bcdec3bdaa8 Rename.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 44
diff changeset
   498
      ui.board.update(ui.board.position);
46
4dadaff6eba4 Update/store/restore game turn. Show max value according to value settings.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 45
diff changeset
   499
      ui.score.update(ui.board.position, ui.board.turn);
45
0bcdec3bdaa8 Rename.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 44
diff changeset
   500
      localStorage.savedBoard = JSON.stringify(ui.board.position);
46
4dadaff6eba4 Update/store/restore game turn. Show max value according to value settings.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 45
diff changeset
   501
      localStorage.savedTurn = ui.board.turn;
41
359719178176 Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 40
diff changeset
   502
    }
94
8bbd7570f437 Iterate by AI until score or max value occur.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 88
diff changeset
   503
    ui.game.finishStep = function() {
8bbd7570f437 Iterate by AI until score or max value occur.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 88
diff changeset
   504
      board.putRandom(ui.board.position);
8bbd7570f437 Iterate by AI until score or max value occur.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 88
diff changeset
   505
      ui.board.turn++;
8bbd7570f437 Iterate by AI until score or max value occur.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 88
diff changeset
   506
      ui.game.refresh();
8bbd7570f437 Iterate by AI until score or max value occur.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 88
diff changeset
   507
    }
41
359719178176 Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 40
diff changeset
   508
39
a75cc5aa5d19 Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 38
diff changeset
   509
    ////////////////////////////////////////////////////////////////
a75cc5aa5d19 Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 38
diff changeset
   510
    // Actions.
a75cc5aa5d19 Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 38
diff changeset
   511
30
1d19768ca71d Highlight AI controls.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 27
diff changeset
   512
    ui.action = {};
1d19768ca71d Highlight AI controls.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 27
diff changeset
   513
1d19768ca71d Highlight AI controls.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 27
diff changeset
   514
    ui.action.start = function() {
0
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   515
      ui.score.clear();
41
359719178176 Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 40
diff changeset
   516
      ui.suggestion.clear();
39
a75cc5aa5d19 Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 38
diff changeset
   517
      ui.game.clearMessage();
46
4dadaff6eba4 Update/store/restore game turn. Show max value according to value settings.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 45
diff changeset
   518
      ui.board.turn = 0;
45
0bcdec3bdaa8 Rename.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 44
diff changeset
   519
      ui.board.position = board.create();
0bcdec3bdaa8 Rename.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 44
diff changeset
   520
      board.putRandom(ui.board.position);
0bcdec3bdaa8 Rename.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 44
diff changeset
   521
      ui.board.update(ui.board.position);
0
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   522
    }
65
b8028f3e30fb Use preventDefault()/stopPropagation(). Add false to addEventListener().
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 64
diff changeset
   523
    document.getElementById("start").addEventListener("click", ui.action.start, false);
0
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   524
40
84e0c62adc08 Generalise code.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 39
diff changeset
   525
    for (var i = 0; i < ui.game.dirs.length; i++) {
84e0c62adc08 Generalise code.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 39
diff changeset
   526
      var dir = ui.game.dirs[i];
84e0c62adc08 Generalise code.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 39
diff changeset
   527
      ui.action[dir] = function(dir) {
65
b8028f3e30fb Use preventDefault()/stopPropagation(). Add false to addEventListener().
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 64
diff changeset
   528
        return function(event) {
b8028f3e30fb Use preventDefault()/stopPropagation(). Add false to addEventListener().
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 64
diff changeset
   529
          event.preventDefault(); event.stopPropagation();
40
84e0c62adc08 Generalise code.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 39
diff changeset
   530
          if (ui.game.checkGameOver())
84e0c62adc08 Generalise code.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 39
diff changeset
   531
            return;
41
359719178176 Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 40
diff changeset
   532
          ui.suggestion.clear();
45
0bcdec3bdaa8 Rename.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 44
diff changeset
   533
          var updated = board.move[dir](ui.board.position);
40
84e0c62adc08 Generalise code.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 39
diff changeset
   534
          if (updated) {
41
359719178176 Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 40
diff changeset
   535
            ui.game.finishStep();
40
84e0c62adc08 Generalise code.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 39
diff changeset
   536
            ui.ai.current && ui.ai.current.cleanup();
84e0c62adc08 Generalise code.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 39
diff changeset
   537
          }
84e0c62adc08 Generalise code.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 39
diff changeset
   538
        }
84e0c62adc08 Generalise code.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 39
diff changeset
   539
      } (dir);
65
b8028f3e30fb Use preventDefault()/stopPropagation(). Add false to addEventListener().
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 64
diff changeset
   540
      document.getElementById(dir).addEventListener("click", ui.action[dir], false);
0
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   541
    }
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   542
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   543
    document.body.addEventListener("keydown", function(event) {
37
fe75d168a605 Ignore board movement when user edit input.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 36
diff changeset
   544
      if (document.activeElement.value)
44
518ab579d24b Prevent from blocking Firefox key binding.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 43
diff changeset
   545
        return;
52
e4e21d2fcbe7 Ignore moves if shift/alt/control key pressed.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 46
diff changeset
   546
      if (event.altKey || event.ctrlKey || event.shiftKey || event.metaKey)
e4e21d2fcbe7 Ignore moves if shift/alt/control key pressed.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 46
diff changeset
   547
        return;
0
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   548
      var key = event.keyCode || event.which;
44
518ab579d24b Prevent from blocking Firefox key binding.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 43
diff changeset
   549
      var keyMoves = {38: "up", 40: "down", 37: "left", 39: "right"};
518ab579d24b Prevent from blocking Firefox key binding.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 43
diff changeset
   550
      if (key in keyMoves) {
65
b8028f3e30fb Use preventDefault()/stopPropagation(). Add false to addEventListener().
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 64
diff changeset
   551
        ui.action[keyMoves[key]](event);
0
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   552
      }
65
b8028f3e30fb Use preventDefault()/stopPropagation(). Add false to addEventListener().
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 64
diff changeset
   553
    }, false);
12
a9a44cfc3e08 Moves for 2d array board + test toolkit.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 11
diff changeset
   554
a9a44cfc3e08 Moves for 2d array board + test toolkit.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 11
diff changeset
   555
    document.getElementById("test").addEventListener("click", function() {
65
b8028f3e30fb Use preventDefault()/stopPropagation(). Add false to addEventListener().
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 64
diff changeset
   556
      event.preventDefault(); event.stopPropagation();
12
a9a44cfc3e08 Moves for 2d array board + test toolkit.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 11
diff changeset
   557
      board.move.replaceByBoardJS(ui.brdEngine);
65
b8028f3e30fb Use preventDefault()/stopPropagation(). Add false to addEventListener().
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 64
diff changeset
   558
    }, false);
34
16b711a81a16 Show AI suggestion.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 33
diff changeset
   559
65
b8028f3e30fb Use preventDefault()/stopPropagation(). Add false to addEventListener().
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 64
diff changeset
   560
    ui.action.suggest = function(event) {
b8028f3e30fb Use preventDefault()/stopPropagation(). Add false to addEventListener().
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 64
diff changeset
   561
      event.preventDefault(); event.stopPropagation();
41
359719178176 Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 40
diff changeset
   562
      if ( ! ui.ai.current) {
359719178176 Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 40
diff changeset
   563
        ui.game.setMessage('Select AI!');
359719178176 Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 40
diff changeset
   564
        return;
359719178176 Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 40
diff changeset
   565
      }
359719178176 Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 40
diff changeset
   566
      if ( ! ui.game.beginStep())
0
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   567
        return;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   568
      var tmpBrd = board.create();
45
0bcdec3bdaa8 Rename.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 44
diff changeset
   569
      board.copy(ui.board.position, tmpBrd);
25
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   570
      var move = ui.ai.current.analyse(tmpBrd);
34
16b711a81a16 Show AI suggestion.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 33
diff changeset
   571
      ui.ai.current.cleanup();
39
a75cc5aa5d19 Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 38
diff changeset
   572
      if ( ! ui.game.checkMoveValid(move))
34
16b711a81a16 Show AI suggestion.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 33
diff changeset
   573
        return;
41
359719178176 Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 40
diff changeset
   574
      ui.suggestion.set(move);
34
16b711a81a16 Show AI suggestion.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 33
diff changeset
   575
    }
65
b8028f3e30fb Use preventDefault()/stopPropagation(). Add false to addEventListener().
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 64
diff changeset
   576
    document.getElementById("suggest").addEventListener("click", ui.action.suggest, false);
34
16b711a81a16 Show AI suggestion.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 33
diff changeset
   577
    ui.action.step = function() {
41
359719178176 Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 40
diff changeset
   578
      if ( ! ui.ai.current) {
359719178176 Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 40
diff changeset
   579
        ui.game.setMessage('Select AI!');
359719178176 Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 40
diff changeset
   580
        return;
359719178176 Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 40
diff changeset
   581
      }
359719178176 Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 40
diff changeset
   582
      if ( ! ui.game.beginStep())
0
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   583
        return;
34
16b711a81a16 Show AI suggestion.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 33
diff changeset
   584
      var tmpBrd = board.create();
45
0bcdec3bdaa8 Rename.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 44
diff changeset
   585
      board.copy(ui.board.position, tmpBrd);
34
16b711a81a16 Show AI suggestion.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 33
diff changeset
   586
      var move = ui.ai.current.analyse(tmpBrd);
16b711a81a16 Show AI suggestion.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 33
diff changeset
   587
      ui.ai.current.cleanup();
39
a75cc5aa5d19 Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 38
diff changeset
   588
      if ( ! ui.game.checkMoveValid(move))
34
16b711a81a16 Show AI suggestion.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 33
diff changeset
   589
        return;
45
0bcdec3bdaa8 Rename.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 44
diff changeset
   590
      board.move[move].call(null, ui.board.position);
41
359719178176 Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 40
diff changeset
   591
      ui.game.finishStep();
0
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   592
    }
65
b8028f3e30fb Use preventDefault()/stopPropagation(). Add false to addEventListener().
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 64
diff changeset
   593
    document.getElementById("step").addEventListener("click", ui.action.step, false);
0
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   594
30
1d19768ca71d Highlight AI controls.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 27
diff changeset
   595
    ui.action.finish = function() {
41
359719178176 Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 40
diff changeset
   596
      if ( ! ui.ai.current) {
359719178176 Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 40
diff changeset
   597
        ui.game.setMessage('Select AI!');
359719178176 Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 40
diff changeset
   598
        return;
359719178176 Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 40
diff changeset
   599
      }
359719178176 Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 40
diff changeset
   600
      ui.game.beginStep();
1
8fb744f2df13 Speed measure.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 0
diff changeset
   601
      var step = 0;
66
c62a1051cef7 Move object creation out of loop.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 65
diff changeset
   602
      var safeBdr = board.create();
1
8fb744f2df13 Speed measure.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 0
diff changeset
   603
      var tsFrom = new Date().getTime();
45
0bcdec3bdaa8 Rename.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 44
diff changeset
   604
      while (!board.gameOver(ui.board.position)) {
66
c62a1051cef7 Move object creation out of loop.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 65
diff changeset
   605
        board.copy(ui.board.position, safeBdr);
c62a1051cef7 Move object creation out of loop.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 65
diff changeset
   606
        var move = ui.ai.current.analyse(safeBdr);
10
70ece7f758a0 Move rule and AI engine to separate files.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 7
diff changeset
   607
        if (typeof move === 'undefined') {
39
a75cc5aa5d19 Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 38
diff changeset
   608
          ui.game.setMessage("I don't know how to move!");
0
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   609
          return;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   610
        }
45
0bcdec3bdaa8 Rename.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 44
diff changeset
   611
        var updated = board.move[move].call(null, ui.board.position);
0
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   612
        if (updated) {
45
0bcdec3bdaa8 Rename.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 44
diff changeset
   613
          board.putRandom(ui.board.position);
0
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   614
        } else {
97
b3cadad6ef14 Fix: turn value sometimes cleared or incorect.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 96
diff changeset
   615
          ui.board.turn += step;
b3cadad6ef14 Fix: turn value sometimes cleared or incorect.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 96
diff changeset
   616
          ui.game.refresh();
39
a75cc5aa5d19 Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 38
diff changeset
   617
          ui.game.setMessage("Wrong move!");
0
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   618
          return;
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   619
        }
1
8fb744f2df13 Speed measure.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 0
diff changeset
   620
        step++; 
0
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   621
      }
1
8fb744f2df13 Speed measure.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 0
diff changeset
   622
      var tsTo = new Date().getTime();
97
b3cadad6ef14 Fix: turn value sometimes cleared or incorect.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 96
diff changeset
   623
      ui.board.turn += step;
41
359719178176 Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 40
diff changeset
   624
      ui.game.finishStep();
97
b3cadad6ef14 Fix: turn value sometimes cleared or incorect.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 96
diff changeset
   625
      ui.score.speed(parseFloat((step*1000.0/(tsTo-tsFrom)).toPrecision(3)));
39
a75cc5aa5d19 Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 38
diff changeset
   626
      ui.game.setMessage("Game over!");
96
a9a6e08c9e35 Remove unnecessary check.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 95
diff changeset
   627
      ui.ai.current.cleanup();
0
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   628
    }
65
b8028f3e30fb Use preventDefault()/stopPropagation(). Add false to addEventListener().
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 64
diff changeset
   629
    document.getElementById("finish").addEventListener("click", ui.action.finish, false);
0
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   630
94
8bbd7570f437 Iterate by AI until score or max value occur.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 88
diff changeset
   631
    ui.action.until = function() {
8bbd7570f437 Iterate by AI until score or max value occur.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 88
diff changeset
   632
      if ( ! ui.ai.current) {
8bbd7570f437 Iterate by AI until score or max value occur.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 88
diff changeset
   633
        ui.game.setMessage('Select AI!');
8bbd7570f437 Iterate by AI until score or max value occur.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 88
diff changeset
   634
        return;
8bbd7570f437 Iterate by AI until score or max value occur.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 88
diff changeset
   635
      }
8bbd7570f437 Iterate by AI until score or max value occur.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 88
diff changeset
   636
      if ( ! ui.game.beginStep())
8bbd7570f437 Iterate by AI until score or max value occur.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 88
diff changeset
   637
        return;
8bbd7570f437 Iterate by AI until score or max value occur.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 88
diff changeset
   638
      var step = 0;
8bbd7570f437 Iterate by AI until score or max value occur.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 88
diff changeset
   639
      var safeBdr = board.create();
8bbd7570f437 Iterate by AI until score or max value occur.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 88
diff changeset
   640
      var tsFrom = new Date().getTime();
8bbd7570f437 Iterate by AI until score or max value occur.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 88
diff changeset
   641
      var scoreLimit = parseInt(document.getElementById("until-score").value);
8bbd7570f437 Iterate by AI until score or max value occur.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 88
diff changeset
   642
      if (!isFinite(scoreLimit) || scoreLimit < 0) {
8bbd7570f437 Iterate by AI until score or max value occur.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 88
diff changeset
   643
        scoreLimit = 1;
8bbd7570f437 Iterate by AI until score or max value occur.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 88
diff changeset
   644
        document.getElementById("until-score").value = scoreLimit;
8bbd7570f437 Iterate by AI until score or max value occur.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 88
diff changeset
   645
      }
8bbd7570f437 Iterate by AI until score or max value occur.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 88
diff changeset
   646
      var maxValLimit = parseInt(document.getElementById("until-max-value").value);
8bbd7570f437 Iterate by AI until score or max value occur.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 88
diff changeset
   647
      if (!isFinite(maxValLimit) || maxValLimit < 0 || maxValLimit > 13) {
8bbd7570f437 Iterate by AI until score or max value occur.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 88
diff changeset
   648
        maxValLimit = 1;
8bbd7570f437 Iterate by AI until score or max value occur.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 88
diff changeset
   649
        document.getElementById("until-max-value").value = maxValLimit;
8bbd7570f437 Iterate by AI until score or max value occur.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 88
diff changeset
   650
      }
95
804e751a3b7f Save and restore continuation limits.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 94
diff changeset
   651
      localStorage.untilScore = scoreLimit;
804e751a3b7f Save and restore continuation limits.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 94
diff changeset
   652
      localStorage.untilMaxVal = maxValLimit;
94
8bbd7570f437 Iterate by AI until score or max value occur.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 88
diff changeset
   653
      while (true) {
8bbd7570f437 Iterate by AI until score or max value occur.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 88
diff changeset
   654
        if (board.gameOver(ui.board.position)) {
8bbd7570f437 Iterate by AI until score or max value occur.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 88
diff changeset
   655
          ui.game.setMessage("Game over!");
8bbd7570f437 Iterate by AI until score or max value occur.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 88
diff changeset
   656
          break;
8bbd7570f437 Iterate by AI until score or max value occur.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 88
diff changeset
   657
        }
8bbd7570f437 Iterate by AI until score or max value occur.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 88
diff changeset
   658
        var stat = board.score(ui.board.position);
8bbd7570f437 Iterate by AI until score or max value occur.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 88
diff changeset
   659
        if (stat.score >= scoreLimit && stat.max >= maxValLimit)
8bbd7570f437 Iterate by AI until score or max value occur.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 88
diff changeset
   660
          break;
8bbd7570f437 Iterate by AI until score or max value occur.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 88
diff changeset
   661
        board.copy(ui.board.position, safeBdr);
8bbd7570f437 Iterate by AI until score or max value occur.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 88
diff changeset
   662
        var move = ui.ai.current.analyse(safeBdr);
8bbd7570f437 Iterate by AI until score or max value occur.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 88
diff changeset
   663
        if (typeof move === 'undefined') {
8bbd7570f437 Iterate by AI until score or max value occur.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 88
diff changeset
   664
          ui.game.setMessage("I don't know how to move!");
8bbd7570f437 Iterate by AI until score or max value occur.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 88
diff changeset
   665
          return;
8bbd7570f437 Iterate by AI until score or max value occur.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 88
diff changeset
   666
        }
8bbd7570f437 Iterate by AI until score or max value occur.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 88
diff changeset
   667
        var updated = board.move[move].call(null, ui.board.position);
8bbd7570f437 Iterate by AI until score or max value occur.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 88
diff changeset
   668
        if (updated) {
8bbd7570f437 Iterate by AI until score or max value occur.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 88
diff changeset
   669
          board.putRandom(ui.board.position);
8bbd7570f437 Iterate by AI until score or max value occur.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 88
diff changeset
   670
        } else {
8bbd7570f437 Iterate by AI until score or max value occur.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 88
diff changeset
   671
          ui.game.finishStep();
8bbd7570f437 Iterate by AI until score or max value occur.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 88
diff changeset
   672
          ui.game.setMessage("Wrong move!");
8bbd7570f437 Iterate by AI until score or max value occur.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 88
diff changeset
   673
          return;
8bbd7570f437 Iterate by AI until score or max value occur.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 88
diff changeset
   674
        }
8bbd7570f437 Iterate by AI until score or max value occur.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 88
diff changeset
   675
        step++; 
8bbd7570f437 Iterate by AI until score or max value occur.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 88
diff changeset
   676
      }
8bbd7570f437 Iterate by AI until score or max value occur.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 88
diff changeset
   677
      var tsTo = new Date().getTime();
97
b3cadad6ef14 Fix: turn value sometimes cleared or incorect.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 96
diff changeset
   678
      ui.board.turn += step;
94
8bbd7570f437 Iterate by AI until score or max value occur.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 88
diff changeset
   679
      ui.game.refresh();
97
b3cadad6ef14 Fix: turn value sometimes cleared or incorect.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 96
diff changeset
   680
      ui.score.speed(parseFloat((step*1000.0/(tsTo-tsFrom)).toPrecision(3)));
96
a9a6e08c9e35 Remove unnecessary check.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 95
diff changeset
   681
      ui.ai.current.cleanup();
94
8bbd7570f437 Iterate by AI until score or max value occur.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 88
diff changeset
   682
    }
8bbd7570f437 Iterate by AI until score or max value occur.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 88
diff changeset
   683
    document.getElementById("until").addEventListener("click", ui.action.until, false);
8bbd7570f437 Iterate by AI until score or max value occur.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 88
diff changeset
   684
10
70ece7f758a0 Move rule and AI engine to separate files.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 7
diff changeset
   685
    ////////////////////////////////////////////////////////////////
70ece7f758a0 Move rule and AI engine to separate files.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 7
diff changeset
   686
    // Register AIs.
0
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   687
25
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   688
    ui.ai = {};
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   689
    ui.ai.current = null;
53
ee53cd2cb69a Join all one step deep AIs into one with linear weight function.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 52
diff changeset
   690
    ui.ai.parseCfg = function(aiDom, cfg) {
55
8ba9dc89be87 Automatically extract config from DOM.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 53
diff changeset
   691
      var optDoms = aiDom.querySelectorAll("div.option > input.positive[type='text']");
53
ee53cd2cb69a Join all one step deep AIs into one with linear weight function.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 52
diff changeset
   692
      for (var i = 0; i < optDoms.length; i++) {
ee53cd2cb69a Join all one step deep AIs into one with linear weight function.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 52
diff changeset
   693
        var val = parseFloat(optDoms[i].value);
55
8ba9dc89be87 Automatically extract config from DOM.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 53
diff changeset
   694
        if (val === NaN || val < 0) {
8ba9dc89be87 Automatically extract config from DOM.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 53
diff changeset
   695
          ui.game.setMessage('' + optDoms[i].name + ' is not a positive number!');
8ba9dc89be87 Automatically extract config from DOM.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 53
diff changeset
   696
          cfg[optDoms[i].name] = 1;
53
ee53cd2cb69a Join all one step deep AIs into one with linear weight function.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 52
diff changeset
   697
          continue;
ee53cd2cb69a Join all one step deep AIs into one with linear weight function.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 52
diff changeset
   698
        }
ee53cd2cb69a Join all one step deep AIs into one with linear weight function.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 52
diff changeset
   699
        cfg[optDoms[i].name] = val;
ee53cd2cb69a Join all one step deep AIs into one with linear weight function.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 52
diff changeset
   700
      }
ee53cd2cb69a Join all one step deep AIs into one with linear weight function.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 52
diff changeset
   701
    }
55
8ba9dc89be87 Automatically extract config from DOM.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 53
diff changeset
   702
25
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   703
    ui.ai.algList = {
35
3d56325d7002 Rename AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 34
diff changeset
   704
      "ai-blind-random": function() {
59
28613a54c71c Capitilize AI names.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 55
diff changeset
   705
        return new ai.BlindRandom(ui.brdEngine);
25
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   706
      },
36
a18fc2601ce8 Add blind eight random AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 35
diff changeset
   707
      "ai-blind-weight-random": function(aiDom) {
a18fc2601ce8 Add blind eight random AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 35
diff changeset
   708
        var cfg = {};
55
8ba9dc89be87 Automatically extract config from DOM.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 53
diff changeset
   709
        ui.ai.parseCfg(aiDom, cfg);
59
28613a54c71c Capitilize AI names.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 55
diff changeset
   710
        return new ai.BlindWeightRandom(ui.brdEngine, cfg);
36
a18fc2601ce8 Add blind eight random AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 35
diff changeset
   711
      },
27
8f96d09a4d94 Add Blind cycle AI. Add example of setting passing.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 26
diff changeset
   712
      "ai-blind-cycle": function(aiDom) {
8f96d09a4d94 Add Blind cycle AI. Add example of setting passing.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 26
diff changeset
   713
        var cfg = {};
8f96d09a4d94 Add Blind cycle AI. Add example of setting passing.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 26
diff changeset
   714
        cfg.clockwise = aiDom.querySelectorAll("input[name='clockwise']")[0].checked;
8f96d09a4d94 Add Blind cycle AI. Add example of setting passing.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 26
diff changeset
   715
        cfg.whilePossible = aiDom.querySelectorAll("input[name='whilePossible']")[0].checked;
59
28613a54c71c Capitilize AI names.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 55
diff changeset
   716
        return new ai.BlindCycle(ui.brdEngine, cfg);
27
8f96d09a4d94 Add Blind cycle AI. Add example of setting passing.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 26
diff changeset
   717
      },
53
ee53cd2cb69a Join all one step deep AIs into one with linear weight function.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 52
diff changeset
   718
      "ai-one-step-deep": function(aiDom) {
ee53cd2cb69a Join all one step deep AIs into one with linear weight function.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 52
diff changeset
   719
        var cfg = {};
ee53cd2cb69a Join all one step deep AIs into one with linear weight function.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 52
diff changeset
   720
        ui.ai.parseCfg(aiDom, cfg);
59
28613a54c71c Capitilize AI names.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 55
diff changeset
   721
        return new ai.OneStepDeep(ui.brdEngine, cfg);
25
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   722
      },
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   723
      "ai-deep-max-score": function() {
59
28613a54c71c Capitilize AI names.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 55
diff changeset
   724
        return new ai.DeepMaxScore(ui.brdEngine);
25
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   725
      },
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   726
      "ai-deep-max-score-corner": function() {
59
28613a54c71c Capitilize AI names.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 55
diff changeset
   727
        return new ai.DeepMaxScoreCorner(ui.brdEngine);
25
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   728
      },
74
93cb48b73b39 Enable config for expectimax. Fix weight function for expectimax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 73
diff changeset
   729
      "ai-expectimax": function(aiDom) {
93cb48b73b39 Enable config for expectimax. Fix weight function for expectimax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 73
diff changeset
   730
        var cfg = {};
93cb48b73b39 Enable config for expectimax. Fix weight function for expectimax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 73
diff changeset
   731
        ui.ai.parseCfg(aiDom, cfg);
93cb48b73b39 Enable config for expectimax. Fix weight function for expectimax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 73
diff changeset
   732
        return new ai.expectimax(ui.brdEngine, cfg);
25
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   733
      },
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   734
      // "": function() {
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   735
      //   return new ai.(ui.brdEngine);
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   736
      // },
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   737
    };
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   738
    ui.ai.domList = document.querySelectorAll('div.ai');
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   739
    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
   740
      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
   741
        ui.ai.enable(event.target.parentNode);
65
b8028f3e30fb Use preventDefault()/stopPropagation(). Add false to addEventListener().
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 64
diff changeset
   742
      }, false);
25
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   743
    }
0
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   744
25
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   745
    ui.ai.moveToTop = function(aiDom) {
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   746
      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
   747
        ui.ai.domList[i].classList.remove('enabled');
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   748
      }
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   749
      var rootDom = aiDom.parentNode;
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   750
      rootDom.removeChild(aiDom);
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   751
      rootDom.insertBefore(aiDom, rootDom.firstChild);
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   752
      aiDom.classList.add('enabled');
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   753
    }
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   754
    ui.ai.enable = function(aiDom) {
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   755
      if (ui.ai.current)
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   756
        ui.ai.current.cleanup();
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   757
      ui.ai.moveToTop(aiDom);
27
8f96d09a4d94 Add Blind cycle AI. Add example of setting passing.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 26
diff changeset
   758
      ui.ai.current = ui.ai.algList[aiDom.id](aiDom);
71
aa0240db128a Show statistic reports.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 70
diff changeset
   759
      ui.ai.currentName = aiDom.id;
25
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   760
    }
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   761
62
a8e4d8a808c1 Add statistic area.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 61
diff changeset
   762
a8e4d8a808c1 Add statistic area.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 61
diff changeset
   763
    ////////////////////////////////////////////////////////////////
a8e4d8a808c1 Add statistic area.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 61
diff changeset
   764
    // Reports and statistic.
a8e4d8a808c1 Add statistic area.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 61
diff changeset
   765
71
aa0240db128a Show statistic reports.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 70
diff changeset
   766
    var reportsDom = document.getElementById('reports');
aa0240db128a Show statistic reports.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 70
diff changeset
   767
73
a50828ac954d Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 72
diff changeset
   768
    ui.report = {};
a50828ac954d Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 72
diff changeset
   769
a50828ac954d Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 72
diff changeset
   770
    ui.report.stat = function() {
68
f8d310e3aecf Add time and turn count to statistic data.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 67
diff changeset
   771
      var stats = [];
64
96e997158562 Gather game statistic.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 63
diff changeset
   772
      var cnt = parseInt(document.getElementById('stat-count-limit').value);
96e997158562 Gather game statistic.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 63
diff changeset
   773
      if (isNaN(cnt) || !isFinite(cnt) || cnt < 1)
96e997158562 Gather game statistic.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 63
diff changeset
   774
        cnt = 100;
96e997158562 Gather game statistic.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 63
diff changeset
   775
      var tsLimit = parseFloat(document.getElementById('stat-count-limit').value);
96e997158562 Gather game statistic.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 63
diff changeset
   776
      if (isNaN(tsLimit) || !isFinite(tsLimit) || tsLimit < 1 || tsLimit > 60)
96e997158562 Gather game statistic.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 63
diff changeset
   777
        tsLimit = 1000 * 10;
96e997158562 Gather game statistic.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 63
diff changeset
   778
      else
96e997158562 Gather game statistic.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 63
diff changeset
   779
        tsLimit = 1000 * tsLimit;
66
c62a1051cef7 Move object creation out of loop.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 65
diff changeset
   780
      var safeBdr = board.create();
68
f8d310e3aecf Add time and turn count to statistic data.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 67
diff changeset
   781
      var tsLimitFrom = new Date().getTime();
72
0d1e5bbcde82 Fix value ordering. Fix game counting when time limit reached.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 71
diff changeset
   782
      for (var gameCnt = 0; gameCnt++ < cnt; void(0)) {
64
96e997158562 Gather game statistic.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 63
diff changeset
   783
        var turn = 0;
96e997158562 Gather game statistic.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 63
diff changeset
   784
        var brd = board.create();
96e997158562 Gather game statistic.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 63
diff changeset
   785
        board.putRandom(brd);
68
f8d310e3aecf Add time and turn count to statistic data.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 67
diff changeset
   786
        var tsFrom = new Date().getTime();
64
96e997158562 Gather game statistic.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 63
diff changeset
   787
        while (!board.gameOver(brd)) {
66
c62a1051cef7 Move object creation out of loop.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 65
diff changeset
   788
          board.copy(brd, safeBdr);
c62a1051cef7 Move object creation out of loop.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 65
diff changeset
   789
          var move = ui.ai.current.analyse(safeBdr);
64
96e997158562 Gather game statistic.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 63
diff changeset
   790
          if (ui.game.dirs.indexOf(move) < 0) {
96e997158562 Gather game statistic.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 63
diff changeset
   791
            ui.game.setMessage("I don't know how to move!");
96e997158562 Gather game statistic.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 63
diff changeset
   792
            return;
96e997158562 Gather game statistic.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 63
diff changeset
   793
          }
96e997158562 Gather game statistic.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 63
diff changeset
   794
          var updated = board.move[move].call(null, brd);
96e997158562 Gather game statistic.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 63
diff changeset
   795
          if (!updated) {
96e997158562 Gather game statistic.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 63
diff changeset
   796
            ui.game.setMessage("Wrong move!");
96e997158562 Gather game statistic.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 63
diff changeset
   797
            return;
96e997158562 Gather game statistic.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 63
diff changeset
   798
          }
96e997158562 Gather game statistic.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 63
diff changeset
   799
          board.putRandom(brd);
96e997158562 Gather game statistic.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 63
diff changeset
   800
          turn++;
96e997158562 Gather game statistic.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 63
diff changeset
   801
        }
96e997158562 Gather game statistic.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 63
diff changeset
   802
        var tsTo = new Date().getTime();
68
f8d310e3aecf Add time and turn count to statistic data.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 67
diff changeset
   803
        var stat = board.score(brd);
f8d310e3aecf Add time and turn count to statistic data.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 67
diff changeset
   804
        stat.turn = turn;
f8d310e3aecf Add time and turn count to statistic data.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 67
diff changeset
   805
        stat.ts = tsTo - tsFrom;
f8d310e3aecf Add time and turn count to statistic data.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 67
diff changeset
   806
        stats.push(stat);
f8d310e3aecf Add time and turn count to statistic data.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 67
diff changeset
   807
        if (tsTo - tsLimitFrom >= tsLimit)
64
96e997158562 Gather game statistic.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 63
diff changeset
   808
          break;
96e997158562 Gather game statistic.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 63
diff changeset
   809
      }
75
ab74c80beffd Add score, turn and speed statistic selection.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 74
diff changeset
   810
      var scoreChecked = document.getElementById('report-score').checked;
ab74c80beffd Add score, turn and speed statistic selection.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 74
diff changeset
   811
      var turnChecked = document.getElementById('report-turn').checked;
ab74c80beffd Add score, turn and speed statistic selection.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 74
diff changeset
   812
      var speedChecked = document.getElementById('report-speed').checked;
69
cbdfc1072009 Calculate statistic histogram.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 68
diff changeset
   813
      var histo = {};
cbdfc1072009 Calculate statistic histogram.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 68
diff changeset
   814
      for (i = stats.length-1; i >= 0; i--) {
cbdfc1072009 Calculate statistic histogram.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 68
diff changeset
   815
        var stat = stats[i];
cbdfc1072009 Calculate statistic histogram.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 68
diff changeset
   816
        if ( ! histo[stat.max])
cbdfc1072009 Calculate statistic histogram.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 68
diff changeset
   817
          histo[stat.max] = { n: 0, minSpeed: Infinity, meanSpeed: 0, maxSpeed: 0, minTurn: Infinity, meanTurn: 0, maxTurn: 0, minScore: Infinity, meanScore: 0, maxScore: 0 };
cbdfc1072009 Calculate statistic histogram.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 68
diff changeset
   818
        var row = histo[stat.max];
cbdfc1072009 Calculate statistic histogram.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 68
diff changeset
   819
        row.n++;
75
ab74c80beffd Add score, turn and speed statistic selection.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 74
diff changeset
   820
        if (scoreChecked) {
ab74c80beffd Add score, turn and speed statistic selection.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 74
diff changeset
   821
          row.minScore = Math.min(row.minScore, stat.score);
ab74c80beffd Add score, turn and speed statistic selection.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 74
diff changeset
   822
          row.meanScore += stat.score;
ab74c80beffd Add score, turn and speed statistic selection.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 74
diff changeset
   823
          row.maxScore = Math.max(row.maxScore, stat.score);
ab74c80beffd Add score, turn and speed statistic selection.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 74
diff changeset
   824
        }
ab74c80beffd Add score, turn and speed statistic selection.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 74
diff changeset
   825
        if (turnChecked) {
ab74c80beffd Add score, turn and speed statistic selection.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 74
diff changeset
   826
          row.minTurn = Math.min(row.minTurn, stat.turn);
ab74c80beffd Add score, turn and speed statistic selection.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 74
diff changeset
   827
          row.meanTurn += stat.turn;
ab74c80beffd Add score, turn and speed statistic selection.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 74
diff changeset
   828
          row.maxTurn = Math.max(row.maxTurn, stat.turn);
ab74c80beffd Add score, turn and speed statistic selection.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 74
diff changeset
   829
        }
ab74c80beffd Add score, turn and speed statistic selection.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 74
diff changeset
   830
        if (speedChecked) {
ab74c80beffd Add score, turn and speed statistic selection.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 74
diff changeset
   831
          var speed = (stat.turn * 1000.0) / stat.ts;
ab74c80beffd Add score, turn and speed statistic selection.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 74
diff changeset
   832
          row.minSpeed = Math.min(row.minSpeed, speed);
ab74c80beffd Add score, turn and speed statistic selection.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 74
diff changeset
   833
          row.meanSpeed += speed;
ab74c80beffd Add score, turn and speed statistic selection.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 74
diff changeset
   834
          row.maxSpeed = Math.max(row.maxSpeed, speed);
ab74c80beffd Add score, turn and speed statistic selection.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 74
diff changeset
   835
        }
69
cbdfc1072009 Calculate statistic histogram.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 68
diff changeset
   836
      }
cbdfc1072009 Calculate statistic histogram.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 68
diff changeset
   837
      for (var i in histo) {
cbdfc1072009 Calculate statistic histogram.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 68
diff changeset
   838
        var row = histo[i];
cbdfc1072009 Calculate statistic histogram.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 68
diff changeset
   839
        var n = row.n;
75
ab74c80beffd Add score, turn and speed statistic selection.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 74
diff changeset
   840
        if (scoreChecked)
ab74c80beffd Add score, turn and speed statistic selection.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 74
diff changeset
   841
          row.meanScore = row.meanScore / n;
ab74c80beffd Add score, turn and speed statistic selection.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 74
diff changeset
   842
        if (turnChecked)
ab74c80beffd Add score, turn and speed statistic selection.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 74
diff changeset
   843
          row.meanTurn = row.meanTurn / n;
ab74c80beffd Add score, turn and speed statistic selection.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 74
diff changeset
   844
        if (speedChecked)
ab74c80beffd Add score, turn and speed statistic selection.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 74
diff changeset
   845
          row.meanSpeed = row.meanSpeed / n;
69
cbdfc1072009 Calculate statistic histogram.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 68
diff changeset
   846
      }
71
aa0240db128a Show statistic reports.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 70
diff changeset
   847
      var reportDom = document.createElement('div');
aa0240db128a Show statistic reports.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 70
diff changeset
   848
      reportDom.classList.add('report');
aa0240db128a Show statistic reports.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 70
diff changeset
   849
      var h5Dom = document.createElement('h5');
aa0240db128a Show statistic reports.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 70
diff changeset
   850
      h5Dom.appendChild(document.createTextNode(ui.ai.currentName));
aa0240db128a Show statistic reports.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 70
diff changeset
   851
      reportDom.appendChild(h5Dom);
72
0d1e5bbcde82 Fix value ordering. Fix game counting when time limit reached.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 71
diff changeset
   852
      var maxVals = Object.keys(histo).sort(function(a,b){return a - b});
73
a50828ac954d Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 72
diff changeset
   853
      var tbl = [];
71
aa0240db128a Show statistic reports.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 70
diff changeset
   854
      for (var i = maxVals.length-1; i >= 0; i--) {
aa0240db128a Show statistic reports.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 70
diff changeset
   855
        var maxVal = maxVals[i];
aa0240db128a Show statistic reports.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 70
diff changeset
   856
        var row = histo[maxVals[i]];
73
a50828ac954d Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 72
diff changeset
   857
        var perc = parseFloat((100 * row.n / gameCnt).toPrecision(3));
75
ab74c80beffd Add score, turn and speed statistic selection.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 74
diff changeset
   858
        var tblRow = [maxVal, row.n, perc];
ab74c80beffd Add score, turn and speed statistic selection.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 74
diff changeset
   859
        if (scoreChecked) {
ab74c80beffd Add score, turn and speed statistic selection.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 74
diff changeset
   860
          tblRow.push(row.minScore);
ab74c80beffd Add score, turn and speed statistic selection.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 74
diff changeset
   861
          var meanScore = parseFloat(row.meanScore.toPrecision(3));
ab74c80beffd Add score, turn and speed statistic selection.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 74
diff changeset
   862
          tblRow.push(meanScore);
ab74c80beffd Add score, turn and speed statistic selection.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 74
diff changeset
   863
          tblRow.push(row.maxScore);
ab74c80beffd Add score, turn and speed statistic selection.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 74
diff changeset
   864
        }
ab74c80beffd Add score, turn and speed statistic selection.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 74
diff changeset
   865
        if (turnChecked) {
ab74c80beffd Add score, turn and speed statistic selection.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 74
diff changeset
   866
          tblRow.push(row.minTurn);
ab74c80beffd Add score, turn and speed statistic selection.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 74
diff changeset
   867
          tblRow.push(Math.floor(row.meanTurn));
ab74c80beffd Add score, turn and speed statistic selection.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 74
diff changeset
   868
          tblRow.push(row.maxTurn);
ab74c80beffd Add score, turn and speed statistic selection.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 74
diff changeset
   869
        }
ab74c80beffd Add score, turn and speed statistic selection.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 74
diff changeset
   870
        if (speedChecked) {
ab74c80beffd Add score, turn and speed statistic selection.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 74
diff changeset
   871
          tblRow.push(parseFloat(row.minSpeed.toPrecision(3)));
ab74c80beffd Add score, turn and speed statistic selection.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 74
diff changeset
   872
          tblRow.push(parseFloat(row.meanSpeed.toPrecision(3)));
ab74c80beffd Add score, turn and speed statistic selection.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 74
diff changeset
   873
          tblRow.push(parseFloat(row.maxSpeed.toPrecision(3)));
ab74c80beffd Add score, turn and speed statistic selection.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 74
diff changeset
   874
        }
ab74c80beffd Add score, turn and speed statistic selection.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 74
diff changeset
   875
        tbl.push(tblRow);
71
aa0240db128a Show statistic reports.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 70
diff changeset
   876
      }
75
ab74c80beffd Add score, turn and speed statistic selection.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 74
diff changeset
   877
      var tblCols = ['maxVal', 'n', '%'];
ab74c80beffd Add score, turn and speed statistic selection.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 74
diff changeset
   878
      if (scoreChecked) {
ab74c80beffd Add score, turn and speed statistic selection.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 74
diff changeset
   879
        tblCols.push('min score');
ab74c80beffd Add score, turn and speed statistic selection.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 74
diff changeset
   880
        tblCols.push('mean score');
ab74c80beffd Add score, turn and speed statistic selection.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 74
diff changeset
   881
        tblCols.push('max score');
ab74c80beffd Add score, turn and speed statistic selection.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 74
diff changeset
   882
      }
ab74c80beffd Add score, turn and speed statistic selection.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 74
diff changeset
   883
      if (turnChecked) {
ab74c80beffd Add score, turn and speed statistic selection.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 74
diff changeset
   884
        tblCols.push('min turn');
ab74c80beffd Add score, turn and speed statistic selection.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 74
diff changeset
   885
        tblCols.push('mean turn');
ab74c80beffd Add score, turn and speed statistic selection.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 74
diff changeset
   886
        tblCols.push('max turn');
ab74c80beffd Add score, turn and speed statistic selection.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 74
diff changeset
   887
      }
ab74c80beffd Add score, turn and speed statistic selection.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 74
diff changeset
   888
      if (speedChecked) {
ab74c80beffd Add score, turn and speed statistic selection.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 74
diff changeset
   889
        tblCols.push('min speed');
ab74c80beffd Add score, turn and speed statistic selection.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 74
diff changeset
   890
        tblCols.push('mean speed');
ab74c80beffd Add score, turn and speed statistic selection.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 74
diff changeset
   891
        tblCols.push('max speed');
ab74c80beffd Add score, turn and speed statistic selection.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 74
diff changeset
   892
      }
ab74c80beffd Add score, turn and speed statistic selection.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 74
diff changeset
   893
      var tableDom = ui.dom.table(tbl, tblCols, { tableClass: 'report-by-maxval' });
71
aa0240db128a Show statistic reports.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 70
diff changeset
   894
      reportDom.appendChild(tableDom);
aa0240db128a Show statistic reports.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 70
diff changeset
   895
      reportsDom.insertBefore(reportDom, reportsDom.firstChild);
62
a8e4d8a808c1 Add statistic area.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 61
diff changeset
   896
    }
a8e4d8a808c1 Add statistic area.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 61
diff changeset
   897
    var statisticBtn = document.getElementById('statistic');
73
a50828ac954d Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 72
diff changeset
   898
    statisticBtn.addEventListener("click", ui.report.stat, false);
62
a8e4d8a808c1 Add statistic area.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 61
diff changeset
   899
98
eb4f89e2a001 Clean statistics button.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 97
diff changeset
   900
    ui.report.statClean = function() {
eb4f89e2a001 Clean statistics button.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 97
diff changeset
   901
      while (reportsDom.hasChildNodes()) {
eb4f89e2a001 Clean statistics button.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 97
diff changeset
   902
        reportsDom.removeChild(reportsDom.lastChild);
eb4f89e2a001 Clean statistics button.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 97
diff changeset
   903
      }
eb4f89e2a001 Clean statistics button.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 97
diff changeset
   904
      reportsDom.appendChild(ui.dom.clearfix());
eb4f89e2a001 Clean statistics button.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 97
diff changeset
   905
    }
eb4f89e2a001 Clean statistics button.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 97
diff changeset
   906
    var statisticCleanBtn = document.getElementById('statistic-clean');
eb4f89e2a001 Clean statistics button.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 97
diff changeset
   907
    statisticCleanBtn.addEventListener("click", ui.report.statClean, false);
62
a8e4d8a808c1 Add statistic area.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 61
diff changeset
   908
a8e4d8a808c1 Add statistic area.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 61
diff changeset
   909
    ////////////////////////////////////////////////////////////////
a8e4d8a808c1 Add statistic area.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 61
diff changeset
   910
    // Initialise game.
a8e4d8a808c1 Add statistic area.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 61
diff changeset
   911
25
2ef10a49a28f Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 21
diff changeset
   912
    ui.brdEngine = BoardArr2d; // TODO make user selectable
0
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   913
95
804e751a3b7f Save and restore continuation limits.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 94
diff changeset
   914
    if (localStorage.untilScore) {
804e751a3b7f Save and restore continuation limits.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 94
diff changeset
   915
      var scoreLimit = parseInt(localStorage.untilScore);
804e751a3b7f Save and restore continuation limits.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 94
diff changeset
   916
      if (isFinite(scoreLimit) && scoreLimit > 0)
804e751a3b7f Save and restore continuation limits.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 94
diff changeset
   917
        document.getElementById("until-score").value = scoreLimit;
804e751a3b7f Save and restore continuation limits.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 94
diff changeset
   918
    }
804e751a3b7f Save and restore continuation limits.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 94
diff changeset
   919
    if (localStorage.untilMaxVal) {
804e751a3b7f Save and restore continuation limits.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 94
diff changeset
   920
      var maxValLimit = parseInt(localStorage.untilMaxVal);
804e751a3b7f Save and restore continuation limits.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 94
diff changeset
   921
      if (isFinite(maxValLimit) && maxValLimit > 0 && maxValLimit <= 13)
804e751a3b7f Save and restore continuation limits.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 94
diff changeset
   922
        document.getElementById("until-max-value").value = maxValLimit;
804e751a3b7f Save and restore continuation limits.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 94
diff changeset
   923
    }
804e751a3b7f Save and restore continuation limits.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 94
diff changeset
   924
43
8b1577211149 Save val2048 preference.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 42
diff changeset
   925
    if (localStorage.val2048) {
8b1577211149 Save val2048 preference.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 42
diff changeset
   926
      try {
8b1577211149 Save val2048 preference.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 42
diff changeset
   927
        ui.board.val2048Dom.checked = JSON.parse(localStorage.val2048);
8b1577211149 Save val2048 preference.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 42
diff changeset
   928
      } catch (ex) { }
8b1577211149 Save val2048 preference.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 42
diff changeset
   929
    }
41
359719178176 Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 40
diff changeset
   930
    if (localStorage.savedBoard) {
42
716508d5494c Save board between browser sessions.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 41
diff changeset
   931
      try {
45
0bcdec3bdaa8 Rename.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 44
diff changeset
   932
        ui.board.position = JSON.parse(localStorage.savedBoard);
46
4dadaff6eba4 Update/store/restore game turn. Show max value according to value settings.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 45
diff changeset
   933
        if (localStorage.savedTurn)
4dadaff6eba4 Update/store/restore game turn. Show max value according to value settings.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 45
diff changeset
   934
          ui.board.turn = JSON.parse(localStorage.savedTurn);
4dadaff6eba4 Update/store/restore game turn. Show max value according to value settings.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 45
diff changeset
   935
        else
4dadaff6eba4 Update/store/restore game turn. Show max value according to value settings.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 45
diff changeset
   936
          ui.board.turn = 0;
45
0bcdec3bdaa8 Rename.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 44
diff changeset
   937
        ui.board.update(ui.board.position);
46
4dadaff6eba4 Update/store/restore game turn. Show max value according to value settings.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 45
diff changeset
   938
        ui.score.update(ui.board.position, ui.board.turn);
42
716508d5494c Save board between browser sessions.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 41
diff changeset
   939
      } catch (ex) { }
41
359719178176 Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 40
diff changeset
   940
    } else {
359719178176 Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 40
diff changeset
   941
      ui.action.start();
359719178176 Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 40
diff changeset
   942
    }
32
f48151863cdf Start game after page load.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 31
diff changeset
   943
0
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   944
  </script>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   945
  
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   946
</body>
2821cc5e0189 basic implementation
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   947
</html>