author | Oleksandr Gavenko <gavenkoa@gmail.com> |
Sun, 07 Sep 2014 03:33:33 +0300 | |
changeset 12 | a9a44cfc3e08 |
parent 11 | 2264f7a3f7e0 |
child 13 | b3bfa9d1b537 |
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> |
14 |
body { |
|
15 |
width: 100%; |
|
16 |
} |
|
4 | 17 |
h1, div.area { |
0 | 18 |
text-align: center; |
4 | 19 |
margin: 10px auto; |
0 | 20 |
} |
21 |
#board { |
|
22 |
margin: 10px auto; |
|
23 |
} |
|
24 |
#board td { |
|
25 |
width: 40px; |
|
26 |
height: 40px; |
|
27 |
border: 1px solid red; |
|
28 |
margin: 0; |
|
29 |
text-align: center; |
|
30 |
} |
|
4 | 31 |
.report > .name { |
32 |
font-weight: bold; |
|
33 |
} |
|
0 | 34 |
</style> |
35 |
</head> |
|
36 |
<body> |
|
37 |
||
38 |
<h1>2048</h1> |
|
39 |
||
11
2264f7a3f7e0
Swap score parameters.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
10
diff
changeset
|
40 |
<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 | 41 |
|
4 | 42 |
<div id="message-area" class="area"></div> |
0 | 43 |
|
44 |
<table id="board"> |
|
45 |
<tr> |
|
46 |
<td></td> |
|
47 |
<td></td> |
|
48 |
<td></td> |
|
49 |
<td></td> |
|
50 |
</tr> |
|
51 |
<tr> |
|
52 |
<td></td> |
|
53 |
<td></td> |
|
54 |
<td></td> |
|
55 |
<td></td> |
|
56 |
</tr> |
|
57 |
<tr> |
|
58 |
<td></td> |
|
59 |
<td></td> |
|
60 |
<td></td> |
|
61 |
<td></td> |
|
62 |
</tr> |
|
63 |
<tr> |
|
64 |
<td></td> |
|
65 |
<td></td> |
|
66 |
<td></td> |
|
67 |
<td></td> |
|
68 |
</tr> |
|
69 |
</table> |
|
70 |
||
4 | 71 |
<div id="control-area" class="area"> |
0 | 72 |
<div> |
73 |
<button id="start">Start</button> |
|
74 |
<button id="step">Step</button> |
|
75 |
<button id="loop">Loop</button> |
|
76 |
<button id="finish">Finish</button> |
|
77 |
</div> |
|
78 |
<div> |
|
79 |
<button id="left">left</button> |
|
80 |
<button id="up">up</button> |
|
81 |
<button id="down">down</button> |
|
82 |
<button id="right">right</button> |
|
12
a9a44cfc3e08
Moves for 2d array board + test toolkit.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
11
diff
changeset
|
83 |
<button id="test">test</button> |
0 | 84 |
</div> |
85 |
<h1>AI</h1> |
|
86 |
<div> |
|
87 |
<button id="ai-random">random</button> |
|
88 |
<button id="ai-next-max-score">next max score</button> |
|
89 |
<button id="ai-next-max-value">next max value</button> |
|
90 |
</div> |
|
91 |
</div> |
|
92 |
||
4 | 93 |
<div id="report-area" class="area"> |
94 |
<h1>Reports</h1> |
|
95 |
<div class="report" class="area"> |
|
96 |
<div class="name">next max score</div> |
|
97 |
<table> |
|
98 |
<tr> |
|
99 |
<th>Max</th> |
|
100 |
<th>Count</th> |
|
101 |
<th>%</th> |
|
102 |
</tr> |
|
103 |
<tr> |
|
104 |
<td>128</td><td>8</td><td><div style="background: red; width: 100px">x</div></td> |
|
105 |
</tr> |
|
106 |
<tr> |
|
107 |
<td>256</td><td>4</td><td><div style="background: red; width: 50px">x</div></td> |
|
108 |
</tr> |
|
109 |
<tr> |
|
110 |
<td>512</td><td>12</td><td><div style="background: red; width: 150px">x</div></td> |
|
111 |
</tr> |
|
112 |
</table> |
|
113 |
</div> |
|
114 |
</div> |
|
115 |
||
0 | 116 |
<script> |
117 |
"use strict"; |
|
118 |
||
119 |
var boardDom = document.getElementById("board"); |
|
120 |
var ui = {}; |
|
121 |
ui.board = {}; |
|
122 |
ui.board.set = function(i, j, val) { |
|
123 |
boardDom.querySelectorAll("tr")[i].querySelectorAll("td")[j].innerHTML = val; |
|
124 |
} |
|
125 |
ui.board.update = function(brd) { |
|
126 |
for (var i = 0; i < 4; i++) { |
|
127 |
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
|
128 |
ui.board.set(i, j, (brd[i][j] >= 1) ? brd[i][j] : ""); |
0 | 129 |
} |
130 |
} |
|
131 |
} |
|
132 |
ui.score = {}; |
|
133 |
var scoreDom = document.getElementById("score"); |
|
134 |
var maxDom = document.getElementById("max"); |
|
1 | 135 |
var speedDom = document.getElementById("speed"); |
136 |
var turnDom = document.getElementById("turn"); |
|
0 | 137 |
ui.score.clear = function(brd) { |
138 |
scoreDom.innerHTML = '0'; |
|
139 |
maxDom.innerHTML = '0'; |
|
1 | 140 |
speedDom.innerHTML = '0'; |
141 |
turnDom.innerHTML = '0'; |
|
0 | 142 |
} |
143 |
ui.score.update = function(brd) { |
|
144 |
var score = board.score(brd); |
|
145 |
scoreDom.innerHTML = '' + score.score; |
|
146 |
maxDom.innerHTML = '' + score.max; |
|
147 |
} |
|
1 | 148 |
ui.score.speed = function(speed, turn) { |
149 |
speedDom.innerHTML = '' + speed; |
|
150 |
turnDom.innerHTML = '' + turn; |
|
151 |
} |
|
0 | 152 |
|
153 |
function start() { |
|
154 |
ui.score.clear(); |
|
155 |
ui.message.clear(); |
|
156 |
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
|
157 |
board.putRandom(board.current); |
0 | 158 |
ui.board.update(board.current); |
159 |
} |
|
160 |
document.getElementById("start").addEventListener("click", start); |
|
161 |
||
162 |
function up() { |
|
163 |
var updated = board.move.up(board.current); |
|
164 |
if (updated) { |
|
7
d8106c8c0481
Rename methods and use sequential numbers instead of power of 2.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
4
diff
changeset
|
165 |
board.putRandom(board.current); |
0 | 166 |
ui.board.update(board.current); |
167 |
ui.score.update(board.current); |
|
168 |
} |
|
169 |
} |
|
170 |
document.getElementById("up").addEventListener("click", up); |
|
171 |
function down() { |
|
172 |
var updated = board.move.down(board.current); |
|
173 |
if (updated) { |
|
7
d8106c8c0481
Rename methods and use sequential numbers instead of power of 2.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
4
diff
changeset
|
174 |
board.putRandom(board.current); |
0 | 175 |
ui.board.update(board.current); |
176 |
ui.score.update(board.current); |
|
177 |
} |
|
178 |
} |
|
179 |
document.getElementById("down").addEventListener("click", down); |
|
180 |
function left() { |
|
181 |
var updated = board.move.left(board.current); |
|
182 |
if (updated) { |
|
7
d8106c8c0481
Rename methods and use sequential numbers instead of power of 2.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
4
diff
changeset
|
183 |
board.putRandom(board.current); |
0 | 184 |
ui.board.update(board.current); |
185 |
ui.score.update(board.current); |
|
186 |
} |
|
187 |
} |
|
188 |
document.getElementById("left").addEventListener("click", left); |
|
189 |
function right() { |
|
190 |
var updated = board.move.right(board.current); |
|
191 |
if (updated) { |
|
7
d8106c8c0481
Rename methods and use sequential numbers instead of power of 2.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
4
diff
changeset
|
192 |
board.putRandom(board.current); |
0 | 193 |
ui.board.update(board.current); |
194 |
ui.score.update(board.current); |
|
195 |
} |
|
196 |
} |
|
197 |
document.getElementById("right").addEventListener("click", right); |
|
198 |
||
199 |
document.body.addEventListener("keydown", function(event) { |
|
200 |
var key = event.keyCode || event.which; |
|
201 |
switch (key) { |
|
202 |
case 38: up(); break; |
|
203 |
case 40: down(); break; |
|
204 |
case 37: left(); break; |
|
205 |
case 39: right(); break; |
|
206 |
} |
|
207 |
return false; |
|
208 |
}); |
|
12
a9a44cfc3e08
Moves for 2d array board + test toolkit.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
11
diff
changeset
|
209 |
|
a9a44cfc3e08
Moves for 2d array board + test toolkit.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
11
diff
changeset
|
210 |
document.getElementById("test").addEventListener("click", function() { |
a9a44cfc3e08
Moves for 2d array board + test toolkit.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
11
diff
changeset
|
211 |
console.log("x1: %o", ui.brdEngine); |
a9a44cfc3e08
Moves for 2d array board + test toolkit.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
11
diff
changeset
|
212 |
board.move.replaceByBoardJS(ui.brdEngine); |
a9a44cfc3e08
Moves for 2d array board + test toolkit.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
11
diff
changeset
|
213 |
}); |
0 | 214 |
|
215 |
ui.message = {}; |
|
216 |
var messageDom = document.getElementById("message-area"); |
|
217 |
ui.message.clear = function() { |
|
218 |
messageDom.innerHTML = ""; |
|
219 |
} |
|
220 |
ui.message.set = function(msg) { |
|
221 |
messageDom.innerHTML = msg; |
|
222 |
} |
|
223 |
||
224 |
function step() { |
|
225 |
ui.message.clear(); |
|
226 |
if (board.gameOver(board.current)) { |
|
227 |
ui.message.set("Game over!"); |
|
228 |
return; |
|
229 |
} |
|
230 |
var tmpBrd = board.create(); |
|
231 |
board.copy(board.current, tmpBrd); |
|
10
70ece7f758a0
Move rule and AI engine to separate files.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
7
diff
changeset
|
232 |
var move = ui.ai.analyse(tmpBrd); |
70ece7f758a0
Move rule and AI engine to separate files.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
7
diff
changeset
|
233 |
if (typeof move === 'undefined') { |
0 | 234 |
ui.message.set("I don't know how to move!"); |
235 |
return; |
|
236 |
} |
|
10
70ece7f758a0
Move rule and AI engine to separate files.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
7
diff
changeset
|
237 |
var updated = board.move[move].call(null, board.current); |
0 | 238 |
if (updated) { |
7
d8106c8c0481
Rename methods and use sequential numbers instead of power of 2.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
4
diff
changeset
|
239 |
board.putRandom(board.current); |
0 | 240 |
ui.board.update(board.current); |
241 |
ui.score.update(board.current); |
|
242 |
} else { |
|
243 |
ui.message.set("Wrong move!"); |
|
244 |
} |
|
245 |
} |
|
246 |
document.getElementById("step").addEventListener("click", step); |
|
247 |
||
248 |
function finish() { |
|
249 |
ui.message.clear(); |
|
1 | 250 |
var step = 0; |
251 |
var tsFrom = new Date().getTime(); |
|
0 | 252 |
while (!board.gameOver(board.current)) { |
253 |
var tmpBrd = board.create(); |
|
254 |
board.copy(board.current, tmpBrd); |
|
10
70ece7f758a0
Move rule and AI engine to separate files.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
7
diff
changeset
|
255 |
var move = ui.ai.analyse(tmpBrd); |
70ece7f758a0
Move rule and AI engine to separate files.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
7
diff
changeset
|
256 |
if (typeof move === 'undefined') { |
0 | 257 |
ui.message.set("I don't know how to move!"); |
258 |
return; |
|
259 |
} |
|
10
70ece7f758a0
Move rule and AI engine to separate files.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
7
diff
changeset
|
260 |
var updated = board.move[move].call(null, board.current); |
0 | 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 |
} else { |
264 |
ui.board.update(board.current); |
|
265 |
ui.score.update(board.current); |
|
266 |
ui.message.set("Wrong move!"); |
|
267 |
return; |
|
268 |
} |
|
1 | 269 |
step++; |
0 | 270 |
} |
1 | 271 |
var tsTo = new Date().getTime(); |
0 | 272 |
ui.board.update(board.current); |
273 |
ui.score.update(board.current); |
|
1 | 274 |
ui.score.speed(step*1000.0/(tsTo-tsFrom), step); |
0 | 275 |
ui.message.set("Game over!"); |
276 |
} |
|
277 |
document.getElementById("finish").addEventListener("click", finish); |
|
278 |
||
10
70ece7f758a0
Move rule and AI engine to separate files.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
7
diff
changeset
|
279 |
//////////////////////////////////////////////////////////////// |
70ece7f758a0
Move rule and AI engine to separate files.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
7
diff
changeset
|
280 |
// Register AIs. |
0 | 281 |
|
10
70ece7f758a0
Move rule and AI engine to separate files.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
7
diff
changeset
|
282 |
ui.brdEngine = BoardArr2d; // TODO make user selectable |
0 | 283 |
|
10
70ece7f758a0
Move rule and AI engine to separate files.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
7
diff
changeset
|
284 |
document.getElementById("ai-random").addEventListener("click", function() { |
70ece7f758a0
Move rule and AI engine to separate files.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
7
diff
changeset
|
285 |
ui.ai = new ai.random(ui.brdEngine); |
0 | 286 |
}); |
10
70ece7f758a0
Move rule and AI engine to separate files.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
7
diff
changeset
|
287 |
document.getElementById("ai-next-max-score").addEventListener("click", function() { |
70ece7f758a0
Move rule and AI engine to separate files.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
7
diff
changeset
|
288 |
ui.ai = new ai.nextMaxScore(ui.brdEngine); |
70ece7f758a0
Move rule and AI engine to separate files.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
7
diff
changeset
|
289 |
}); |
0 | 290 |
document.getElementById("ai-next-max-value").addEventListener("click", function() { |
10
70ece7f758a0
Move rule and AI engine to separate files.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
7
diff
changeset
|
291 |
ui.ai = new ai.nextMaxValue(ui.brdEngine); |
0 | 292 |
}); |
293 |
||
294 |
</script> |
|
295 |
||
296 |
</body> |
|
297 |
</html> |