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