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 */
|
|
145 |
board.score = function(brd) {
|
|
146 |
var score = 0;
|
|
147 |
var max = 0;
|
|
148 |
for (var i = 0; i < 4; i++) {
|
|
149 |
for (var j = 0; j < 4; j++) {
|
|
150 |
var val = brd[i][j];
|
|
151 |
if (val > 2)
|
|
152 |
score += Math.log2(val) * val;
|
|
153 |
if (max < val)
|
|
154 |
max = val;
|
|
155 |
}
|
|
156 |
}
|
|
157 |
return {score: score, max: max};
|
|
158 |
}
|
|
159 |
|
|
160 |
board.row = {};
|
|
161 |
board.row.init = function() {
|
|
162 |
return {stack: [], curr: 0};
|
|
163 |
}
|
|
164 |
board.row.push = function(state, val) {
|
|
165 |
if (val === 0)
|
|
166 |
return;
|
|
167 |
if (state.curr === 0) {
|
|
168 |
state.curr = val;
|
|
169 |
return;
|
|
170 |
}
|
|
171 |
if (state.curr === val) {
|
|
172 |
state.stack.push(state.curr*2);
|
|
173 |
state.curr = 0;
|
|
174 |
} else {
|
|
175 |
state.stack.push(state.curr);
|
|
176 |
state.curr = val;
|
|
177 |
}
|
|
178 |
}
|
|
179 |
board.row.finish = function(state) {
|
|
180 |
if (state.curr !== 0)
|
|
181 |
state.stack.push(state.curr);
|
|
182 |
}
|
|
183 |
board.move = {};
|
|
184 |
board.move.up = function(brd) {
|
|
185 |
var updated = false;
|
|
186 |
for (var j = 0; j < 4; j++) {
|
|
187 |
var state = board.row.init();
|
|
188 |
for (var i = 0; i < 4; i++) {
|
|
189 |
board.row.push(state, brd[i][j]);
|
|
190 |
}
|
|
191 |
board.row.finish(state);
|
|
192 |
for (var i = 0; i < state.stack.length; i++) {
|
|
193 |
if (brd[i][j] !== state.stack[i])
|
|
194 |
updated = true;
|
|
195 |
brd[i][j] = state.stack[i];
|
|
196 |
}
|
|
197 |
for (; i < 4; i++) {
|
|
198 |
if (brd[i][j] !== 0)
|
|
199 |
updated = true;
|
|
200 |
brd[i][j] = 0;
|
|
201 |
}
|
|
202 |
}
|
|
203 |
return updated;
|
|
204 |
};
|
|
205 |
board.move.down = function(brd) {
|
|
206 |
var updated = false;
|
|
207 |
for (var j = 0; j < 4; j++) {
|
|
208 |
var state = board.row.init();
|
|
209 |
for (var i = 3; i >= 0; i--) {
|
|
210 |
board.row.push(state, brd[i][j]);
|
|
211 |
}
|
|
212 |
board.row.finish(state);
|
|
213 |
for (var i = 0; i < state.stack.length; i++) {
|
|
214 |
if (brd[3-i][j] !== state.stack[i])
|
|
215 |
updated = true;
|
|
216 |
brd[3-i][j] = state.stack[i];
|
|
217 |
}
|
|
218 |
for (; i < 4; i++) {
|
|
219 |
if (brd[3-i][j] !== 0)
|
|
220 |
updated = true;
|
|
221 |
brd[3-i][j] = 0;
|
|
222 |
}
|
|
223 |
}
|
|
224 |
return updated;
|
|
225 |
};
|
|
226 |
board.move.left = function(brd) {
|
|
227 |
var updated = false;
|
|
228 |
for (var i = 0; i < 4; i++) {
|
|
229 |
var state = board.row.init();
|
|
230 |
for (var j = 0; j < 4; j++) {
|
|
231 |
board.row.push(state, brd[i][j]);
|
|
232 |
}
|
|
233 |
board.row.finish(state);
|
|
234 |
for (var j = 0; j < state.stack.length; j++) {
|
|
235 |
if (brd[i][j] !== state.stack[j])
|
|
236 |
updated = true;
|
|
237 |
brd[i][j] = state.stack[j];
|
|
238 |
}
|
|
239 |
for (; j < 4; j++) {
|
|
240 |
if (brd[i][j] !== 0)
|
|
241 |
updated = true;
|
|
242 |
brd[i][j] = 0;
|
|
243 |
}
|
|
244 |
}
|
|
245 |
return updated;
|
|
246 |
};
|
|
247 |
board.move.right = function(brd) {
|
|
248 |
var updated = false;
|
|
249 |
for (var i = 0; i < 4; i++) {
|
|
250 |
var state = board.row.init();
|
|
251 |
for (var j = 3; j >= 0; j--) {
|
|
252 |
board.row.push(state, brd[i][j]);
|
|
253 |
}
|
|
254 |
board.row.finish(state);
|
|
255 |
for (var j = 0; j < state.stack.length; j++) {
|
|
256 |
if (brd[i][3-j] !== state.stack[j])
|
|
257 |
updated = true;
|
|
258 |
brd[i][3-j] = state.stack[j];
|
|
259 |
}
|
|
260 |
for (; j < 4; j++) {
|
|
261 |
if (brd[i][3-j] !== 0)
|
|
262 |
updated = true;
|
|
263 |
brd[i][3-j] = 0;
|
|
264 |
}
|
|
265 |
}
|
|
266 |
return updated;
|
|
267 |
};
|
|
268 |
|
|
269 |
var boardDom = document.getElementById("board");
|
|
270 |
var ui = {};
|
|
271 |
ui.board = {};
|
|
272 |
ui.board.set = function(i, j, val) {
|
|
273 |
boardDom.querySelectorAll("tr")[i].querySelectorAll("td")[j].innerHTML = val;
|
|
274 |
}
|
|
275 |
ui.board.update = function(brd) {
|
|
276 |
for (var i = 0; i < 4; i++) {
|
|
277 |
for (var j = 0; j < 4; j++) {
|
|
278 |
ui.board.set(i, j, (brd[i][j] >= 2) ? brd[i][j] : "");
|
|
279 |
}
|
|
280 |
}
|
|
281 |
}
|
|
282 |
ui.score = {};
|
|
283 |
var scoreDom = document.getElementById("score");
|
|
284 |
var maxDom = document.getElementById("max");
|
1
|
285 |
var speedDom = document.getElementById("speed");
|
|
286 |
var turnDom = document.getElementById("turn");
|
0
|
287 |
ui.score.clear = function(brd) {
|
|
288 |
scoreDom.innerHTML = '0';
|
|
289 |
maxDom.innerHTML = '0';
|
1
|
290 |
speedDom.innerHTML = '0';
|
|
291 |
turnDom.innerHTML = '0';
|
0
|
292 |
}
|
|
293 |
ui.score.update = function(brd) {
|
|
294 |
var score = board.score(brd);
|
|
295 |
scoreDom.innerHTML = '' + score.score;
|
|
296 |
maxDom.innerHTML = '' + score.max;
|
|
297 |
}
|
1
|
298 |
ui.score.speed = function(speed, turn) {
|
|
299 |
speedDom.innerHTML = '' + speed;
|
|
300 |
turnDom.innerHTML = '' + turn;
|
|
301 |
}
|
0
|
302 |
|
|
303 |
function start() {
|
|
304 |
ui.score.clear();
|
|
305 |
ui.message.clear();
|
|
306 |
board.current = board.create();
|
|
307 |
board.random(board.current);
|
|
308 |
ui.board.update(board.current);
|
|
309 |
}
|
|
310 |
document.getElementById("start").addEventListener("click", start);
|
|
311 |
|
|
312 |
function up() {
|
|
313 |
var updated = board.move.up(board.current);
|
|
314 |
if (updated) {
|
|
315 |
board.random(board.current);
|
|
316 |
ui.board.update(board.current);
|
|
317 |
ui.score.update(board.current);
|
|
318 |
}
|
|
319 |
}
|
|
320 |
document.getElementById("up").addEventListener("click", up);
|
|
321 |
function down() {
|
|
322 |
var updated = board.move.down(board.current);
|
|
323 |
if (updated) {
|
|
324 |
board.random(board.current);
|
|
325 |
ui.board.update(board.current);
|
|
326 |
ui.score.update(board.current);
|
|
327 |
}
|
|
328 |
}
|
|
329 |
document.getElementById("down").addEventListener("click", down);
|
|
330 |
function left() {
|
|
331 |
var updated = board.move.left(board.current);
|
|
332 |
if (updated) {
|
|
333 |
board.random(board.current);
|
|
334 |
ui.board.update(board.current);
|
|
335 |
ui.score.update(board.current);
|
|
336 |
}
|
|
337 |
}
|
|
338 |
document.getElementById("left").addEventListener("click", left);
|
|
339 |
function right() {
|
|
340 |
var updated = board.move.right(board.current);
|
|
341 |
if (updated) {
|
|
342 |
board.random(board.current);
|
|
343 |
ui.board.update(board.current);
|
|
344 |
ui.score.update(board.current);
|
|
345 |
}
|
|
346 |
}
|
|
347 |
document.getElementById("right").addEventListener("click", right);
|
|
348 |
|
|
349 |
document.body.addEventListener("keydown", function(event) {
|
|
350 |
var key = event.keyCode || event.which;
|
|
351 |
switch (key) {
|
|
352 |
case 38: up(); break;
|
|
353 |
case 40: down(); break;
|
|
354 |
case 37: left(); break;
|
|
355 |
case 39: right(); break;
|
|
356 |
}
|
|
357 |
return false;
|
|
358 |
});
|
|
359 |
|
|
360 |
ui.message = {};
|
|
361 |
var messageDom = document.getElementById("message-area");
|
|
362 |
ui.message.clear = function() {
|
|
363 |
messageDom.innerHTML = "";
|
|
364 |
}
|
|
365 |
ui.message.set = function(msg) {
|
|
366 |
messageDom.innerHTML = msg;
|
|
367 |
}
|
|
368 |
|
|
369 |
function step() {
|
|
370 |
ui.message.clear();
|
|
371 |
if (board.gameOver(board.current)) {
|
|
372 |
ui.message.set("Game over!");
|
|
373 |
return;
|
|
374 |
}
|
|
375 |
var tmpBrd = board.create();
|
|
376 |
board.copy(board.current, tmpBrd);
|
|
377 |
var fn = ai.current(tmpBrd);
|
|
378 |
if (typeof fn === 'undefined') {
|
|
379 |
ui.message.set("I don't know how to move!");
|
|
380 |
return;
|
|
381 |
}
|
|
382 |
var updated = board.move[fn].call(null, board.current);
|
|
383 |
if (updated) {
|
|
384 |
board.random(board.current);
|
|
385 |
ui.board.update(board.current);
|
|
386 |
ui.score.update(board.current);
|
|
387 |
} else {
|
|
388 |
ui.message.set("Wrong move!");
|
|
389 |
}
|
|
390 |
}
|
|
391 |
document.getElementById("step").addEventListener("click", step);
|
|
392 |
|
|
393 |
function finish() {
|
|
394 |
ui.message.clear();
|
1
|
395 |
var step = 0;
|
|
396 |
var tsFrom = new Date().getTime();
|
0
|
397 |
while (!board.gameOver(board.current)) {
|
|
398 |
var tmpBrd = board.create();
|
|
399 |
board.copy(board.current, tmpBrd);
|
|
400 |
var fn = ai.current(tmpBrd);
|
|
401 |
if (typeof fn === 'undefined') {
|
|
402 |
ui.message.set("I don't know how to move!");
|
|
403 |
return;
|
|
404 |
}
|
|
405 |
var updated = board.move[fn].call(null, board.current);
|
|
406 |
if (updated) {
|
|
407 |
board.random(board.current);
|
|
408 |
} else {
|
|
409 |
ui.board.update(board.current);
|
|
410 |
ui.score.update(board.current);
|
|
411 |
ui.message.set("Wrong move!");
|
|
412 |
return;
|
|
413 |
}
|
1
|
414 |
step++;
|
0
|
415 |
}
|
1
|
416 |
var tsTo = new Date().getTime();
|
0
|
417 |
ui.board.update(board.current);
|
|
418 |
ui.score.update(board.current);
|
1
|
419 |
ui.score.speed(step*1000.0/(tsTo-tsFrom), step);
|
0
|
420 |
ui.message.set("Game over!");
|
|
421 |
}
|
|
422 |
document.getElementById("finish").addEventListener("click", finish);
|
|
423 |
|
|
424 |
var ai = {};
|
|
425 |
|
|
426 |
ai.random = function(brd) {
|
|
427 |
var tmpBrd = board.create();
|
|
428 |
do {
|
|
429 |
var action = ["up", "down", "left", "right"][Math.floor(Math.random()*4)];
|
|
430 |
board.copy(brd, tmpBrd);
|
|
431 |
} while (!board.move[action](tmpBrd));
|
|
432 |
return action;
|
|
433 |
}
|
|
434 |
document.getElementById("ai-random").addEventListener("click", function() {
|
|
435 |
ai.current = ai.random;
|
|
436 |
});
|
|
437 |
|
|
438 |
ai.nextMaxScore = function(brd) {
|
|
439 |
var tmpBrd = board.create();
|
|
440 |
board.copy(brd, tmpBrd);
|
|
441 |
var maxScore = -1;
|
|
442 |
var action;
|
|
443 |
if (board.move.up(tmpBrd)) {
|
|
444 |
maxScore = board.score(tmpBrd).score;
|
|
445 |
action = "up";
|
|
446 |
}
|
|
447 |
board.copy(brd, tmpBrd);
|
|
448 |
if (board.move.left(tmpBrd)) {
|
|
449 |
var score = board.score(tmpBrd).score;
|
|
450 |
if (maxScore < score) {
|
|
451 |
action = "left";
|
|
452 |
maxScore = score;
|
|
453 |
}
|
|
454 |
}
|
|
455 |
board.copy(brd, tmpBrd);
|
|
456 |
if (board.move.down(tmpBrd)) {
|
|
457 |
var score = board.score(tmpBrd).score;
|
|
458 |
if (maxScore < score) {
|
|
459 |
action = "down";
|
|
460 |
maxScore = score;
|
|
461 |
}
|
|
462 |
}
|
|
463 |
board.copy(brd, tmpBrd);
|
|
464 |
if (board.move.right(tmpBrd)) {
|
|
465 |
var score = board.score(tmpBrd).score;
|
|
466 |
if (maxScore < score) {
|
|
467 |
action = "right";
|
|
468 |
maxScore = score;
|
|
469 |
}
|
|
470 |
}
|
|
471 |
return action;
|
|
472 |
}
|
|
473 |
document.getElementById("ai-next-max-score").addEventListener("click", function() {
|
|
474 |
ai.current = ai.nextMaxScore;
|
|
475 |
});
|
|
476 |
|
|
477 |
|
|
478 |
ai.nextMaxValue = function(brd) {
|
|
479 |
var tmpBrd = board.create();
|
|
480 |
board.copy(brd, tmpBrd);
|
|
481 |
var maxMax = -1;
|
|
482 |
var action;
|
|
483 |
if (board.move.up(tmpBrd)) {
|
|
484 |
maxMax = board.score(tmpBrd).max;
|
|
485 |
action = "up";
|
|
486 |
}
|
|
487 |
board.copy(brd, tmpBrd);
|
|
488 |
if (board.move.left(tmpBrd)) {
|
|
489 |
var max = board.score(tmpBrd).max;
|
|
490 |
if (maxMax < max) {
|
|
491 |
action = "left";
|
|
492 |
maxMax = max;
|
|
493 |
}
|
|
494 |
}
|
|
495 |
board.copy(brd, tmpBrd);
|
|
496 |
if (board.move.down(tmpBrd)) {
|
|
497 |
var max = board.score(tmpBrd).max;
|
|
498 |
if (maxMax < max) {
|
|
499 |
action = "down";
|
|
500 |
maxMax = max;
|
|
501 |
}
|
|
502 |
}
|
|
503 |
board.copy(brd, tmpBrd);
|
|
504 |
if (board.move.right(tmpBrd)) {
|
|
505 |
var max = board.score(tmpBrd).max;
|
|
506 |
if (maxMax < max) {
|
|
507 |
action = "right";
|
|
508 |
maxMax = max;
|
|
509 |
}
|
|
510 |
}
|
|
511 |
return action;
|
|
512 |
}
|
|
513 |
document.getElementById("ai-next-max-value").addEventListener("click", function() {
|
|
514 |
ai.current = ai.nextMaxValue;
|
|
515 |
});
|
|
516 |
|
|
517 |
</script>
|
|
518 |
|
|
519 |
</body>
|
|
520 |
</html>
|