author | Oleksandr Gavenko <gavenkoa@gmail.com> |
Tue, 09 Sep 2014 19:37:44 +0300 | |
changeset 27 | 8f96d09a4d94 |
parent 26 | fba85224a8f0 |
child 30 | 1d19768ca71d |
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> |
27
8f96d09a4d94
Add Blind cycle AI. Add example of setting passing.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
26
diff
changeset
|
14 |
body { |
8f96d09a4d94
Add Blind cycle AI. Add example of setting passing.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
26
diff
changeset
|
15 |
font-size: 11pt; |
8f96d09a4d94
Add Blind cycle AI. Add example of setting passing.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
26
diff
changeset
|
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 |
} |
|
25
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
34 |
div.ai { |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
35 |
border: 1px red solid; |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
36 |
margin-top: 20px; |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
37 |
padding: 2px; |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
38 |
position: relative; |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
39 |
background-color: cornsilk; |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
40 |
} |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
41 |
div.ai.enabled { |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
42 |
background-color: wheat; |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
43 |
} |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
44 |
div.ai > h5 { |
27
8f96d09a4d94
Add Blind cycle AI. Add example of setting passing.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
26
diff
changeset
|
45 |
margin: 2px 0 2px 6em; |
25
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
46 |
} |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
47 |
button.ai { |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
48 |
position: absolute; |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
49 |
left: 1em; |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
50 |
top: -1em; |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
51 |
} |
27
8f96d09a4d94
Add Blind cycle AI. Add example of setting passing.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
26
diff
changeset
|
52 |
div.ai > div.option { |
8f96d09a4d94
Add Blind cycle AI. Add example of setting passing.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
26
diff
changeset
|
53 |
display: inline-block; |
8f96d09a4d94
Add Blind cycle AI. Add example of setting passing.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
26
diff
changeset
|
54 |
float: left; |
8f96d09a4d94
Add Blind cycle AI. Add example of setting passing.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
26
diff
changeset
|
55 |
margin: 1px 4px; |
8f96d09a4d94
Add Blind cycle AI. Add example of setting passing.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
26
diff
changeset
|
56 |
} |
8f96d09a4d94
Add Blind cycle AI. Add example of setting passing.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
26
diff
changeset
|
57 |
.clearfix { |
8f96d09a4d94
Add Blind cycle AI. Add example of setting passing.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
26
diff
changeset
|
58 |
clear: both; |
8f96d09a4d94
Add Blind cycle AI. Add example of setting passing.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
26
diff
changeset
|
59 |
} |
0 | 60 |
</style> |
61 |
</head> |
|
62 |
<body> |
|
63 |
||
64 |
<h1>2048</h1> |
|
65 |
||
11
2264f7a3f7e0
Swap score parameters.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
10
diff
changeset
|
66 |
<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 | 67 |
|
4 | 68 |
<div id="message-area" class="area"></div> |
0 | 69 |
|
70 |
<table id="board"> |
|
71 |
<tr> |
|
72 |
<td></td> |
|
73 |
<td></td> |
|
74 |
<td></td> |
|
75 |
<td></td> |
|
76 |
</tr> |
|
77 |
<tr> |
|
78 |
<td></td> |
|
79 |
<td></td> |
|
80 |
<td></td> |
|
81 |
<td></td> |
|
82 |
</tr> |
|
83 |
<tr> |
|
84 |
<td></td> |
|
85 |
<td></td> |
|
86 |
<td></td> |
|
87 |
<td></td> |
|
88 |
</tr> |
|
89 |
<tr> |
|
90 |
<td></td> |
|
91 |
<td></td> |
|
92 |
<td></td> |
|
93 |
<td></td> |
|
94 |
</tr> |
|
95 |
</table> |
|
96 |
||
4 | 97 |
<div id="control-area" class="area"> |
0 | 98 |
<div> |
99 |
<button id="start">Start</button> |
|
100 |
<button id="step">Step</button> |
|
101 |
<button id="loop">Loop</button> |
|
102 |
<button id="finish">Finish</button> |
|
103 |
</div> |
|
104 |
<div> |
|
105 |
<button id="left">left</button> |
|
106 |
<button id="up">up</button> |
|
107 |
<button id="down">down</button> |
|
108 |
<button id="right">right</button> |
|
12
a9a44cfc3e08
Moves for 2d array board + test toolkit.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
11
diff
changeset
|
109 |
<button id="test">test</button> |
0 | 110 |
</div> |
25
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
111 |
<h1>AIs</h1> |
0 | 112 |
<div> |
25
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
113 |
<div class="ai" id="ai-random"> |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
114 |
<button class="ai">enable</button> |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
115 |
<h5>bling random</h5> |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
116 |
</div> |
27
8f96d09a4d94
Add Blind cycle AI. Add example of setting passing.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
26
diff
changeset
|
117 |
<div class="ai" id="ai-blind-cycle"> |
25
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
118 |
<button class="ai">enable</button> |
27
8f96d09a4d94
Add Blind cycle AI. Add example of setting passing.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
26
diff
changeset
|
119 |
<h5>blind cycle</h5> |
8f96d09a4d94
Add Blind cycle AI. Add example of setting passing.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
26
diff
changeset
|
120 |
<div class="option"> |
8f96d09a4d94
Add Blind cycle AI. Add example of setting passing.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
26
diff
changeset
|
121 |
<input type="checkbox" name="clockwise"/> clockwise |
8f96d09a4d94
Add Blind cycle AI. Add example of setting passing.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
26
diff
changeset
|
122 |
</div> |
8f96d09a4d94
Add Blind cycle AI. Add example of setting passing.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
26
diff
changeset
|
123 |
<div class="option"> |
8f96d09a4d94
Add Blind cycle AI. Add example of setting passing.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
26
diff
changeset
|
124 |
<input type="checkbox" name="whilePossible"/> max move in one direction |
8f96d09a4d94
Add Blind cycle AI. Add example of setting passing.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
26
diff
changeset
|
125 |
</div> |
8f96d09a4d94
Add Blind cycle AI. Add example of setting passing.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
26
diff
changeset
|
126 |
<div class="clearfix"></div> |
25
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
127 |
</div> |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
128 |
<div class="ai" id="ai-next-max-value"> |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
129 |
<button class="ai">enable</button> |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
130 |
<h5>next merge makes max value</h5> |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
131 |
</div> |
26 | 132 |
<div class="ai" id="ai-deep-max-score"> |
25
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
133 |
<button class="ai">enable</button> |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
134 |
<h5>deep merges without simulation make max score</h5> |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
135 |
</div> |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
136 |
<div class="ai" id="ai-deep-max-score-corner"> |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
137 |
<button class="ai">enable</button> |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
138 |
<h5>deep merges without simulation make max score + bonus if max value at corner/edge</h5> |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
139 |
</div> |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
140 |
<div class="ai" id="ai-expectimax"> |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
141 |
<button class="ai">enable</button> |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
142 |
<h5>expectimax</h5> |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
143 |
</div> |
0 | 144 |
</div> |
145 |
</div> |
|
146 |
||
4 | 147 |
<div id="report-area" class="area"> |
148 |
<h1>Reports</h1> |
|
149 |
<div class="report" class="area"> |
|
150 |
<div class="name">next max score</div> |
|
151 |
<table> |
|
152 |
<tr> |
|
153 |
<th>Max</th> |
|
154 |
<th>Count</th> |
|
155 |
<th>%</th> |
|
156 |
</tr> |
|
157 |
<tr> |
|
158 |
<td>128</td><td>8</td><td><div style="background: red; width: 100px">x</div></td> |
|
159 |
</tr> |
|
160 |
<tr> |
|
161 |
<td>256</td><td>4</td><td><div style="background: red; width: 50px">x</div></td> |
|
162 |
</tr> |
|
163 |
<tr> |
|
164 |
<td>512</td><td>12</td><td><div style="background: red; width: 150px">x</div></td> |
|
165 |
</tr> |
|
166 |
</table> |
|
167 |
</div> |
|
168 |
</div> |
|
169 |
||
0 | 170 |
<script> |
171 |
"use strict"; |
|
172 |
||
173 |
var boardDom = document.getElementById("board"); |
|
174 |
var ui = {}; |
|
175 |
ui.board = {}; |
|
15
0fa97e430a9e
ui.board.set care about 'val' argument.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
14
diff
changeset
|
176 |
/* 'val' typesafe. */ |
0 | 177 |
ui.board.set = function(i, j, val) { |
16 | 178 |
if (typeof val !== 'number') |
15
0fa97e430a9e
ui.board.set care about 'val' argument.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
14
diff
changeset
|
179 |
val = 0; |
13
b3bfa9d1b537
Add color to board depending on values..
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
12
diff
changeset
|
180 |
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
|
181 |
if (0 < val && val < 15) { |
0fa97e430a9e
ui.board.set care about 'val' argument.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
14
diff
changeset
|
182 |
dom.innerHTML = val; |
0fa97e430a9e
ui.board.set care about 'val' argument.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
14
diff
changeset
|
183 |
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
|
184 |
} else { |
0fa97e430a9e
ui.board.set care about 'val' argument.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
14
diff
changeset
|
185 |
dom.innerHTML = ''; |
0fa97e430a9e
ui.board.set care about 'val' argument.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
14
diff
changeset
|
186 |
var rgb = 'white'; |
0fa97e430a9e
ui.board.set care about 'val' argument.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
14
diff
changeset
|
187 |
} |
13
b3bfa9d1b537
Add color to board depending on values..
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
12
diff
changeset
|
188 |
dom.style.backgroundColor = rgb; |
0 | 189 |
} |
190 |
ui.board.update = function(brd) { |
|
191 |
for (var i = 0; i < 4; i++) { |
|
192 |
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
|
193 |
ui.board.set(i, j, brd[i][j]); |
0 | 194 |
} |
195 |
} |
|
196 |
} |
|
17 | 197 |
ui.board.handleEdit = function(tdDom, i, j) { |
198 |
if (!tdDom.firstChild || tdDom.firstChild.nodeType === Node.TEXT_NODE) { |
|
199 |
if (tdDom.firstChild) |
|
200 |
tdDom.removeChild(tdDom.firstChild); |
|
201 |
var inputDom = document.createElement('input'); |
|
202 |
inputDom.value = board.current[i][j]; |
|
203 |
inputDom.style.width = '1em'; |
|
204 |
tdDom.appendChild(inputDom); |
|
205 |
inputDom.focus(); |
|
206 |
inputDom.select(); |
|
207 |
inputDom.addEventListener("blur", function(tdDom, i, j) { |
|
208 |
return function(event) { |
|
209 |
ui.board.handleEdit(tdDom, i, j); |
|
210 |
return false; |
|
211 |
} |
|
212 |
}(tdDom, i, j)); |
|
213 |
} else { |
|
214 |
var inputDom = tdDom.firstChild; |
|
215 |
var val = parseInt(inputDom.value); |
|
216 |
if (0 <= val && val < 15) { |
|
217 |
board.current[i][j] = val; |
|
218 |
ui.board.set(i, j, val); |
|
219 |
} else { |
|
220 |
ui.board.set(i, j, board.current[i][j]); |
|
221 |
} |
|
222 |
} |
|
223 |
} |
|
224 |
ui.board.enableEdit = function() { |
|
225 |
var trDoms = boardDom.querySelectorAll("tr"); |
|
226 |
for (var i = 0; i < 4; i++) { |
|
227 |
var tdDoms = trDoms[i].querySelectorAll("td"); |
|
228 |
for (var j = 0; j < 4; j++) { |
|
229 |
var tdDom = tdDoms[j]; |
|
230 |
tdDom.addEventListener("click", function(tdDom, i, j) { |
|
231 |
return function(event) { |
|
232 |
ui.board.handleEdit(tdDom, i, j); |
|
233 |
return false; |
|
234 |
} |
|
235 |
}(tdDom, i, j)); |
|
236 |
tdDom.addEventListener("keyup", function(tdDom, i, j) { |
|
237 |
return function(event) { |
|
238 |
if (event.keyCode === 13) |
|
239 |
ui.board.handleEdit(tdDom, i, j); |
|
240 |
} |
|
241 |
}(tdDom, i, j)); |
|
242 |
} |
|
243 |
} |
|
244 |
} |
|
245 |
ui.board.enableEdit(); |
|
246 |
||
0 | 247 |
ui.score = {}; |
248 |
var scoreDom = document.getElementById("score"); |
|
249 |
var maxDom = document.getElementById("max"); |
|
1 | 250 |
var speedDom = document.getElementById("speed"); |
251 |
var turnDom = document.getElementById("turn"); |
|
0 | 252 |
ui.score.clear = function(brd) { |
253 |
scoreDom.innerHTML = '0'; |
|
254 |
maxDom.innerHTML = '0'; |
|
1 | 255 |
speedDom.innerHTML = '0'; |
256 |
turnDom.innerHTML = '0'; |
|
0 | 257 |
} |
258 |
ui.score.update = function(brd) { |
|
259 |
var score = board.score(brd); |
|
260 |
scoreDom.innerHTML = '' + score.score; |
|
261 |
maxDom.innerHTML = '' + score.max; |
|
262 |
} |
|
1 | 263 |
ui.score.speed = function(speed, turn) { |
264 |
speedDom.innerHTML = '' + speed; |
|
265 |
turnDom.innerHTML = '' + turn; |
|
266 |
} |
|
0 | 267 |
|
268 |
function start() { |
|
269 |
ui.score.clear(); |
|
270 |
ui.message.clear(); |
|
271 |
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
|
272 |
board.putRandom(board.current); |
0 | 273 |
ui.board.update(board.current); |
274 |
} |
|
275 |
document.getElementById("start").addEventListener("click", start); |
|
276 |
||
277 |
function up() { |
|
278 |
var updated = board.move.up(board.current); |
|
279 |
if (updated) { |
|
7
d8106c8c0481
Rename methods and use sequential numbers instead of power of 2.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
4
diff
changeset
|
280 |
board.putRandom(board.current); |
0 | 281 |
ui.board.update(board.current); |
282 |
ui.score.update(board.current); |
|
283 |
} |
|
284 |
} |
|
285 |
document.getElementById("up").addEventListener("click", up); |
|
286 |
function down() { |
|
287 |
var updated = board.move.down(board.current); |
|
288 |
if (updated) { |
|
7
d8106c8c0481
Rename methods and use sequential numbers instead of power of 2.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
4
diff
changeset
|
289 |
board.putRandom(board.current); |
0 | 290 |
ui.board.update(board.current); |
291 |
ui.score.update(board.current); |
|
292 |
} |
|
293 |
} |
|
294 |
document.getElementById("down").addEventListener("click", down); |
|
295 |
function left() { |
|
296 |
var updated = board.move.left(board.current); |
|
297 |
if (updated) { |
|
7
d8106c8c0481
Rename methods and use sequential numbers instead of power of 2.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
4
diff
changeset
|
298 |
board.putRandom(board.current); |
0 | 299 |
ui.board.update(board.current); |
300 |
ui.score.update(board.current); |
|
301 |
} |
|
302 |
} |
|
303 |
document.getElementById("left").addEventListener("click", left); |
|
304 |
function right() { |
|
305 |
var updated = board.move.right(board.current); |
|
306 |
if (updated) { |
|
7
d8106c8c0481
Rename methods and use sequential numbers instead of power of 2.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
4
diff
changeset
|
307 |
board.putRandom(board.current); |
0 | 308 |
ui.board.update(board.current); |
309 |
ui.score.update(board.current); |
|
310 |
} |
|
311 |
} |
|
312 |
document.getElementById("right").addEventListener("click", right); |
|
313 |
||
314 |
document.body.addEventListener("keydown", function(event) { |
|
315 |
var key = event.keyCode || event.which; |
|
316 |
switch (key) { |
|
317 |
case 38: up(); break; |
|
318 |
case 40: down(); break; |
|
319 |
case 37: left(); break; |
|
320 |
case 39: right(); break; |
|
321 |
} |
|
322 |
return false; |
|
323 |
}); |
|
12
a9a44cfc3e08
Moves for 2d array board + test toolkit.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
11
diff
changeset
|
324 |
|
a9a44cfc3e08
Moves for 2d array board + test toolkit.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
11
diff
changeset
|
325 |
document.getElementById("test").addEventListener("click", function() { |
a9a44cfc3e08
Moves for 2d array board + test toolkit.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
11
diff
changeset
|
326 |
board.move.replaceByBoardJS(ui.brdEngine); |
a9a44cfc3e08
Moves for 2d array board + test toolkit.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
11
diff
changeset
|
327 |
}); |
0 | 328 |
|
329 |
ui.message = {}; |
|
330 |
var messageDom = document.getElementById("message-area"); |
|
331 |
ui.message.clear = function() { |
|
332 |
messageDom.innerHTML = ""; |
|
333 |
} |
|
334 |
ui.message.set = function(msg) { |
|
335 |
messageDom.innerHTML = msg; |
|
336 |
} |
|
337 |
||
338 |
function step() { |
|
339 |
ui.message.clear(); |
|
340 |
if (board.gameOver(board.current)) { |
|
341 |
ui.message.set("Game over!"); |
|
342 |
return; |
|
343 |
} |
|
344 |
var tmpBrd = board.create(); |
|
345 |
board.copy(board.current, tmpBrd); |
|
25
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
346 |
var move = ui.ai.current.analyse(tmpBrd); |
10
70ece7f758a0
Move rule and AI engine to separate files.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
7
diff
changeset
|
347 |
if (typeof move === 'undefined') { |
0 | 348 |
ui.message.set("I don't know how to move!"); |
349 |
return; |
|
350 |
} |
|
10
70ece7f758a0
Move rule and AI engine to separate files.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
7
diff
changeset
|
351 |
var updated = board.move[move].call(null, board.current); |
0 | 352 |
if (updated) { |
7
d8106c8c0481
Rename methods and use sequential numbers instead of power of 2.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
4
diff
changeset
|
353 |
board.putRandom(board.current); |
0 | 354 |
ui.board.update(board.current); |
355 |
ui.score.update(board.current); |
|
356 |
} else { |
|
357 |
ui.message.set("Wrong move!"); |
|
358 |
} |
|
359 |
} |
|
360 |
document.getElementById("step").addEventListener("click", step); |
|
361 |
||
362 |
function finish() { |
|
363 |
ui.message.clear(); |
|
1 | 364 |
var step = 0; |
365 |
var tsFrom = new Date().getTime(); |
|
0 | 366 |
while (!board.gameOver(board.current)) { |
367 |
var tmpBrd = board.create(); |
|
368 |
board.copy(board.current, tmpBrd); |
|
25
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
369 |
var move = ui.ai.current.analyse(tmpBrd); |
10
70ece7f758a0
Move rule and AI engine to separate files.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
7
diff
changeset
|
370 |
if (typeof move === 'undefined') { |
0 | 371 |
ui.message.set("I don't know how to move!"); |
372 |
return; |
|
373 |
} |
|
10
70ece7f758a0
Move rule and AI engine to separate files.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
7
diff
changeset
|
374 |
var updated = board.move[move].call(null, board.current); |
0 | 375 |
if (updated) { |
7
d8106c8c0481
Rename methods and use sequential numbers instead of power of 2.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
4
diff
changeset
|
376 |
board.putRandom(board.current); |
0 | 377 |
} else { |
378 |
ui.board.update(board.current); |
|
379 |
ui.score.update(board.current); |
|
380 |
ui.message.set("Wrong move!"); |
|
381 |
return; |
|
382 |
} |
|
1 | 383 |
step++; |
0 | 384 |
} |
1 | 385 |
var tsTo = new Date().getTime(); |
0 | 386 |
ui.board.update(board.current); |
387 |
ui.score.update(board.current); |
|
1 | 388 |
ui.score.speed(step*1000.0/(tsTo-tsFrom), step); |
0 | 389 |
ui.message.set("Game over!"); |
390 |
} |
|
391 |
document.getElementById("finish").addEventListener("click", finish); |
|
392 |
||
10
70ece7f758a0
Move rule and AI engine to separate files.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
7
diff
changeset
|
393 |
//////////////////////////////////////////////////////////////// |
70ece7f758a0
Move rule and AI engine to separate files.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
7
diff
changeset
|
394 |
// Register AIs. |
0 | 395 |
|
25
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
396 |
ui.ai = {}; |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
397 |
ui.ai.current = null; |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
398 |
ui.ai.algList = { |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
399 |
"ai-random": function() { |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
400 |
return new ai.random(ui.brdEngine); |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
401 |
}, |
27
8f96d09a4d94
Add Blind cycle AI. Add example of setting passing.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
26
diff
changeset
|
402 |
"ai-blind-cycle": function(aiDom) { |
8f96d09a4d94
Add Blind cycle AI. Add example of setting passing.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
26
diff
changeset
|
403 |
var cfg = {}; |
8f96d09a4d94
Add Blind cycle AI. Add example of setting passing.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
26
diff
changeset
|
404 |
cfg.clockwise = aiDom.querySelectorAll("input[name='clockwise']")[0].checked; |
8f96d09a4d94
Add Blind cycle AI. Add example of setting passing.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
26
diff
changeset
|
405 |
cfg.whilePossible = aiDom.querySelectorAll("input[name='whilePossible']")[0].checked; |
8f96d09a4d94
Add Blind cycle AI. Add example of setting passing.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
26
diff
changeset
|
406 |
return new ai.blindCycle(ui.brdEngine, cfg); |
8f96d09a4d94
Add Blind cycle AI. Add example of setting passing.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
26
diff
changeset
|
407 |
}, |
25
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
408 |
"ai-next-max-score": function() { |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
409 |
return new ai.nextMaxScore(ui.brdEngine); |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
410 |
}, |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
411 |
"ai-next-max-value": function() { |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
412 |
return new ai.nextMaxValue(ui.brdEngine); |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
413 |
}, |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
414 |
"ai-deep-max-score": function() { |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
415 |
return new ai.deepMaxScore(ui.brdEngine); |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
416 |
}, |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
417 |
"ai-deep-max-score-corner": function() { |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
418 |
return new ai.deepMaxScoreCorner(ui.brdEngine); |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
419 |
}, |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
420 |
"ai-expectimax": function() { |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
421 |
return new ai.expectimax(ui.brdEngine); |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
422 |
}, |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
423 |
// "": function() { |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
424 |
// return new ai.(ui.brdEngine); |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
425 |
// }, |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
426 |
}; |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
427 |
ui.ai.domList = document.querySelectorAll('div.ai'); |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
428 |
for (var i = 0; i < ui.ai.domList.length; i++) { |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
429 |
ui.ai.domList[i].querySelectorAll('button.ai')[0].addEventListener("click", function (event) { |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
430 |
ui.ai.enable(event.target.parentNode); |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
431 |
}); |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
432 |
} |
0 | 433 |
|
25
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
434 |
ui.ai.moveToTop = function(aiDom) { |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
435 |
for (var i = 0; i < ui.ai.domList.length; i++) { |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
436 |
ui.ai.domList[i].classList.remove('enabled'); |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
437 |
} |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
438 |
var rootDom = aiDom.parentNode; |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
439 |
rootDom.removeChild(aiDom); |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
440 |
rootDom.insertBefore(aiDom, rootDom.firstChild); |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
441 |
aiDom.classList.add('enabled'); |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
442 |
} |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
443 |
ui.ai.enable = function(aiDom) { |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
444 |
if (ui.ai.current) |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
445 |
ui.ai.current.cleanup(); |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
446 |
ui.ai.moveToTop(aiDom); |
27
8f96d09a4d94
Add Blind cycle AI. Add example of setting passing.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
26
diff
changeset
|
447 |
ui.ai.current = ui.ai.algList[aiDom.id](aiDom); |
25
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
448 |
} |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
449 |
|
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
450 |
ui.brdEngine = BoardArr2d; // TODO make user selectable |
0 | 451 |
|
452 |
</script> |
|
453 |
||
454 |
</body> |
|
455 |
</html> |