author | Oleksandr Gavenko <gavenkoa@gmail.com> |
Sun, 07 Sep 2014 00:19:00 +0300 | |
changeset 10 | 70ece7f758a0 |
parent 7 | d8106c8c0481 |
child 11 | 2264f7a3f7e0 |
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 |
||
4 | 40 |
<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 | 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> |
|
83 |
</div> |
|
84 |
<h1>AI</h1> |
|
85 |
<div> |
|
86 |
<button id="ai-random">random</button> |
|
87 |
<button id="ai-next-max-score">next max score</button> |
|
88 |
<button id="ai-next-max-value">next max value</button> |
|
89 |
</div> |
|
90 |
</div> |
|
91 |
||
4 | 92 |
<div id="report-area" class="area"> |
93 |
<h1>Reports</h1> |
|
94 |
<div class="report" class="area"> |
|
95 |
<div class="name">next max score</div> |
|
96 |
<table> |
|
97 |
<tr> |
|
98 |
<th>Max</th> |
|
99 |
<th>Count</th> |
|
100 |
<th>%</th> |
|
101 |
</tr> |
|
102 |
<tr> |
|
103 |
<td>128</td><td>8</td><td><div style="background: red; width: 100px">x</div></td> |
|
104 |
</tr> |
|
105 |
<tr> |
|
106 |
<td>256</td><td>4</td><td><div style="background: red; width: 50px">x</div></td> |
|
107 |
</tr> |
|
108 |
<tr> |
|
109 |
<td>512</td><td>12</td><td><div style="background: red; width: 150px">x</div></td> |
|
110 |
</tr> |
|
111 |
</table> |
|
112 |
</div> |
|
113 |
</div> |
|
114 |
||
0 | 115 |
<script> |
116 |
"use strict"; |
|
117 |
||
118 |
var boardDom = document.getElementById("board"); |
|
119 |
var ui = {}; |
|
120 |
ui.board = {}; |
|
121 |
ui.board.set = function(i, j, val) { |
|
122 |
boardDom.querySelectorAll("tr")[i].querySelectorAll("td")[j].innerHTML = val; |
|
123 |
} |
|
124 |
ui.board.update = function(brd) { |
|
125 |
for (var i = 0; i < 4; i++) { |
|
126 |
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
|
127 |
ui.board.set(i, j, (brd[i][j] >= 1) ? brd[i][j] : ""); |
0 | 128 |
} |
129 |
} |
|
130 |
} |
|
131 |
ui.score = {}; |
|
132 |
var scoreDom = document.getElementById("score"); |
|
133 |
var maxDom = document.getElementById("max"); |
|
1 | 134 |
var speedDom = document.getElementById("speed"); |
135 |
var turnDom = document.getElementById("turn"); |
|
0 | 136 |
ui.score.clear = function(brd) { |
137 |
scoreDom.innerHTML = '0'; |
|
138 |
maxDom.innerHTML = '0'; |
|
1 | 139 |
speedDom.innerHTML = '0'; |
140 |
turnDom.innerHTML = '0'; |
|
0 | 141 |
} |
142 |
ui.score.update = function(brd) { |
|
143 |
var score = board.score(brd); |
|
144 |
scoreDom.innerHTML = '' + score.score; |
|
145 |
maxDom.innerHTML = '' + score.max; |
|
146 |
} |
|
1 | 147 |
ui.score.speed = function(speed, turn) { |
148 |
speedDom.innerHTML = '' + speed; |
|
149 |
turnDom.innerHTML = '' + turn; |
|
150 |
} |
|
0 | 151 |
|
152 |
function start() { |
|
153 |
ui.score.clear(); |
|
154 |
ui.message.clear(); |
|
155 |
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
|
156 |
board.putRandom(board.current); |
0 | 157 |
ui.board.update(board.current); |
158 |
} |
|
159 |
document.getElementById("start").addEventListener("click", start); |
|
160 |
||
161 |
function up() { |
|
162 |
var updated = board.move.up(board.current); |
|
163 |
if (updated) { |
|
7
d8106c8c0481
Rename methods and use sequential numbers instead of power of 2.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
4
diff
changeset
|
164 |
board.putRandom(board.current); |
0 | 165 |
ui.board.update(board.current); |
166 |
ui.score.update(board.current); |
|
167 |
} |
|
168 |
} |
|
169 |
document.getElementById("up").addEventListener("click", up); |
|
170 |
function down() { |
|
171 |
var updated = board.move.down(board.current); |
|
172 |
if (updated) { |
|
7
d8106c8c0481
Rename methods and use sequential numbers instead of power of 2.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
4
diff
changeset
|
173 |
board.putRandom(board.current); |
0 | 174 |
ui.board.update(board.current); |
175 |
ui.score.update(board.current); |
|
176 |
} |
|
177 |
} |
|
178 |
document.getElementById("down").addEventListener("click", down); |
|
179 |
function left() { |
|
180 |
var updated = board.move.left(board.current); |
|
181 |
if (updated) { |
|
7
d8106c8c0481
Rename methods and use sequential numbers instead of power of 2.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
4
diff
changeset
|
182 |
board.putRandom(board.current); |
0 | 183 |
ui.board.update(board.current); |
184 |
ui.score.update(board.current); |
|
185 |
} |
|
186 |
} |
|
187 |
document.getElementById("left").addEventListener("click", left); |
|
188 |
function right() { |
|
189 |
var updated = board.move.right(board.current); |
|
190 |
if (updated) { |
|
7
d8106c8c0481
Rename methods and use sequential numbers instead of power of 2.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
4
diff
changeset
|
191 |
board.putRandom(board.current); |
0 | 192 |
ui.board.update(board.current); |
193 |
ui.score.update(board.current); |
|
194 |
} |
|
195 |
} |
|
196 |
document.getElementById("right").addEventListener("click", right); |
|
197 |
||
198 |
document.body.addEventListener("keydown", function(event) { |
|
199 |
var key = event.keyCode || event.which; |
|
200 |
switch (key) { |
|
201 |
case 38: up(); break; |
|
202 |
case 40: down(); break; |
|
203 |
case 37: left(); break; |
|
204 |
case 39: right(); break; |
|
205 |
} |
|
206 |
return false; |
|
207 |
}); |
|
208 |
||
209 |
ui.message = {}; |
|
210 |
var messageDom = document.getElementById("message-area"); |
|
211 |
ui.message.clear = function() { |
|
212 |
messageDom.innerHTML = ""; |
|
213 |
} |
|
214 |
ui.message.set = function(msg) { |
|
215 |
messageDom.innerHTML = msg; |
|
216 |
} |
|
217 |
||
218 |
function step() { |
|
219 |
ui.message.clear(); |
|
220 |
if (board.gameOver(board.current)) { |
|
221 |
ui.message.set("Game over!"); |
|
222 |
return; |
|
223 |
} |
|
224 |
var tmpBrd = board.create(); |
|
225 |
board.copy(board.current, tmpBrd); |
|
10
70ece7f758a0
Move rule and AI engine to separate files.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
7
diff
changeset
|
226 |
var move = ui.ai.analyse(tmpBrd); |
70ece7f758a0
Move rule and AI engine to separate files.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
7
diff
changeset
|
227 |
if (typeof move === 'undefined') { |
0 | 228 |
ui.message.set("I don't know how to move!"); |
229 |
return; |
|
230 |
} |
|
10
70ece7f758a0
Move rule and AI engine to separate files.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
7
diff
changeset
|
231 |
var updated = board.move[move].call(null, board.current); |
0 | 232 |
if (updated) { |
7
d8106c8c0481
Rename methods and use sequential numbers instead of power of 2.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
4
diff
changeset
|
233 |
board.putRandom(board.current); |
0 | 234 |
ui.board.update(board.current); |
235 |
ui.score.update(board.current); |
|
236 |
} else { |
|
237 |
ui.message.set("Wrong move!"); |
|
238 |
} |
|
239 |
} |
|
240 |
document.getElementById("step").addEventListener("click", step); |
|
241 |
||
242 |
function finish() { |
|
243 |
ui.message.clear(); |
|
1 | 244 |
var step = 0; |
245 |
var tsFrom = new Date().getTime(); |
|
0 | 246 |
while (!board.gameOver(board.current)) { |
247 |
var tmpBrd = board.create(); |
|
248 |
board.copy(board.current, tmpBrd); |
|
10
70ece7f758a0
Move rule and AI engine to separate files.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
7
diff
changeset
|
249 |
var move = ui.ai.analyse(tmpBrd); |
70ece7f758a0
Move rule and AI engine to separate files.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
7
diff
changeset
|
250 |
if (typeof move === 'undefined') { |
0 | 251 |
ui.message.set("I don't know how to move!"); |
252 |
return; |
|
253 |
} |
|
10
70ece7f758a0
Move rule and AI engine to separate files.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
7
diff
changeset
|
254 |
var updated = board.move[move].call(null, board.current); |
0 | 255 |
if (updated) { |
7
d8106c8c0481
Rename methods and use sequential numbers instead of power of 2.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
4
diff
changeset
|
256 |
board.putRandom(board.current); |
0 | 257 |
} else { |
258 |
ui.board.update(board.current); |
|
259 |
ui.score.update(board.current); |
|
260 |
ui.message.set("Wrong move!"); |
|
261 |
return; |
|
262 |
} |
|
1 | 263 |
step++; |
0 | 264 |
} |
1 | 265 |
var tsTo = new Date().getTime(); |
0 | 266 |
ui.board.update(board.current); |
267 |
ui.score.update(board.current); |
|
1 | 268 |
ui.score.speed(step*1000.0/(tsTo-tsFrom), step); |
0 | 269 |
ui.message.set("Game over!"); |
270 |
} |
|
271 |
document.getElementById("finish").addEventListener("click", finish); |
|
272 |
||
10
70ece7f758a0
Move rule and AI engine to separate files.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
7
diff
changeset
|
273 |
//////////////////////////////////////////////////////////////// |
70ece7f758a0
Move rule and AI engine to separate files.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
7
diff
changeset
|
274 |
// Register AIs. |
0 | 275 |
|
10
70ece7f758a0
Move rule and AI engine to separate files.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
7
diff
changeset
|
276 |
ui.brdEngine = BoardArr2d; // TODO make user selectable |
0 | 277 |
|
10
70ece7f758a0
Move rule and AI engine to separate files.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
7
diff
changeset
|
278 |
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
|
279 |
ui.ai = new ai.random(ui.brdEngine); |
0 | 280 |
}); |
10
70ece7f758a0
Move rule and AI engine to separate files.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
7
diff
changeset
|
281 |
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
|
282 |
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
|
283 |
}); |
0 | 284 |
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
|
285 |
ui.ai = new ai.nextMaxValue(ui.brdEngine); |
0 | 286 |
}); |
287 |
||
288 |
</script> |
|
289 |
||
290 |
</body> |
|
291 |
</html> |