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