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