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