author | Oleksandr Gavenko <gavenkoa@gmail.com> |
Wed, 17 Sep 2014 23:05:39 +0300 | |
changeset 72 | 0d1e5bbcde82 |
parent 71 | aa0240db128a |
child 73 | a50828ac954d |
permissions | -rw-r--r-- |
0 | 1 |
<!DOCTYPE html> |
2 |
<html> |
|
3 |
<head> |
|
4 |
<title>2048 AI</title> |
|
5 |
<meta name="viewport" content="width=device-width; initial-scale=1.0"/> |
|
6 |
<meta charset="utf-8"/> |
|
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 | 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 | 17 |
h1, div.area { |
0 | 18 |
text-align: center; |
4 | 19 |
margin: 10px auto; |
0 | 20 |
} |
21 |
#board { |
|
22 |
margin: 10px auto; |
|
23 |
} |
|
24 |
#board td { |
|
25 |
width: 40px; |
|
26 |
height: 40px; |
|
27 |
border: 1px solid red; |
|
28 |
margin: 0; |
|
29 |
text-align: center; |
|
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 | 35 |
background-color: cornsilk; |
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 | 39 |
} |
34 | 40 |
div.move-control button.suggestion { |
41 |
background-color: cyan; |
|
42 |
} |
|
31 | 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 |
} |
71
aa0240db128a
Show statistic reports.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
70
diff
changeset
|
61 |
div.ai > h5, div.report > 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 |
} |
62 | 69 |
button#statistic { |
70 |
display: inline-block; |
|
71 |
float: left; |
|
72 |
margin: 3px 4px; |
|
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 |
} |
62 | 82 |
div.ai > div.option > input.positive, div.settings > div.setting > input { |
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 | 90 |
#test { |
91 |
display: none; |
|
92 |
} |
|
71
aa0240db128a
Show statistic reports.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
70
diff
changeset
|
93 |
table.report-by-maxval { |
aa0240db128a
Show statistic reports.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
70
diff
changeset
|
94 |
border-collapse: collapse; |
aa0240db128a
Show statistic reports.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
70
diff
changeset
|
95 |
margin: 2px; |
aa0240db128a
Show statistic reports.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
70
diff
changeset
|
96 |
} |
aa0240db128a
Show statistic reports.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
70
diff
changeset
|
97 |
table.report-by-maxval > tr > th { |
aa0240db128a
Show statistic reports.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
70
diff
changeset
|
98 |
border: 2px solid green; |
aa0240db128a
Show statistic reports.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
70
diff
changeset
|
99 |
padding: 2px; |
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 > td { |
aa0240db128a
Show statistic reports.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
70
diff
changeset
|
102 |
border: 1px 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 |
} |
0 | 105 |
</style> |
106 |
</head> |
|
107 |
<body> |
|
108 |
||
109 |
<h1>2048</h1> |
|
110 |
||
33
f1bbe12e2c3d
Highlight score area.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
32
diff
changeset
|
111 |
<div id="score-area" class="area"> |
f1bbe12e2c3d
Highlight score area.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
32
diff
changeset
|
112 |
<div class="score-conrol"> |
f1bbe12e2c3d
Highlight score area.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
32
diff
changeset
|
113 |
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
|
114 |
</div> |
f1bbe12e2c3d
Highlight score area.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
32
diff
changeset
|
115 |
</div> |
0 | 116 |
|
4 | 117 |
<div id="message-area" class="area"></div> |
0 | 118 |
|
119 |
<table id="board"> |
|
120 |
<tr> |
|
121 |
<td></td> |
|
122 |
<td></td> |
|
123 |
<td></td> |
|
124 |
<td></td> |
|
125 |
</tr> |
|
126 |
<tr> |
|
127 |
<td></td> |
|
128 |
<td></td> |
|
129 |
<td></td> |
|
130 |
<td></td> |
|
131 |
</tr> |
|
132 |
<tr> |
|
133 |
<td></td> |
|
134 |
<td></td> |
|
135 |
<td></td> |
|
136 |
<td></td> |
|
137 |
</tr> |
|
138 |
<tr> |
|
139 |
<td></td> |
|
140 |
<td></td> |
|
141 |
<td></td> |
|
142 |
<td></td> |
|
143 |
</tr> |
|
144 |
</table> |
|
145 |
||
4 | 146 |
<div id="control-area" class="area"> |
0 | 147 |
<div> |
148 |
<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
|
149 |
<span><input type="checkbox" id="2048" checked> 2048</span> |
30
1d19768ca71d
Highlight AI controls.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
27
diff
changeset
|
150 |
</div> |
1d19768ca71d
Highlight AI controls.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
27
diff
changeset
|
151 |
<div class="ai-control"> |
1d19768ca71d
Highlight AI controls.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
27
diff
changeset
|
152 |
<button id="suggest">Suggest</button> |
0 | 153 |
<button id="step">Step</button> |
154 |
<button id="finish">Finish</button> |
|
155 |
</div> |
|
31 | 156 |
<div class="clearfix"></div> |
157 |
<div class="move-control"> |
|
158 |
<table> |
|
159 |
<tr> |
|
63
251c39718d67
Replace words with arrows to show directions.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
62
diff
changeset
|
160 |
<td></td><td><button id="up">↑</button></td><td></td> |
31 | 161 |
</tr> |
162 |
<tr> |
|
63
251c39718d67
Replace words with arrows to show directions.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
62
diff
changeset
|
163 |
<td><button id="left">←</button></td><td><button id="down">↓</button></td><td><button id="right">→</button></td> |
31 | 164 |
</tr> |
165 |
</table> |
|
12
a9a44cfc3e08
Moves for 2d array board + test toolkit.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
11
diff
changeset
|
166 |
<button id="test">test</button> |
0 | 167 |
</div> |
25
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
168 |
<h1>AIs</h1> |
0 | 169 |
<div> |
35 | 170 |
<div class="ai" id="ai-blind-random"> |
25
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
171 |
<button class="ai">enable</button> |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
172 |
<h5>bling random</h5> |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
173 |
</div> |
36
a18fc2601ce8
Add blind eight random AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
35
diff
changeset
|
174 |
<div class="ai" id="ai-blind-weight-random"> |
a18fc2601ce8
Add blind eight random AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
35
diff
changeset
|
175 |
<button class="ai">enable</button> |
a18fc2601ce8
Add blind eight random AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
35
diff
changeset
|
176 |
<h5>bling weight random</h5> |
a18fc2601ce8
Add blind eight random AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
35
diff
changeset
|
177 |
<div class="option"> |
55
8ba9dc89be87
Automatically extract config from DOM.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
53
diff
changeset
|
178 |
<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
|
179 |
</div> |
a18fc2601ce8
Add blind eight random AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
35
diff
changeset
|
180 |
<div class="option"> |
55
8ba9dc89be87
Automatically extract config from DOM.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
53
diff
changeset
|
181 |
<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
|
182 |
</div> |
a18fc2601ce8
Add blind eight random AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
35
diff
changeset
|
183 |
<div class="option"> |
55
8ba9dc89be87
Automatically extract config from DOM.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
53
diff
changeset
|
184 |
<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
|
185 |
</div> |
a18fc2601ce8
Add blind eight random AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
35
diff
changeset
|
186 |
<div class="option"> |
55
8ba9dc89be87
Automatically extract config from DOM.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
53
diff
changeset
|
187 |
<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
|
188 |
</div> |
a18fc2601ce8
Add blind eight random AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
35
diff
changeset
|
189 |
<div class="clearfix"></div> |
a18fc2601ce8
Add blind eight random AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
35
diff
changeset
|
190 |
</div> |
27
8f96d09a4d94
Add Blind cycle AI. Add example of setting passing.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
26
diff
changeset
|
191 |
<div class="ai" id="ai-blind-cycle"> |
25
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
192 |
<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
|
193 |
<h5>blind cycle</h5> |
8f96d09a4d94
Add Blind cycle AI. Add example of setting passing.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
26
diff
changeset
|
194 |
<div class="option"> |
8f96d09a4d94
Add Blind cycle AI. Add example of setting passing.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
26
diff
changeset
|
195 |
<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
|
196 |
</div> |
8f96d09a4d94
Add Blind cycle AI. Add example of setting passing.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
26
diff
changeset
|
197 |
<div class="option"> |
8f96d09a4d94
Add Blind cycle AI. Add example of setting passing.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
26
diff
changeset
|
198 |
<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
|
199 |
</div> |
8f96d09a4d94
Add Blind cycle AI. Add example of setting passing.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
26
diff
changeset
|
200 |
<div class="clearfix"></div> |
25
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
201 |
</div> |
53
ee53cd2cb69a
Join all one step deep AIs into one with linear weight function.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
52
diff
changeset
|
202 |
<div class="ai" id="ai-one-step-deep"> |
25
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
203 |
<button class="ai">enable</button> |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
204 |
<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
|
205 |
<div class="option"> |
55
8ba9dc89be87
Automatically extract config from DOM.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
53
diff
changeset
|
206 |
<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
|
207 |
</div> |
ee53cd2cb69a
Join all one step deep AIs into one with linear weight function.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
52
diff
changeset
|
208 |
<div class="option"> |
55
8ba9dc89be87
Automatically extract config from DOM.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
53
diff
changeset
|
209 |
<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
|
210 |
</div> |
ee53cd2cb69a
Join all one step deep AIs into one with linear weight function.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
52
diff
changeset
|
211 |
<div class="option"> |
55
8ba9dc89be87
Automatically extract config from DOM.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
53
diff
changeset
|
212 |
<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
|
213 |
</div> |
ee53cd2cb69a
Join all one step deep AIs into one with linear weight function.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
52
diff
changeset
|
214 |
<div class="option"> |
55
8ba9dc89be87
Automatically extract config from DOM.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
53
diff
changeset
|
215 |
<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
|
216 |
</div> |
ee53cd2cb69a
Join all one step deep AIs into one with linear weight function.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
52
diff
changeset
|
217 |
<div class="option"> |
55
8ba9dc89be87
Automatically extract config from DOM.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
53
diff
changeset
|
218 |
<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
|
219 |
</div> |
ee53cd2cb69a
Join all one step deep AIs into one with linear weight function.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
52
diff
changeset
|
220 |
<div class="clearfix"></div> |
25
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
221 |
</div> |
26 | 222 |
<div class="ai" id="ai-deep-max-score"> |
25
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
223 |
<button class="ai">enable</button> |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
224 |
<h5>deep merges without simulation make max score</h5> |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
225 |
</div> |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
226 |
<div class="ai" id="ai-deep-max-score-corner"> |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
227 |
<button class="ai">enable</button> |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
228 |
<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
|
229 |
</div> |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
230 |
<div class="ai" id="ai-expectimax"> |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
231 |
<button class="ai">enable</button> |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
232 |
<h5>expectimax</h5> |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
233 |
</div> |
0 | 234 |
</div> |
235 |
</div> |
|
236 |
||
4 | 237 |
<div id="report-area" class="area"> |
238 |
<h1>Reports</h1> |
|
62 | 239 |
<div class="settings"> |
70
01076b56feae
Fix histogram calculation.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
69
diff
changeset
|
240 |
<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
|
241 |
<div class="setting">limit to <input type="text" id="stat-time-limit" value="10"> sec</div> |
62 | 242 |
<button id="statistic">Start</button> |
243 |
<div class="clearfix"></div> |
|
244 |
</div> |
|
71
aa0240db128a
Show statistic reports.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
70
diff
changeset
|
245 |
<div id="reports"> |
aa0240db128a
Show statistic reports.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
70
diff
changeset
|
246 |
<div class="clearfix"></div> |
4 | 247 |
</div> |
248 |
</div> |
|
249 |
||
0 | 250 |
<script> |
251 |
"use strict"; |
|
252 |
||
253 |
var boardDom = document.getElementById("board"); |
|
254 |
var ui = {}; |
|
255 |
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
|
256 |
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
|
257 |
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
|
258 |
event.preventDefault(); event.stopPropagation(); |
45 | 259 |
ui.board.update(ui.board.position); |
43
8b1577211149
Save val2048 preference.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
42
diff
changeset
|
260 |
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
|
261 |
event.target.blur(); |
65
b8028f3e30fb
Use preventDefault()/stopPropagation(). Add false to addEventListener().
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
64
diff
changeset
|
262 |
}, 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
|
263 |
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
|
264 |
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
|
265 |
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
|
266 |
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
|
267 |
} |
15
0fa97e430a9e
ui.board.set care about 'val' argument.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
14
diff
changeset
|
268 |
/* 'val' typesafe. */ |
0 | 269 |
ui.board.set = function(i, j, val) { |
16 | 270 |
if (typeof val !== 'number') |
15
0fa97e430a9e
ui.board.set care about 'val' argument.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
14
diff
changeset
|
271 |
val = 0; |
13
b3bfa9d1b537
Add color to board depending on values..
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
12
diff
changeset
|
272 |
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
|
273 |
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
|
274 |
dom.innerHTML = ui.board.val2048(val); |
15
0fa97e430a9e
ui.board.set care about 'val' argument.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
14
diff
changeset
|
275 |
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
|
276 |
} else { |
0fa97e430a9e
ui.board.set care about 'val' argument.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
14
diff
changeset
|
277 |
dom.innerHTML = ''; |
0fa97e430a9e
ui.board.set care about 'val' argument.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
14
diff
changeset
|
278 |
var rgb = 'white'; |
0fa97e430a9e
ui.board.set care about 'val' argument.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
14
diff
changeset
|
279 |
} |
13
b3bfa9d1b537
Add color to board depending on values..
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
12
diff
changeset
|
280 |
dom.style.backgroundColor = rgb; |
0 | 281 |
} |
282 |
ui.board.update = function(brd) { |
|
283 |
for (var i = 0; i < 4; i++) { |
|
284 |
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
|
285 |
ui.board.set(i, j, brd[i][j]); |
0 | 286 |
} |
287 |
} |
|
288 |
} |
|
17 | 289 |
ui.board.handleEdit = function(tdDom, i, j) { |
290 |
if (!tdDom.firstChild || tdDom.firstChild.nodeType === Node.TEXT_NODE) { |
|
291 |
if (tdDom.firstChild) |
|
292 |
tdDom.removeChild(tdDom.firstChild); |
|
293 |
var inputDom = document.createElement('input'); |
|
45 | 294 |
inputDom.value = ui.board.position[i][j]; |
17 | 295 |
inputDom.style.width = '1em'; |
296 |
tdDom.appendChild(inputDom); |
|
297 |
inputDom.focus(); |
|
298 |
inputDom.select(); |
|
299 |
inputDom.addEventListener("blur", function(tdDom, i, j) { |
|
300 |
return function(event) { |
|
65
b8028f3e30fb
Use preventDefault()/stopPropagation(). Add false to addEventListener().
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
64
diff
changeset
|
301 |
event.preventDefault(); event.stopPropagation(); |
17 | 302 |
ui.board.handleEdit(tdDom, i, j); |
303 |
} |
|
65
b8028f3e30fb
Use preventDefault()/stopPropagation(). Add false to addEventListener().
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
64
diff
changeset
|
304 |
}(tdDom, i, j), false); |
17 | 305 |
} else { |
306 |
var inputDom = tdDom.firstChild; |
|
307 |
var val = parseInt(inputDom.value); |
|
308 |
if (0 <= val && val < 15) { |
|
45 | 309 |
ui.board.position[i][j] = val; |
17 | 310 |
ui.board.set(i, j, val); |
311 |
} else { |
|
45 | 312 |
ui.board.set(i, j, ui.board.position[i][j]); |
17 | 313 |
} |
314 |
} |
|
315 |
} |
|
316 |
ui.board.enableEdit = function() { |
|
317 |
var trDoms = boardDom.querySelectorAll("tr"); |
|
318 |
for (var i = 0; i < 4; i++) { |
|
319 |
var tdDoms = trDoms[i].querySelectorAll("td"); |
|
320 |
for (var j = 0; j < 4; j++) { |
|
321 |
var tdDom = tdDoms[j]; |
|
322 |
tdDom.addEventListener("click", function(tdDom, i, j) { |
|
323 |
return function(event) { |
|
65
b8028f3e30fb
Use preventDefault()/stopPropagation(). Add false to addEventListener().
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
64
diff
changeset
|
324 |
event.preventDefault(); event.stopPropagation(); |
17 | 325 |
ui.board.handleEdit(tdDom, i, j); |
326 |
} |
|
65
b8028f3e30fb
Use preventDefault()/stopPropagation(). Add false to addEventListener().
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
64
diff
changeset
|
327 |
}(tdDom, i, j), false); |
17 | 328 |
tdDom.addEventListener("keyup", function(tdDom, i, j) { |
329 |
return function(event) { |
|
65
b8028f3e30fb
Use preventDefault()/stopPropagation(). Add false to addEventListener().
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
64
diff
changeset
|
330 |
event.preventDefault(); event.stopPropagation(); |
17 | 331 |
if (event.keyCode === 13) |
332 |
ui.board.handleEdit(tdDom, i, j); |
|
333 |
} |
|
65
b8028f3e30fb
Use preventDefault()/stopPropagation(). Add false to addEventListener().
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
64
diff
changeset
|
334 |
}(tdDom, i, j), false); |
17 | 335 |
} |
336 |
} |
|
337 |
} |
|
338 |
ui.board.enableEdit(); |
|
339 |
||
41
359719178176
Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
40
diff
changeset
|
340 |
//////////////////////////////////////////////////////////////// |
359719178176
Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
40
diff
changeset
|
341 |
// Score statistic. |
359719178176
Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
40
diff
changeset
|
342 |
|
0 | 343 |
ui.score = {}; |
344 |
var scoreDom = document.getElementById("score"); |
|
345 |
var maxDom = document.getElementById("max"); |
|
1 | 346 |
var speedDom = document.getElementById("speed"); |
347 |
var turnDom = document.getElementById("turn"); |
|
0 | 348 |
ui.score.clear = function(brd) { |
349 |
scoreDom.innerHTML = '0'; |
|
350 |
maxDom.innerHTML = '0'; |
|
1 | 351 |
speedDom.innerHTML = '0'; |
352 |
turnDom.innerHTML = '0'; |
|
0 | 353 |
} |
46
4dadaff6eba4
Update/store/restore game turn. Show max value according to value settings.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
45
diff
changeset
|
354 |
ui.score.update = function(brd, turn) { |
0 | 355 |
var score = board.score(brd); |
356 |
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
|
357 |
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
|
358 |
turnDom.innerHTML = '' + turn; |
0 | 359 |
} |
1 | 360 |
ui.score.speed = function(speed, turn) { |
361 |
speedDom.innerHTML = '' + speed; |
|
362 |
turnDom.innerHTML = '' + turn; |
|
363 |
} |
|
0 | 364 |
|
39
a75cc5aa5d19
Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
38
diff
changeset
|
365 |
//////////////////////////////////////////////////////////////// |
41
359719178176
Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
40
diff
changeset
|
366 |
// Suggestion UI. |
359719178176
Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
40
diff
changeset
|
367 |
|
359719178176
Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
40
diff
changeset
|
368 |
ui.suggestion = {}; |
359719178176
Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
40
diff
changeset
|
369 |
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
|
370 |
ui.suggestion.clear = function() { |
359719178176
Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
40
diff
changeset
|
371 |
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
|
372 |
ui.suggestion.btns[i].classList.remove('suggestion'); |
359719178176
Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
40
diff
changeset
|
373 |
} |
359719178176
Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
40
diff
changeset
|
374 |
ui.suggestion.set = function(dir) { |
359719178176
Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
40
diff
changeset
|
375 |
document.getElementById(dir).classList.add('suggestion'); |
359719178176
Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
40
diff
changeset
|
376 |
} |
359719178176
Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
40
diff
changeset
|
377 |
|
359719178176
Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
40
diff
changeset
|
378 |
//////////////////////////////////////////////////////////////// |
39
a75cc5aa5d19
Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
38
diff
changeset
|
379 |
// Game flow. |
a75cc5aa5d19
Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
38
diff
changeset
|
380 |
|
40 | 381 |
ui.game = {}; |
382 |
ui.game.dirs = ["up", "right", "down", "left"]; |
|
39
a75cc5aa5d19
Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
38
diff
changeset
|
383 |
|
a75cc5aa5d19
Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
38
diff
changeset
|
384 |
var messageDom = document.getElementById("message-area"); |
a75cc5aa5d19
Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
38
diff
changeset
|
385 |
ui.game.clearMessage = function() { |
a75cc5aa5d19
Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
38
diff
changeset
|
386 |
messageDom.innerHTML = ""; |
a75cc5aa5d19
Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
38
diff
changeset
|
387 |
} |
a75cc5aa5d19
Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
38
diff
changeset
|
388 |
ui.game.setMessage = function(msg) { |
a75cc5aa5d19
Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
38
diff
changeset
|
389 |
messageDom.innerHTML = msg; |
a75cc5aa5d19
Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
38
diff
changeset
|
390 |
} |
a75cc5aa5d19
Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
38
diff
changeset
|
391 |
|
a75cc5aa5d19
Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
38
diff
changeset
|
392 |
ui.game.checkGameOver = function() { |
45 | 393 |
if (board.gameOver(ui.board.position)) { |
39
a75cc5aa5d19
Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
38
diff
changeset
|
394 |
ui.game.setMessage("Game over!"); |
a75cc5aa5d19
Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
38
diff
changeset
|
395 |
return true; |
a75cc5aa5d19
Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
38
diff
changeset
|
396 |
} else { |
a75cc5aa5d19
Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
38
diff
changeset
|
397 |
return false; |
a75cc5aa5d19
Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
38
diff
changeset
|
398 |
} |
a75cc5aa5d19
Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
38
diff
changeset
|
399 |
} |
a75cc5aa5d19
Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
38
diff
changeset
|
400 |
ui.game.checkMoveValid = function(move) { |
a75cc5aa5d19
Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
38
diff
changeset
|
401 |
var tmpBrd = board.create(); |
45 | 402 |
board.copy(ui.board.position, tmpBrd); |
40 | 403 |
if (ui.game.dirs.indexOf(move) === -1) { |
39
a75cc5aa5d19
Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
38
diff
changeset
|
404 |
ui.game.setMessage("AI can't find move!"); |
a75cc5aa5d19
Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
38
diff
changeset
|
405 |
return false; |
a75cc5aa5d19
Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
38
diff
changeset
|
406 |
} |
a75cc5aa5d19
Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
38
diff
changeset
|
407 |
if ( ! board.move[move].call(null, tmpBrd)) { |
a75cc5aa5d19
Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
38
diff
changeset
|
408 |
ui.game.setMessage("AI move "+move+" is ivalid!"); |
a75cc5aa5d19
Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
38
diff
changeset
|
409 |
return false; |
a75cc5aa5d19
Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
38
diff
changeset
|
410 |
} |
a75cc5aa5d19
Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
38
diff
changeset
|
411 |
return true; |
a75cc5aa5d19
Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
38
diff
changeset
|
412 |
} |
a75cc5aa5d19
Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
38
diff
changeset
|
413 |
|
41
359719178176
Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
40
diff
changeset
|
414 |
ui.game.beginStep = function() { |
359719178176
Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
40
diff
changeset
|
415 |
ui.suggestion.clear(); |
359719178176
Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
40
diff
changeset
|
416 |
ui.game.clearMessage(); |
359719178176
Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
40
diff
changeset
|
417 |
if (ui.game.checkGameOver()) |
359719178176
Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
40
diff
changeset
|
418 |
return false; |
359719178176
Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
40
diff
changeset
|
419 |
return true; |
359719178176
Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
40
diff
changeset
|
420 |
} |
359719178176
Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
40
diff
changeset
|
421 |
ui.game.finishStep = function() { |
45 | 422 |
board.putRandom(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
|
423 |
ui.board.turn++; |
45 | 424 |
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
|
425 |
ui.score.update(ui.board.position, ui.board.turn); |
45 | 426 |
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
|
427 |
localStorage.savedTurn = ui.board.turn; |
41
359719178176
Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
40
diff
changeset
|
428 |
} |
359719178176
Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
40
diff
changeset
|
429 |
|
39
a75cc5aa5d19
Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
38
diff
changeset
|
430 |
//////////////////////////////////////////////////////////////// |
a75cc5aa5d19
Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
38
diff
changeset
|
431 |
// Actions. |
a75cc5aa5d19
Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
38
diff
changeset
|
432 |
|
30
1d19768ca71d
Highlight AI controls.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
27
diff
changeset
|
433 |
ui.action = {}; |
1d19768ca71d
Highlight AI controls.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
27
diff
changeset
|
434 |
|
1d19768ca71d
Highlight AI controls.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
27
diff
changeset
|
435 |
ui.action.start = function() { |
0 | 436 |
ui.score.clear(); |
41
359719178176
Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
40
diff
changeset
|
437 |
ui.suggestion.clear(); |
39
a75cc5aa5d19
Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
38
diff
changeset
|
438 |
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
|
439 |
ui.board.turn = 0; |
45 | 440 |
ui.board.position = board.create(); |
441 |
board.putRandom(ui.board.position); |
|
442 |
ui.board.update(ui.board.position); |
|
0 | 443 |
} |
65
b8028f3e30fb
Use preventDefault()/stopPropagation(). Add false to addEventListener().
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
64
diff
changeset
|
444 |
document.getElementById("start").addEventListener("click", ui.action.start, false); |
0 | 445 |
|
40 | 446 |
for (var i = 0; i < ui.game.dirs.length; i++) { |
447 |
var dir = ui.game.dirs[i]; |
|
448 |
ui.action[dir] = function(dir) { |
|
65
b8028f3e30fb
Use preventDefault()/stopPropagation(). Add false to addEventListener().
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
64
diff
changeset
|
449 |
return function(event) { |
b8028f3e30fb
Use preventDefault()/stopPropagation(). Add false to addEventListener().
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
64
diff
changeset
|
450 |
event.preventDefault(); event.stopPropagation(); |
40 | 451 |
if (ui.game.checkGameOver()) |
452 |
return; |
|
41
359719178176
Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
40
diff
changeset
|
453 |
ui.suggestion.clear(); |
45 | 454 |
var updated = board.move[dir](ui.board.position); |
40 | 455 |
if (updated) { |
41
359719178176
Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
40
diff
changeset
|
456 |
ui.game.finishStep(); |
40 | 457 |
ui.ai.current && ui.ai.current.cleanup(); |
458 |
} |
|
459 |
} |
|
460 |
} (dir); |
|
65
b8028f3e30fb
Use preventDefault()/stopPropagation(). Add false to addEventListener().
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
64
diff
changeset
|
461 |
document.getElementById(dir).addEventListener("click", ui.action[dir], false); |
0 | 462 |
} |
463 |
||
464 |
document.body.addEventListener("keydown", function(event) { |
|
37
fe75d168a605
Ignore board movement when user edit input.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
36
diff
changeset
|
465 |
if (document.activeElement.value) |
44
518ab579d24b
Prevent from blocking Firefox key binding.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
43
diff
changeset
|
466 |
return; |
52
e4e21d2fcbe7
Ignore moves if shift/alt/control key pressed.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
46
diff
changeset
|
467 |
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
|
468 |
return; |
0 | 469 |
var key = event.keyCode || event.which; |
44
518ab579d24b
Prevent from blocking Firefox key binding.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
43
diff
changeset
|
470 |
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
|
471 |
if (key in keyMoves) { |
65
b8028f3e30fb
Use preventDefault()/stopPropagation(). Add false to addEventListener().
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
64
diff
changeset
|
472 |
ui.action[keyMoves[key]](event); |
0 | 473 |
} |
65
b8028f3e30fb
Use preventDefault()/stopPropagation(). Add false to addEventListener().
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
64
diff
changeset
|
474 |
}, false); |
12
a9a44cfc3e08
Moves for 2d array board + test toolkit.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
11
diff
changeset
|
475 |
|
a9a44cfc3e08
Moves for 2d array board + test toolkit.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
11
diff
changeset
|
476 |
document.getElementById("test").addEventListener("click", function() { |
65
b8028f3e30fb
Use preventDefault()/stopPropagation(). Add false to addEventListener().
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
64
diff
changeset
|
477 |
event.preventDefault(); event.stopPropagation(); |
12
a9a44cfc3e08
Moves for 2d array board + test toolkit.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
11
diff
changeset
|
478 |
board.move.replaceByBoardJS(ui.brdEngine); |
65
b8028f3e30fb
Use preventDefault()/stopPropagation(). Add false to addEventListener().
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
64
diff
changeset
|
479 |
}, false); |
34 | 480 |
|
65
b8028f3e30fb
Use preventDefault()/stopPropagation(). Add false to addEventListener().
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
64
diff
changeset
|
481 |
ui.action.suggest = function(event) { |
b8028f3e30fb
Use preventDefault()/stopPropagation(). Add false to addEventListener().
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
64
diff
changeset
|
482 |
event.preventDefault(); event.stopPropagation(); |
41
359719178176
Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
40
diff
changeset
|
483 |
if ( ! ui.ai.current) { |
359719178176
Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
40
diff
changeset
|
484 |
ui.game.setMessage('Select AI!'); |
359719178176
Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
40
diff
changeset
|
485 |
return; |
359719178176
Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
40
diff
changeset
|
486 |
} |
359719178176
Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
40
diff
changeset
|
487 |
if ( ! ui.game.beginStep()) |
0 | 488 |
return; |
489 |
var tmpBrd = board.create(); |
|
45 | 490 |
board.copy(ui.board.position, tmpBrd); |
25
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
491 |
var move = ui.ai.current.analyse(tmpBrd); |
34 | 492 |
ui.ai.current.cleanup(); |
39
a75cc5aa5d19
Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
38
diff
changeset
|
493 |
if ( ! ui.game.checkMoveValid(move)) |
34 | 494 |
return; |
41
359719178176
Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
40
diff
changeset
|
495 |
ui.suggestion.set(move); |
34 | 496 |
} |
65
b8028f3e30fb
Use preventDefault()/stopPropagation(). Add false to addEventListener().
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
64
diff
changeset
|
497 |
document.getElementById("suggest").addEventListener("click", ui.action.suggest, false); |
34 | 498 |
ui.action.step = function() { |
41
359719178176
Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
40
diff
changeset
|
499 |
if ( ! ui.ai.current) { |
359719178176
Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
40
diff
changeset
|
500 |
ui.game.setMessage('Select AI!'); |
359719178176
Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
40
diff
changeset
|
501 |
return; |
359719178176
Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
40
diff
changeset
|
502 |
} |
359719178176
Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
40
diff
changeset
|
503 |
if ( ! ui.game.beginStep()) |
0 | 504 |
return; |
34 | 505 |
var tmpBrd = board.create(); |
45 | 506 |
board.copy(ui.board.position, tmpBrd); |
34 | 507 |
var move = ui.ai.current.analyse(tmpBrd); |
508 |
ui.ai.current.cleanup(); |
|
39
a75cc5aa5d19
Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
38
diff
changeset
|
509 |
if ( ! ui.game.checkMoveValid(move)) |
34 | 510 |
return; |
45 | 511 |
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
|
512 |
ui.game.finishStep(); |
0 | 513 |
} |
65
b8028f3e30fb
Use preventDefault()/stopPropagation(). Add false to addEventListener().
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
64
diff
changeset
|
514 |
document.getElementById("step").addEventListener("click", ui.action.step, false); |
0 | 515 |
|
30
1d19768ca71d
Highlight AI controls.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
27
diff
changeset
|
516 |
ui.action.finish = function() { |
41
359719178176
Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
40
diff
changeset
|
517 |
if ( ! ui.ai.current) { |
359719178176
Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
40
diff
changeset
|
518 |
ui.game.setMessage('Select AI!'); |
359719178176
Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
40
diff
changeset
|
519 |
return; |
359719178176
Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
40
diff
changeset
|
520 |
} |
359719178176
Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
40
diff
changeset
|
521 |
ui.game.beginStep(); |
1 | 522 |
var step = 0; |
66
c62a1051cef7
Move object creation out of loop.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
65
diff
changeset
|
523 |
var safeBdr = board.create(); |
1 | 524 |
var tsFrom = new Date().getTime(); |
45 | 525 |
while (!board.gameOver(ui.board.position)) { |
66
c62a1051cef7
Move object creation out of loop.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
65
diff
changeset
|
526 |
board.copy(ui.board.position, safeBdr); |
c62a1051cef7
Move object creation out of loop.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
65
diff
changeset
|
527 |
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
|
528 |
if (typeof move === 'undefined') { |
39
a75cc5aa5d19
Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
38
diff
changeset
|
529 |
ui.game.setMessage("I don't know how to move!"); |
0 | 530 |
return; |
531 |
} |
|
45 | 532 |
var updated = board.move[move].call(null, ui.board.position); |
0 | 533 |
if (updated) { |
45 | 534 |
board.putRandom(ui.board.position); |
0 | 535 |
} else { |
41
359719178176
Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
40
diff
changeset
|
536 |
ui.game.finishStep(); |
39
a75cc5aa5d19
Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
38
diff
changeset
|
537 |
ui.game.setMessage("Wrong move!"); |
0 | 538 |
return; |
539 |
} |
|
1 | 540 |
step++; |
0 | 541 |
} |
1 | 542 |
var tsTo = new Date().getTime(); |
41
359719178176
Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
40
diff
changeset
|
543 |
ui.game.finishStep(); |
1 | 544 |
ui.score.speed(step*1000.0/(tsTo-tsFrom), step); |
39
a75cc5aa5d19
Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
38
diff
changeset
|
545 |
ui.game.setMessage("Game over!"); |
30
1d19768ca71d
Highlight AI controls.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
27
diff
changeset
|
546 |
ui.ai.current && ui.ai.current.cleanup(); |
0 | 547 |
} |
65
b8028f3e30fb
Use preventDefault()/stopPropagation(). Add false to addEventListener().
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
64
diff
changeset
|
548 |
document.getElementById("finish").addEventListener("click", ui.action.finish, false); |
0 | 549 |
|
10
70ece7f758a0
Move rule and AI engine to separate files.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
7
diff
changeset
|
550 |
//////////////////////////////////////////////////////////////// |
70ece7f758a0
Move rule and AI engine to separate files.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
7
diff
changeset
|
551 |
// Register AIs. |
0 | 552 |
|
25
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
553 |
ui.ai = {}; |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
554 |
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
|
555 |
ui.ai.parseCfg = function(aiDom, cfg) { |
55
8ba9dc89be87
Automatically extract config from DOM.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
53
diff
changeset
|
556 |
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
|
557 |
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
|
558 |
var val = parseFloat(optDoms[i].value); |
55
8ba9dc89be87
Automatically extract config from DOM.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
53
diff
changeset
|
559 |
if (val === NaN || val < 0) { |
8ba9dc89be87
Automatically extract config from DOM.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
53
diff
changeset
|
560 |
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
|
561 |
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
|
562 |
continue; |
ee53cd2cb69a
Join all one step deep AIs into one with linear weight function.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
52
diff
changeset
|
563 |
} |
ee53cd2cb69a
Join all one step deep AIs into one with linear weight function.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
52
diff
changeset
|
564 |
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
|
565 |
} |
ee53cd2cb69a
Join all one step deep AIs into one with linear weight function.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
52
diff
changeset
|
566 |
} |
55
8ba9dc89be87
Automatically extract config from DOM.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
53
diff
changeset
|
567 |
|
25
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
568 |
ui.ai.algList = { |
35 | 569 |
"ai-blind-random": function() { |
59 | 570 |
return new ai.BlindRandom(ui.brdEngine); |
25
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
571 |
}, |
36
a18fc2601ce8
Add blind eight random AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
35
diff
changeset
|
572 |
"ai-blind-weight-random": function(aiDom) { |
a18fc2601ce8
Add blind eight random AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
35
diff
changeset
|
573 |
var cfg = {}; |
55
8ba9dc89be87
Automatically extract config from DOM.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
53
diff
changeset
|
574 |
ui.ai.parseCfg(aiDom, cfg); |
59 | 575 |
return new ai.BlindWeightRandom(ui.brdEngine, cfg); |
36
a18fc2601ce8
Add blind eight random AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
35
diff
changeset
|
576 |
}, |
27
8f96d09a4d94
Add Blind cycle AI. Add example of setting passing.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
26
diff
changeset
|
577 |
"ai-blind-cycle": function(aiDom) { |
8f96d09a4d94
Add Blind cycle AI. Add example of setting passing.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
26
diff
changeset
|
578 |
var cfg = {}; |
8f96d09a4d94
Add Blind cycle AI. Add example of setting passing.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
26
diff
changeset
|
579 |
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
|
580 |
cfg.whilePossible = aiDom.querySelectorAll("input[name='whilePossible']")[0].checked; |
59 | 581 |
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
|
582 |
}, |
53
ee53cd2cb69a
Join all one step deep AIs into one with linear weight function.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
52
diff
changeset
|
583 |
"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
|
584 |
var cfg = {}; |
ee53cd2cb69a
Join all one step deep AIs into one with linear weight function.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
52
diff
changeset
|
585 |
ui.ai.parseCfg(aiDom, cfg); |
59 | 586 |
return new ai.OneStepDeep(ui.brdEngine, cfg); |
25
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
587 |
}, |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
588 |
"ai-deep-max-score": function() { |
59 | 589 |
return new ai.DeepMaxScore(ui.brdEngine); |
25
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
590 |
}, |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
591 |
"ai-deep-max-score-corner": function() { |
59 | 592 |
return new ai.DeepMaxScoreCorner(ui.brdEngine); |
25
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
593 |
}, |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
594 |
"ai-expectimax": function() { |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
595 |
return new ai.expectimax(ui.brdEngine); |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
596 |
}, |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
597 |
// "": function() { |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
598 |
// return new ai.(ui.brdEngine); |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
599 |
// }, |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
600 |
}; |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
601 |
ui.ai.domList = document.querySelectorAll('div.ai'); |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
602 |
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
|
603 |
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
|
604 |
ui.ai.enable(event.target.parentNode); |
65
b8028f3e30fb
Use preventDefault()/stopPropagation(). Add false to addEventListener().
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
64
diff
changeset
|
605 |
}, false); |
25
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
606 |
} |
0 | 607 |
|
25
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
608 |
ui.ai.moveToTop = function(aiDom) { |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
609 |
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
|
610 |
ui.ai.domList[i].classList.remove('enabled'); |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
611 |
} |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
612 |
var rootDom = aiDom.parentNode; |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
613 |
rootDom.removeChild(aiDom); |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
614 |
rootDom.insertBefore(aiDom, rootDom.firstChild); |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
615 |
aiDom.classList.add('enabled'); |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
616 |
} |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
617 |
ui.ai.enable = function(aiDom) { |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
618 |
if (ui.ai.current) |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
619 |
ui.ai.current.cleanup(); |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
620 |
ui.ai.moveToTop(aiDom); |
27
8f96d09a4d94
Add Blind cycle AI. Add example of setting passing.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
26
diff
changeset
|
621 |
ui.ai.current = ui.ai.algList[aiDom.id](aiDom); |
71
aa0240db128a
Show statistic reports.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
70
diff
changeset
|
622 |
ui.ai.currentName = aiDom.id; |
25
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
623 |
} |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
624 |
|
62 | 625 |
|
626 |
//////////////////////////////////////////////////////////////// |
|
627 |
// Reports and statistic. |
|
628 |
||
71
aa0240db128a
Show statistic reports.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
70
diff
changeset
|
629 |
var reportsDom = document.getElementById('reports'); |
aa0240db128a
Show statistic reports.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
70
diff
changeset
|
630 |
|
62 | 631 |
function statistic() { |
68
f8d310e3aecf
Add time and turn count to statistic data.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
67
diff
changeset
|
632 |
var stats = []; |
64
96e997158562
Gather game statistic.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
63
diff
changeset
|
633 |
var cnt = parseInt(document.getElementById('stat-count-limit').value); |
96e997158562
Gather game statistic.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
63
diff
changeset
|
634 |
if (isNaN(cnt) || !isFinite(cnt) || cnt < 1) |
96e997158562
Gather game statistic.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
63
diff
changeset
|
635 |
cnt = 100; |
96e997158562
Gather game statistic.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
63
diff
changeset
|
636 |
var tsLimit = parseFloat(document.getElementById('stat-count-limit').value); |
96e997158562
Gather game statistic.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
63
diff
changeset
|
637 |
if (isNaN(tsLimit) || !isFinite(tsLimit) || tsLimit < 1 || tsLimit > 60) |
96e997158562
Gather game statistic.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
63
diff
changeset
|
638 |
tsLimit = 1000 * 10; |
96e997158562
Gather game statistic.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
63
diff
changeset
|
639 |
else |
96e997158562
Gather game statistic.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
63
diff
changeset
|
640 |
tsLimit = 1000 * tsLimit; |
66
c62a1051cef7
Move object creation out of loop.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
65
diff
changeset
|
641 |
var safeBdr = board.create(); |
68
f8d310e3aecf
Add time and turn count to statistic data.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
67
diff
changeset
|
642 |
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
|
643 |
for (var gameCnt = 0; gameCnt++ < cnt; void(0)) { |
64
96e997158562
Gather game statistic.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
63
diff
changeset
|
644 |
var turn = 0; |
96e997158562
Gather game statistic.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
63
diff
changeset
|
645 |
var brd = board.create(); |
96e997158562
Gather game statistic.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
63
diff
changeset
|
646 |
board.putRandom(brd); |
68
f8d310e3aecf
Add time and turn count to statistic data.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
67
diff
changeset
|
647 |
var tsFrom = new Date().getTime(); |
64
96e997158562
Gather game statistic.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
63
diff
changeset
|
648 |
while (!board.gameOver(brd)) { |
66
c62a1051cef7
Move object creation out of loop.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
65
diff
changeset
|
649 |
board.copy(brd, safeBdr); |
c62a1051cef7
Move object creation out of loop.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
65
diff
changeset
|
650 |
var move = ui.ai.current.analyse(safeBdr); |
64
96e997158562
Gather game statistic.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
63
diff
changeset
|
651 |
if (ui.game.dirs.indexOf(move) < 0) { |
96e997158562
Gather game statistic.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
63
diff
changeset
|
652 |
ui.game.setMessage("I don't know how to move!"); |
96e997158562
Gather game statistic.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
63
diff
changeset
|
653 |
return; |
96e997158562
Gather game statistic.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
63
diff
changeset
|
654 |
} |
96e997158562
Gather game statistic.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
63
diff
changeset
|
655 |
var updated = board.move[move].call(null, brd); |
96e997158562
Gather game statistic.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
63
diff
changeset
|
656 |
if (!updated) { |
96e997158562
Gather game statistic.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
63
diff
changeset
|
657 |
ui.game.setMessage("Wrong move!"); |
96e997158562
Gather game statistic.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
63
diff
changeset
|
658 |
return; |
96e997158562
Gather game statistic.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
63
diff
changeset
|
659 |
} |
96e997158562
Gather game statistic.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
63
diff
changeset
|
660 |
board.putRandom(brd); |
96e997158562
Gather game statistic.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
63
diff
changeset
|
661 |
turn++; |
96e997158562
Gather game statistic.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
63
diff
changeset
|
662 |
} |
96e997158562
Gather game statistic.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
63
diff
changeset
|
663 |
var tsTo = new Date().getTime(); |
68
f8d310e3aecf
Add time and turn count to statistic data.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
67
diff
changeset
|
664 |
var stat = board.score(brd); |
f8d310e3aecf
Add time and turn count to statistic data.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
67
diff
changeset
|
665 |
stat.turn = turn; |
f8d310e3aecf
Add time and turn count to statistic data.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
67
diff
changeset
|
666 |
stat.ts = tsTo - tsFrom; |
f8d310e3aecf
Add time and turn count to statistic data.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
67
diff
changeset
|
667 |
stats.push(stat); |
f8d310e3aecf
Add time and turn count to statistic data.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
67
diff
changeset
|
668 |
if (tsTo - tsLimitFrom >= tsLimit) |
64
96e997158562
Gather game statistic.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
63
diff
changeset
|
669 |
break; |
96e997158562
Gather game statistic.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
63
diff
changeset
|
670 |
} |
69
cbdfc1072009
Calculate statistic histogram.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
68
diff
changeset
|
671 |
var histo = {}; |
cbdfc1072009
Calculate statistic histogram.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
68
diff
changeset
|
672 |
for (i = stats.length-1; i >= 0; i--) { |
cbdfc1072009
Calculate statistic histogram.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
68
diff
changeset
|
673 |
var stat = stats[i]; |
cbdfc1072009
Calculate statistic histogram.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
68
diff
changeset
|
674 |
if ( ! histo[stat.max]) |
cbdfc1072009
Calculate statistic histogram.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
68
diff
changeset
|
675 |
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
|
676 |
var row = histo[stat.max]; |
cbdfc1072009
Calculate statistic histogram.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
68
diff
changeset
|
677 |
row.n++; |
cbdfc1072009
Calculate statistic histogram.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
68
diff
changeset
|
678 |
var speed = (stat.turn * 1000.0) / stat.ts; |
cbdfc1072009
Calculate statistic histogram.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
68
diff
changeset
|
679 |
row.minSpeed = Math.min(row.minSpeed, speed); |
cbdfc1072009
Calculate statistic histogram.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
68
diff
changeset
|
680 |
row.meanSpeed += speed; |
70
01076b56feae
Fix histogram calculation.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
69
diff
changeset
|
681 |
row.maxSpeed = Math.max(row.maxSpeed, speed); |
69
cbdfc1072009
Calculate statistic histogram.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
68
diff
changeset
|
682 |
row.minTurn = Math.min(row.minTurn, stat.turn); |
cbdfc1072009
Calculate statistic histogram.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
68
diff
changeset
|
683 |
row.meanTurn += stat.turn; |
70
01076b56feae
Fix histogram calculation.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
69
diff
changeset
|
684 |
row.maxTurn = Math.max(row.maxTurn, stat.turn); |
69
cbdfc1072009
Calculate statistic histogram.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
68
diff
changeset
|
685 |
row.minScore = Math.min(row.minScore, stat.score); |
cbdfc1072009
Calculate statistic histogram.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
68
diff
changeset
|
686 |
row.meanScore += stat.score; |
70
01076b56feae
Fix histogram calculation.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
69
diff
changeset
|
687 |
row.maxScore = Math.max(row.maxScore, stat.score); |
69
cbdfc1072009
Calculate statistic histogram.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
68
diff
changeset
|
688 |
} |
cbdfc1072009
Calculate statistic histogram.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
68
diff
changeset
|
689 |
for (var i in histo) { |
cbdfc1072009
Calculate statistic histogram.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
68
diff
changeset
|
690 |
var row = histo[i]; |
cbdfc1072009
Calculate statistic histogram.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
68
diff
changeset
|
691 |
var n = row.n; |
cbdfc1072009
Calculate statistic histogram.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
68
diff
changeset
|
692 |
row.meanSpeed = row.meanSpeed / n; |
cbdfc1072009
Calculate statistic histogram.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
68
diff
changeset
|
693 |
row.meanTurn = row.meanTurn / n; |
cbdfc1072009
Calculate statistic histogram.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
68
diff
changeset
|
694 |
row.meanScore = row.meanScore / n; |
cbdfc1072009
Calculate statistic histogram.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
68
diff
changeset
|
695 |
} |
71
aa0240db128a
Show statistic reports.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
70
diff
changeset
|
696 |
var reportDom = document.createElement('div'); |
aa0240db128a
Show statistic reports.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
70
diff
changeset
|
697 |
reportDom.classList.add('report'); |
aa0240db128a
Show statistic reports.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
70
diff
changeset
|
698 |
var h5Dom = document.createElement('h5'); |
aa0240db128a
Show statistic reports.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
70
diff
changeset
|
699 |
h5Dom.appendChild(document.createTextNode(ui.ai.currentName)); |
aa0240db128a
Show statistic reports.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
70
diff
changeset
|
700 |
reportDom.appendChild(h5Dom); |
aa0240db128a
Show statistic reports.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
70
diff
changeset
|
701 |
var tableDom = document.createElement('table'); |
aa0240db128a
Show statistic reports.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
70
diff
changeset
|
702 |
tableDom.classList.add('report-by-maxval'); |
aa0240db128a
Show statistic reports.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
70
diff
changeset
|
703 |
var trDom = document.createElement('tr'); |
aa0240db128a
Show statistic reports.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
70
diff
changeset
|
704 |
var thDom = document.createElement('td'); |
aa0240db128a
Show statistic reports.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
70
diff
changeset
|
705 |
thDom.appendChild(document.createTextNode('maxVal')); |
aa0240db128a
Show statistic reports.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
70
diff
changeset
|
706 |
trDom.appendChild(thDom); |
aa0240db128a
Show statistic reports.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
70
diff
changeset
|
707 |
var thDom = document.createElement('td'); |
aa0240db128a
Show statistic reports.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
70
diff
changeset
|
708 |
thDom.appendChild(document.createTextNode('n')); |
aa0240db128a
Show statistic reports.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
70
diff
changeset
|
709 |
trDom.appendChild(thDom); |
aa0240db128a
Show statistic reports.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
70
diff
changeset
|
710 |
var thDom = document.createElement('td'); |
aa0240db128a
Show statistic reports.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
70
diff
changeset
|
711 |
thDom.appendChild(document.createTextNode('%')); |
aa0240db128a
Show statistic reports.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
70
diff
changeset
|
712 |
trDom.appendChild(thDom); |
aa0240db128a
Show statistic reports.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
70
diff
changeset
|
713 |
var thDom = document.createElement('td'); |
aa0240db128a
Show statistic reports.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
70
diff
changeset
|
714 |
thDom.appendChild(document.createTextNode('min\nscore')); |
aa0240db128a
Show statistic reports.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
70
diff
changeset
|
715 |
trDom.appendChild(thDom); |
aa0240db128a
Show statistic reports.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
70
diff
changeset
|
716 |
var thDom = document.createElement('td'); |
aa0240db128a
Show statistic reports.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
70
diff
changeset
|
717 |
thDom.appendChild(document.createTextNode('mean score')); |
aa0240db128a
Show statistic reports.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
70
diff
changeset
|
718 |
trDom.appendChild(thDom); |
aa0240db128a
Show statistic reports.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
70
diff
changeset
|
719 |
var thDom = document.createElement('td'); |
aa0240db128a
Show statistic reports.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
70
diff
changeset
|
720 |
thDom.appendChild(document.createTextNode('max score')); |
aa0240db128a
Show statistic reports.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
70
diff
changeset
|
721 |
trDom.appendChild(thDom); |
aa0240db128a
Show statistic reports.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
70
diff
changeset
|
722 |
tableDom.appendChild(trDom); |
72
0d1e5bbcde82
Fix value ordering. Fix game counting when time limit reached.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
71
diff
changeset
|
723 |
var maxVals = Object.keys(histo).sort(function(a,b){return a - b}); |
71
aa0240db128a
Show statistic reports.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
70
diff
changeset
|
724 |
for (var i = maxVals.length-1; i >= 0; i--) { |
aa0240db128a
Show statistic reports.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
70
diff
changeset
|
725 |
var maxVal = maxVals[i]; |
aa0240db128a
Show statistic reports.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
70
diff
changeset
|
726 |
var row = histo[maxVals[i]]; |
aa0240db128a
Show statistic reports.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
70
diff
changeset
|
727 |
console.log("%d: %o", maxVals[i], row); |
aa0240db128a
Show statistic reports.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
70
diff
changeset
|
728 |
var trDom = document.createElement('tr'); |
aa0240db128a
Show statistic reports.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
70
diff
changeset
|
729 |
var tdDom = document.createElement('td'); |
aa0240db128a
Show statistic reports.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
70
diff
changeset
|
730 |
tdDom.appendChild(document.createTextNode(maxVal)); |
aa0240db128a
Show statistic reports.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
70
diff
changeset
|
731 |
trDom.appendChild(tdDom); |
aa0240db128a
Show statistic reports.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
70
diff
changeset
|
732 |
var tdDom = document.createElement('td'); |
aa0240db128a
Show statistic reports.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
70
diff
changeset
|
733 |
tdDom.appendChild(document.createTextNode(row.n)); |
aa0240db128a
Show statistic reports.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
70
diff
changeset
|
734 |
trDom.appendChild(tdDom); |
aa0240db128a
Show statistic reports.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
70
diff
changeset
|
735 |
var tdDom = document.createElement('td'); |
aa0240db128a
Show statistic reports.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
70
diff
changeset
|
736 |
tdDom.appendChild(document.createTextNode(parseFloat((100 * row.n / gameCnt).toPrecision(3)))); |
aa0240db128a
Show statistic reports.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
70
diff
changeset
|
737 |
trDom.appendChild(tdDom); |
aa0240db128a
Show statistic reports.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
70
diff
changeset
|
738 |
var tdDom = document.createElement('td'); |
aa0240db128a
Show statistic reports.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
70
diff
changeset
|
739 |
tdDom.appendChild(document.createTextNode(row.minScore)); |
aa0240db128a
Show statistic reports.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
70
diff
changeset
|
740 |
trDom.appendChild(tdDom); |
aa0240db128a
Show statistic reports.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
70
diff
changeset
|
741 |
var tdDom = document.createElement('td'); |
aa0240db128a
Show statistic reports.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
70
diff
changeset
|
742 |
tdDom.appendChild(document.createTextNode(parseFloat(row.meanScore.toPrecision(3)))); |
aa0240db128a
Show statistic reports.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
70
diff
changeset
|
743 |
trDom.appendChild(tdDom); |
aa0240db128a
Show statistic reports.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
70
diff
changeset
|
744 |
var tdDom = document.createElement('td'); |
aa0240db128a
Show statistic reports.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
70
diff
changeset
|
745 |
tdDom.appendChild(document.createTextNode(row.maxScore)); |
aa0240db128a
Show statistic reports.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
70
diff
changeset
|
746 |
trDom.appendChild(tdDom); |
aa0240db128a
Show statistic reports.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
70
diff
changeset
|
747 |
tableDom.appendChild(trDom); |
aa0240db128a
Show statistic reports.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
70
diff
changeset
|
748 |
} |
aa0240db128a
Show statistic reports.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
70
diff
changeset
|
749 |
reportDom.appendChild(tableDom); |
aa0240db128a
Show statistic reports.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
70
diff
changeset
|
750 |
reportsDom.insertBefore(reportDom, reportsDom.firstChild); |
62 | 751 |
} |
752 |
||
753 |
var statisticBtn = document.getElementById('statistic'); |
|
65
b8028f3e30fb
Use preventDefault()/stopPropagation(). Add false to addEventListener().
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
64
diff
changeset
|
754 |
statisticBtn.addEventListener("click", statistic, false); |
62 | 755 |
|
756 |
||
757 |
//////////////////////////////////////////////////////////////// |
|
758 |
// Initialise game. |
|
759 |
||
25
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
760 |
ui.brdEngine = BoardArr2d; // TODO make user selectable |
0 | 761 |
|
43
8b1577211149
Save val2048 preference.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
42
diff
changeset
|
762 |
if (localStorage.val2048) { |
8b1577211149
Save val2048 preference.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
42
diff
changeset
|
763 |
try { |
8b1577211149
Save val2048 preference.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
42
diff
changeset
|
764 |
ui.board.val2048Dom.checked = JSON.parse(localStorage.val2048); |
8b1577211149
Save val2048 preference.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
42
diff
changeset
|
765 |
} catch (ex) { } |
8b1577211149
Save val2048 preference.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
42
diff
changeset
|
766 |
} |
41
359719178176
Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
40
diff
changeset
|
767 |
if (localStorage.savedBoard) { |
42
716508d5494c
Save board between browser sessions.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
41
diff
changeset
|
768 |
try { |
45 | 769 |
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
|
770 |
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
|
771 |
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
|
772 |
else |
4dadaff6eba4
Update/store/restore game turn. Show max value according to value settings.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
45
diff
changeset
|
773 |
ui.board.turn = 0; |
45 | 774 |
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
|
775 |
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
|
776 |
} catch (ex) { } |
41
359719178176
Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
40
diff
changeset
|
777 |
} else { |
359719178176
Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
40
diff
changeset
|
778 |
ui.action.start(); |
359719178176
Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
40
diff
changeset
|
779 |
} |
32
f48151863cdf
Start game after page load.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
31
diff
changeset
|
780 |
|
0 | 781 |
</script> |
782 |
||
783 |
</body> |
|
784 |
</html> |