author | Oleksandr Gavenko <gavenkoa@gmail.com> |
Sat, 06 Sep 2014 22:58:53 +0300 | |
changeset 7 | d8106c8c0481 |
parent 4 | 732aef931a9e |
child 10 | 70ece7f758a0 |
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 |
||
7
d8106c8c0481
Rename methods and use sequential numbers instead of power of 2.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
4
diff
changeset
|
8 |
<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
|
9 |
<script src="perf.js"></script> |
d8106c8c0481
Rename methods and use sequential numbers instead of power of 2.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
4
diff
changeset
|
10 |
|
0 | 11 |
<style> |
12 |
body { |
|
13 |
width: 100%; |
|
14 |
} |
|
4 | 15 |
h1, div.area { |
0 | 16 |
text-align: center; |
4 | 17 |
margin: 10px auto; |
0 | 18 |
} |
19 |
#board { |
|
20 |
margin: 10px auto; |
|
21 |
} |
|
22 |
#board td { |
|
23 |
width: 40px; |
|
24 |
height: 40px; |
|
25 |
border: 1px solid red; |
|
26 |
margin: 0; |
|
27 |
text-align: center; |
|
28 |
} |
|
4 | 29 |
.report > .name { |
30 |
font-weight: bold; |
|
31 |
} |
|
0 | 32 |
</style> |
33 |
</head> |
|
34 |
<body> |
|
35 |
||
36 |
<h1>2048</h1> |
|
37 |
||
4 | 38 |
<div id="score-area" class="area">Score: <span id="score">0</span>, Max: <span id="max">0</span>, Speed: <span id="speed">0</span> t/s, Turn: <span id="turn">0</span></div> |
0 | 39 |
|
4 | 40 |
<div id="message-area" class="area"></div> |
0 | 41 |
|
42 |
<table id="board"> |
|
43 |
<tr> |
|
44 |
<td></td> |
|
45 |
<td></td> |
|
46 |
<td></td> |
|
47 |
<td></td> |
|
48 |
</tr> |
|
49 |
<tr> |
|
50 |
<td></td> |
|
51 |
<td></td> |
|
52 |
<td></td> |
|
53 |
<td></td> |
|
54 |
</tr> |
|
55 |
<tr> |
|
56 |
<td></td> |
|
57 |
<td></td> |
|
58 |
<td></td> |
|
59 |
<td></td> |
|
60 |
</tr> |
|
61 |
<tr> |
|
62 |
<td></td> |
|
63 |
<td></td> |
|
64 |
<td></td> |
|
65 |
<td></td> |
|
66 |
</tr> |
|
67 |
</table> |
|
68 |
||
4 | 69 |
<div id="control-area" class="area"> |
0 | 70 |
<div> |
71 |
<button id="start">Start</button> |
|
72 |
<button id="step">Step</button> |
|
73 |
<button id="loop">Loop</button> |
|
74 |
<button id="finish">Finish</button> |
|
75 |
</div> |
|
76 |
<div> |
|
77 |
<button id="left">left</button> |
|
78 |
<button id="up">up</button> |
|
79 |
<button id="down">down</button> |
|
80 |
<button id="right">right</button> |
|
81 |
</div> |
|
82 |
<h1>AI</h1> |
|
83 |
<div> |
|
84 |
<button id="ai-random">random</button> |
|
85 |
<button id="ai-next-max-score">next max score</button> |
|
86 |
<button id="ai-next-max-value">next max value</button> |
|
87 |
</div> |
|
88 |
</div> |
|
89 |
||
4 | 90 |
<div id="report-area" class="area"> |
91 |
<h1>Reports</h1> |
|
92 |
<div class="report" class="area"> |
|
93 |
<div class="name">next max score</div> |
|
94 |
<table> |
|
95 |
<tr> |
|
96 |
<th>Max</th> |
|
97 |
<th>Count</th> |
|
98 |
<th>%</th> |
|
99 |
</tr> |
|
100 |
<tr> |
|
101 |
<td>128</td><td>8</td><td><div style="background: red; width: 100px">x</div></td> |
|
102 |
</tr> |
|
103 |
<tr> |
|
104 |
<td>256</td><td>4</td><td><div style="background: red; width: 50px">x</div></td> |
|
105 |
</tr> |
|
106 |
<tr> |
|
107 |
<td>512</td><td>12</td><td><div style="background: red; width: 150px">x</div></td> |
|
108 |
</tr> |
|
109 |
</table> |
|
110 |
</div> |
|
111 |
</div> |
|
112 |
||
0 | 113 |
<script> |
114 |
"use strict"; |
|
115 |
||
116 |
var board = {}; |
|
117 |
board.create = function() { |
|
118 |
var brd = []; |
|
119 |
for (var i = 0; i < 4; i++) { |
|
120 |
brd[i] = []; |
|
121 |
for (var j = 0; j < 4; j++) { |
|
122 |
brd[i][j] = 0; |
|
123 |
} |
|
124 |
} |
|
125 |
return brd; |
|
126 |
} |
|
127 |
board.copy = function(from, to) { |
|
128 |
for (var i = 0; i < 4; i++) { |
|
129 |
for (var j = 0; j < 4; j++) { |
|
130 |
to[i][j] = from[i][j]; |
|
131 |
} |
|
132 |
} |
|
133 |
} |
|
134 |
board.freeCnt = function(brd) { |
|
135 |
var cnt = 0; |
|
136 |
for (var i = 0; i < 4; i++) { |
|
137 |
for (var j = 0; j < 4; j++) { |
|
138 |
if (brd[i][j] === 0) |
|
139 |
cnt++; |
|
140 |
} |
|
141 |
} |
|
142 |
return cnt; |
|
143 |
} |
|
144 |
board.gameOver = function(brd) { |
|
145 |
if (board.freeCnt(brd) > 0) |
|
146 |
return false; |
|
147 |
for (var i = 0; i < 4; i++) { |
|
148 |
for (var j = 0; j < 3; j++) { |
|
149 |
if (brd[i][j] === brd[i][j+1]) |
|
150 |
return false; |
|
151 |
} |
|
152 |
} |
|
153 |
for (var j = 0; j < 4; j++) { |
|
154 |
for (var i = 0; i < 3; i++) { |
|
155 |
if (brd[i][j] === brd[i+1][j]) |
|
156 |
return false; |
|
157 |
} |
|
158 |
} |
|
159 |
return true; |
|
160 |
} |
|
7
d8106c8c0481
Rename methods and use sequential numbers instead of power of 2.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
4
diff
changeset
|
161 |
board.putRandom = function(brd) { |
0 | 162 |
var cnt = board.freeCnt(brd); |
163 |
cnt = Math.floor(Math.random() * cnt)+1; |
|
164 |
for (var i = 0; i < 4 && cnt > 0; i++) { |
|
165 |
for (var j = 0; j < 4 && cnt > 0; j++) { |
|
166 |
if (brd[i][j] !== 0) |
|
167 |
continue; |
|
168 |
if (cnt === 1) |
|
7
d8106c8c0481
Rename methods and use sequential numbers instead of power of 2.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
4
diff
changeset
|
169 |
brd[i][j] = (Math.random() > .9) ? 2 : 1; |
0 | 170 |
cnt--; |
171 |
} |
|
172 |
} |
|
173 |
} |
|
174 |
/* http://www.reddit.com/r/2048/comments/214njx/highest_possible_score_for_2048_warning_math */ |
|
7
d8106c8c0481
Rename methods and use sequential numbers instead of power of 2.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
4
diff
changeset
|
175 |
var boardScoreTbl = [0]; |
2
11da0a8fabf3
Cache for scores values.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1
diff
changeset
|
176 |
for (var i = 1, exp = 2; i < 16; i++, exp *= 2) { |
7
d8106c8c0481
Rename methods and use sequential numbers instead of power of 2.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
4
diff
changeset
|
177 |
boardScoreTbl[i] = (i-1)*exp; |
2
11da0a8fabf3
Cache for scores values.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1
diff
changeset
|
178 |
} |
0 | 179 |
board.score = function(brd) { |
180 |
var score = 0; |
|
181 |
var max = 0; |
|
182 |
for (var i = 0; i < 4; i++) { |
|
183 |
for (var j = 0; j < 4; j++) { |
|
184 |
var val = brd[i][j]; |
|
3 | 185 |
score += boardScoreTbl[val]; |
0 | 186 |
if (max < val) |
187 |
max = val; |
|
188 |
} |
|
189 |
} |
|
7
d8106c8c0481
Rename methods and use sequential numbers instead of power of 2.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
4
diff
changeset
|
190 |
return {score: score, max: Math.pow(2, max)}; |
0 | 191 |
} |
192 |
||
193 |
board.row = {}; |
|
194 |
board.row.init = function() { |
|
195 |
return {stack: [], curr: 0}; |
|
196 |
} |
|
197 |
board.row.push = function(state, val) { |
|
198 |
if (val === 0) |
|
199 |
return; |
|
200 |
if (state.curr === 0) { |
|
201 |
state.curr = val; |
|
202 |
return; |
|
203 |
} |
|
204 |
if (state.curr === val) { |
|
7
d8106c8c0481
Rename methods and use sequential numbers instead of power of 2.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
4
diff
changeset
|
205 |
state.stack.push(state.curr+1); |
0 | 206 |
state.curr = 0; |
207 |
} else { |
|
208 |
state.stack.push(state.curr); |
|
209 |
state.curr = val; |
|
210 |
} |
|
211 |
} |
|
212 |
board.row.finish = function(state) { |
|
213 |
if (state.curr !== 0) |
|
214 |
state.stack.push(state.curr); |
|
215 |
} |
|
216 |
board.move = {}; |
|
217 |
board.move.up = function(brd) { |
|
218 |
var updated = false; |
|
219 |
for (var j = 0; j < 4; j++) { |
|
220 |
var state = board.row.init(); |
|
221 |
for (var i = 0; i < 4; i++) { |
|
222 |
board.row.push(state, brd[i][j]); |
|
223 |
} |
|
224 |
board.row.finish(state); |
|
225 |
for (var i = 0; i < state.stack.length; i++) { |
|
226 |
if (brd[i][j] !== state.stack[i]) |
|
227 |
updated = true; |
|
228 |
brd[i][j] = state.stack[i]; |
|
229 |
} |
|
230 |
for (; i < 4; i++) { |
|
231 |
if (brd[i][j] !== 0) |
|
232 |
updated = true; |
|
233 |
brd[i][j] = 0; |
|
234 |
} |
|
235 |
} |
|
236 |
return updated; |
|
237 |
}; |
|
238 |
board.move.down = function(brd) { |
|
239 |
var updated = false; |
|
240 |
for (var j = 0; j < 4; j++) { |
|
241 |
var state = board.row.init(); |
|
242 |
for (var i = 3; i >= 0; i--) { |
|
243 |
board.row.push(state, brd[i][j]); |
|
244 |
} |
|
245 |
board.row.finish(state); |
|
246 |
for (var i = 0; i < state.stack.length; i++) { |
|
247 |
if (brd[3-i][j] !== state.stack[i]) |
|
248 |
updated = true; |
|
249 |
brd[3-i][j] = state.stack[i]; |
|
250 |
} |
|
251 |
for (; i < 4; i++) { |
|
252 |
if (brd[3-i][j] !== 0) |
|
253 |
updated = true; |
|
254 |
brd[3-i][j] = 0; |
|
255 |
} |
|
256 |
} |
|
257 |
return updated; |
|
258 |
}; |
|
259 |
board.move.left = function(brd) { |
|
260 |
var updated = false; |
|
261 |
for (var i = 0; i < 4; i++) { |
|
262 |
var state = board.row.init(); |
|
263 |
for (var j = 0; j < 4; j++) { |
|
264 |
board.row.push(state, brd[i][j]); |
|
265 |
} |
|
266 |
board.row.finish(state); |
|
267 |
for (var j = 0; j < state.stack.length; j++) { |
|
268 |
if (brd[i][j] !== state.stack[j]) |
|
269 |
updated = true; |
|
270 |
brd[i][j] = state.stack[j]; |
|
271 |
} |
|
272 |
for (; j < 4; j++) { |
|
273 |
if (brd[i][j] !== 0) |
|
274 |
updated = true; |
|
275 |
brd[i][j] = 0; |
|
276 |
} |
|
277 |
} |
|
278 |
return updated; |
|
279 |
}; |
|
280 |
board.move.right = function(brd) { |
|
281 |
var updated = false; |
|
282 |
for (var i = 0; i < 4; i++) { |
|
283 |
var state = board.row.init(); |
|
284 |
for (var j = 3; j >= 0; j--) { |
|
285 |
board.row.push(state, brd[i][j]); |
|
286 |
} |
|
287 |
board.row.finish(state); |
|
288 |
for (var j = 0; j < state.stack.length; j++) { |
|
289 |
if (brd[i][3-j] !== state.stack[j]) |
|
290 |
updated = true; |
|
291 |
brd[i][3-j] = state.stack[j]; |
|
292 |
} |
|
293 |
for (; j < 4; j++) { |
|
294 |
if (brd[i][3-j] !== 0) |
|
295 |
updated = true; |
|
296 |
brd[i][3-j] = 0; |
|
297 |
} |
|
298 |
} |
|
299 |
return updated; |
|
300 |
}; |
|
301 |
||
302 |
var boardDom = document.getElementById("board"); |
|
303 |
var ui = {}; |
|
304 |
ui.board = {}; |
|
305 |
ui.board.set = function(i, j, val) { |
|
306 |
boardDom.querySelectorAll("tr")[i].querySelectorAll("td")[j].innerHTML = val; |
|
307 |
} |
|
308 |
ui.board.update = function(brd) { |
|
309 |
for (var i = 0; i < 4; i++) { |
|
310 |
for (var j = 0; j < 4; j++) { |
|
7
d8106c8c0481
Rename methods and use sequential numbers instead of power of 2.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
4
diff
changeset
|
311 |
ui.board.set(i, j, (brd[i][j] >= 1) ? brd[i][j] : ""); |
0 | 312 |
} |
313 |
} |
|
314 |
} |
|
315 |
ui.score = {}; |
|
316 |
var scoreDom = document.getElementById("score"); |
|
317 |
var maxDom = document.getElementById("max"); |
|
1 | 318 |
var speedDom = document.getElementById("speed"); |
319 |
var turnDom = document.getElementById("turn"); |
|
0 | 320 |
ui.score.clear = function(brd) { |
321 |
scoreDom.innerHTML = '0'; |
|
322 |
maxDom.innerHTML = '0'; |
|
1 | 323 |
speedDom.innerHTML = '0'; |
324 |
turnDom.innerHTML = '0'; |
|
0 | 325 |
} |
326 |
ui.score.update = function(brd) { |
|
327 |
var score = board.score(brd); |
|
328 |
scoreDom.innerHTML = '' + score.score; |
|
329 |
maxDom.innerHTML = '' + score.max; |
|
330 |
} |
|
1 | 331 |
ui.score.speed = function(speed, turn) { |
332 |
speedDom.innerHTML = '' + speed; |
|
333 |
turnDom.innerHTML = '' + turn; |
|
334 |
} |
|
0 | 335 |
|
336 |
function start() { |
|
337 |
ui.score.clear(); |
|
338 |
ui.message.clear(); |
|
339 |
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
|
340 |
board.putRandom(board.current); |
0 | 341 |
ui.board.update(board.current); |
342 |
} |
|
343 |
document.getElementById("start").addEventListener("click", start); |
|
344 |
||
345 |
function up() { |
|
346 |
var updated = board.move.up(board.current); |
|
347 |
if (updated) { |
|
7
d8106c8c0481
Rename methods and use sequential numbers instead of power of 2.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
4
diff
changeset
|
348 |
board.putRandom(board.current); |
0 | 349 |
ui.board.update(board.current); |
350 |
ui.score.update(board.current); |
|
351 |
} |
|
352 |
} |
|
353 |
document.getElementById("up").addEventListener("click", up); |
|
354 |
function down() { |
|
355 |
var updated = board.move.down(board.current); |
|
356 |
if (updated) { |
|
7
d8106c8c0481
Rename methods and use sequential numbers instead of power of 2.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
4
diff
changeset
|
357 |
board.putRandom(board.current); |
0 | 358 |
ui.board.update(board.current); |
359 |
ui.score.update(board.current); |
|
360 |
} |
|
361 |
} |
|
362 |
document.getElementById("down").addEventListener("click", down); |
|
363 |
function left() { |
|
364 |
var updated = board.move.left(board.current); |
|
365 |
if (updated) { |
|
7
d8106c8c0481
Rename methods and use sequential numbers instead of power of 2.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
4
diff
changeset
|
366 |
board.putRandom(board.current); |
0 | 367 |
ui.board.update(board.current); |
368 |
ui.score.update(board.current); |
|
369 |
} |
|
370 |
} |
|
371 |
document.getElementById("left").addEventListener("click", left); |
|
372 |
function right() { |
|
373 |
var updated = board.move.right(board.current); |
|
374 |
if (updated) { |
|
7
d8106c8c0481
Rename methods and use sequential numbers instead of power of 2.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
4
diff
changeset
|
375 |
board.putRandom(board.current); |
0 | 376 |
ui.board.update(board.current); |
377 |
ui.score.update(board.current); |
|
378 |
} |
|
379 |
} |
|
380 |
document.getElementById("right").addEventListener("click", right); |
|
381 |
||
382 |
document.body.addEventListener("keydown", function(event) { |
|
383 |
var key = event.keyCode || event.which; |
|
384 |
switch (key) { |
|
385 |
case 38: up(); break; |
|
386 |
case 40: down(); break; |
|
387 |
case 37: left(); break; |
|
388 |
case 39: right(); break; |
|
389 |
} |
|
390 |
return false; |
|
391 |
}); |
|
392 |
||
393 |
ui.message = {}; |
|
394 |
var messageDom = document.getElementById("message-area"); |
|
395 |
ui.message.clear = function() { |
|
396 |
messageDom.innerHTML = ""; |
|
397 |
} |
|
398 |
ui.message.set = function(msg) { |
|
399 |
messageDom.innerHTML = msg; |
|
400 |
} |
|
401 |
||
402 |
function step() { |
|
403 |
ui.message.clear(); |
|
404 |
if (board.gameOver(board.current)) { |
|
405 |
ui.message.set("Game over!"); |
|
406 |
return; |
|
407 |
} |
|
408 |
var tmpBrd = board.create(); |
|
409 |
board.copy(board.current, tmpBrd); |
|
410 |
var fn = ai.current(tmpBrd); |
|
411 |
if (typeof fn === 'undefined') { |
|
412 |
ui.message.set("I don't know how to move!"); |
|
413 |
return; |
|
414 |
} |
|
415 |
var updated = board.move[fn].call(null, board.current); |
|
416 |
if (updated) { |
|
7
d8106c8c0481
Rename methods and use sequential numbers instead of power of 2.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
4
diff
changeset
|
417 |
board.putRandom(board.current); |
0 | 418 |
ui.board.update(board.current); |
419 |
ui.score.update(board.current); |
|
420 |
} else { |
|
421 |
ui.message.set("Wrong move!"); |
|
422 |
} |
|
423 |
} |
|
424 |
document.getElementById("step").addEventListener("click", step); |
|
425 |
||
426 |
function finish() { |
|
427 |
ui.message.clear(); |
|
1 | 428 |
var step = 0; |
429 |
var tsFrom = new Date().getTime(); |
|
0 | 430 |
while (!board.gameOver(board.current)) { |
431 |
var tmpBrd = board.create(); |
|
432 |
board.copy(board.current, tmpBrd); |
|
433 |
var fn = ai.current(tmpBrd); |
|
434 |
if (typeof fn === 'undefined') { |
|
435 |
ui.message.set("I don't know how to move!"); |
|
436 |
return; |
|
437 |
} |
|
438 |
var updated = board.move[fn].call(null, board.current); |
|
439 |
if (updated) { |
|
7
d8106c8c0481
Rename methods and use sequential numbers instead of power of 2.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
4
diff
changeset
|
440 |
board.putRandom(board.current); |
0 | 441 |
} else { |
442 |
ui.board.update(board.current); |
|
443 |
ui.score.update(board.current); |
|
444 |
ui.message.set("Wrong move!"); |
|
445 |
return; |
|
446 |
} |
|
1 | 447 |
step++; |
0 | 448 |
} |
1 | 449 |
var tsTo = new Date().getTime(); |
0 | 450 |
ui.board.update(board.current); |
451 |
ui.score.update(board.current); |
|
1 | 452 |
ui.score.speed(step*1000.0/(tsTo-tsFrom), step); |
0 | 453 |
ui.message.set("Game over!"); |
454 |
} |
|
455 |
document.getElementById("finish").addEventListener("click", finish); |
|
456 |
||
457 |
var ai = {}; |
|
458 |
||
459 |
ai.random = function(brd) { |
|
460 |
var tmpBrd = board.create(); |
|
461 |
do { |
|
462 |
var action = ["up", "down", "left", "right"][Math.floor(Math.random()*4)]; |
|
463 |
board.copy(brd, tmpBrd); |
|
464 |
} while (!board.move[action](tmpBrd)); |
|
465 |
return action; |
|
466 |
} |
|
467 |
document.getElementById("ai-random").addEventListener("click", function() { |
|
468 |
ai.current = ai.random; |
|
469 |
}); |
|
470 |
||
471 |
ai.nextMaxScore = function(brd) { |
|
472 |
var tmpBrd = board.create(); |
|
473 |
board.copy(brd, tmpBrd); |
|
474 |
var maxScore = -1; |
|
475 |
var action; |
|
476 |
if (board.move.up(tmpBrd)) { |
|
477 |
maxScore = board.score(tmpBrd).score; |
|
478 |
action = "up"; |
|
479 |
} |
|
480 |
board.copy(brd, tmpBrd); |
|
481 |
if (board.move.left(tmpBrd)) { |
|
482 |
var score = board.score(tmpBrd).score; |
|
483 |
if (maxScore < score) { |
|
484 |
action = "left"; |
|
485 |
maxScore = score; |
|
486 |
} |
|
487 |
} |
|
488 |
board.copy(brd, tmpBrd); |
|
489 |
if (board.move.down(tmpBrd)) { |
|
490 |
var score = board.score(tmpBrd).score; |
|
491 |
if (maxScore < score) { |
|
492 |
action = "down"; |
|
493 |
maxScore = score; |
|
494 |
} |
|
495 |
} |
|
496 |
board.copy(brd, tmpBrd); |
|
497 |
if (board.move.right(tmpBrd)) { |
|
498 |
var score = board.score(tmpBrd).score; |
|
499 |
if (maxScore < score) { |
|
500 |
action = "right"; |
|
501 |
maxScore = score; |
|
502 |
} |
|
503 |
} |
|
504 |
return action; |
|
505 |
} |
|
506 |
document.getElementById("ai-next-max-score").addEventListener("click", function() { |
|
507 |
ai.current = ai.nextMaxScore; |
|
508 |
}); |
|
509 |
||
510 |
||
511 |
ai.nextMaxValue = function(brd) { |
|
512 |
var tmpBrd = board.create(); |
|
513 |
board.copy(brd, tmpBrd); |
|
514 |
var maxMax = -1; |
|
515 |
var action; |
|
516 |
if (board.move.up(tmpBrd)) { |
|
517 |
maxMax = board.score(tmpBrd).max; |
|
518 |
action = "up"; |
|
519 |
} |
|
520 |
board.copy(brd, tmpBrd); |
|
521 |
if (board.move.left(tmpBrd)) { |
|
522 |
var max = board.score(tmpBrd).max; |
|
523 |
if (maxMax < max) { |
|
524 |
action = "left"; |
|
525 |
maxMax = max; |
|
526 |
} |
|
527 |
} |
|
528 |
board.copy(brd, tmpBrd); |
|
529 |
if (board.move.down(tmpBrd)) { |
|
530 |
var max = board.score(tmpBrd).max; |
|
531 |
if (maxMax < max) { |
|
532 |
action = "down"; |
|
533 |
maxMax = max; |
|
534 |
} |
|
535 |
} |
|
536 |
board.copy(brd, tmpBrd); |
|
537 |
if (board.move.right(tmpBrd)) { |
|
538 |
var max = board.score(tmpBrd).max; |
|
539 |
if (maxMax < max) { |
|
540 |
action = "right"; |
|
541 |
maxMax = max; |
|
542 |
} |
|
543 |
} |
|
544 |
return action; |
|
545 |
} |
|
546 |
document.getElementById("ai-next-max-value").addEventListener("click", function() { |
|
547 |
ai.current = ai.nextMaxValue; |
|
548 |
}); |
|
549 |
||
550 |
</script> |
|
551 |
||
552 |
</body> |
|
553 |
</html> |