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