author | Oleksandr Gavenko <gavenkoa@gmail.com> |
Thu, 11 Sep 2014 20:01:09 +0300 | |
changeset 55 | 8ba9dc89be87 |
parent 53 | ee53cd2cb69a |
child 59 | 28613a54c71c |
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 |
} |
|
33
f1bbe12e2c3d
Highlight score area.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
32
diff
changeset
|
31 |
div.ai-control, div.move-control, div.score-conrol { |
30
1d19768ca71d
Highlight AI controls.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
27
diff
changeset
|
32 |
border: 1px red solid; |
1d19768ca71d
Highlight AI controls.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
27
diff
changeset
|
33 |
display: inline-block; |
1d19768ca71d
Highlight AI controls.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
27
diff
changeset
|
34 |
margin: 10px 0 2px 0; |
31 | 35 |
background-color: cornsilk; |
36 |
} |
|
33
f1bbe12e2c3d
Highlight score area.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
32
diff
changeset
|
37 |
div.move-control, div.score-conrol { |
f1bbe12e2c3d
Highlight score area.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
32
diff
changeset
|
38 |
padding: 3px; |
31 | 39 |
} |
34 | 40 |
div.move-control button.suggestion { |
41 |
background-color: cyan; |
|
42 |
} |
|
31 | 43 |
div.ai-control { |
30
1d19768ca71d
Highlight AI controls.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
27
diff
changeset
|
44 |
padding: 2px 2px 2px 1em; |
1d19768ca71d
Highlight AI controls.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
27
diff
changeset
|
45 |
} |
1d19768ca71d
Highlight AI controls.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
27
diff
changeset
|
46 |
div.ai-control:before { |
1d19768ca71d
Highlight AI controls.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
27
diff
changeset
|
47 |
content: "AI: "; |
1d19768ca71d
Highlight AI controls.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
27
diff
changeset
|
48 |
margin-left: -.5em; |
1d19768ca71d
Highlight AI controls.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
27
diff
changeset
|
49 |
padding: 0; |
1d19768ca71d
Highlight AI controls.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
27
diff
changeset
|
50 |
} |
4 | 51 |
.report > .name { |
52 |
font-weight: bold; |
|
53 |
} |
|
25
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
54 |
div.ai { |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
55 |
border: 1px red solid; |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
56 |
margin-top: 20px; |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
57 |
padding: 2px; |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
58 |
position: relative; |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
59 |
background-color: cornsilk; |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
60 |
} |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
61 |
div.ai.enabled { |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
62 |
background-color: wheat; |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
63 |
} |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
64 |
div.ai > h5 { |
27
8f96d09a4d94
Add Blind cycle AI. Add example of setting passing.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
26
diff
changeset
|
65 |
margin: 2px 0 2px 6em; |
25
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
66 |
} |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
67 |
button.ai { |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
68 |
position: absolute; |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
69 |
left: 1em; |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
70 |
top: -1em; |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
71 |
} |
27
8f96d09a4d94
Add Blind cycle AI. Add example of setting passing.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
26
diff
changeset
|
72 |
div.ai > div.option { |
8f96d09a4d94
Add Blind cycle AI. Add example of setting passing.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
26
diff
changeset
|
73 |
display: inline-block; |
8f96d09a4d94
Add Blind cycle AI. Add example of setting passing.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
26
diff
changeset
|
74 |
float: left; |
8f96d09a4d94
Add Blind cycle AI. Add example of setting passing.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
26
diff
changeset
|
75 |
margin: 1px 4px; |
36
a18fc2601ce8
Add blind eight random AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
35
diff
changeset
|
76 |
padding: 2px; |
a18fc2601ce8
Add blind eight random AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
35
diff
changeset
|
77 |
border: 1px solid tan; |
a18fc2601ce8
Add blind eight random AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
35
diff
changeset
|
78 |
border-radius: 4px; |
a18fc2601ce8
Add blind eight random AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
35
diff
changeset
|
79 |
} |
55
8ba9dc89be87
Automatically extract config from DOM.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
53
diff
changeset
|
80 |
div.ai > div.option > input.positive { |
36
a18fc2601ce8
Add blind eight random AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
35
diff
changeset
|
81 |
text-align: right; |
a18fc2601ce8
Add blind eight random AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
35
diff
changeset
|
82 |
max-width: 4em; |
a18fc2601ce8
Add blind eight random AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
35
diff
changeset
|
83 |
margin-rght: 2px; |
27
8f96d09a4d94
Add Blind cycle AI. Add example of setting passing.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
26
diff
changeset
|
84 |
} |
8f96d09a4d94
Add Blind cycle AI. Add example of setting passing.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
26
diff
changeset
|
85 |
.clearfix { |
8f96d09a4d94
Add Blind cycle AI. Add example of setting passing.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
26
diff
changeset
|
86 |
clear: both; |
8f96d09a4d94
Add Blind cycle AI. Add example of setting passing.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
26
diff
changeset
|
87 |
} |
31 | 88 |
#test { |
89 |
display: none; |
|
90 |
} |
|
0 | 91 |
</style> |
92 |
</head> |
|
93 |
<body> |
|
94 |
||
95 |
<h1>2048</h1> |
|
96 |
||
33
f1bbe12e2c3d
Highlight score area.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
32
diff
changeset
|
97 |
<div id="score-area" class="area"> |
f1bbe12e2c3d
Highlight score area.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
32
diff
changeset
|
98 |
<div class="score-conrol"> |
f1bbe12e2c3d
Highlight score area.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
32
diff
changeset
|
99 |
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 |
f1bbe12e2c3d
Highlight score area.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
32
diff
changeset
|
100 |
</div> |
f1bbe12e2c3d
Highlight score area.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
32
diff
changeset
|
101 |
</div> |
0 | 102 |
|
4 | 103 |
<div id="message-area" class="area"></div> |
0 | 104 |
|
105 |
<table id="board"> |
|
106 |
<tr> |
|
107 |
<td></td> |
|
108 |
<td></td> |
|
109 |
<td></td> |
|
110 |
<td></td> |
|
111 |
</tr> |
|
112 |
<tr> |
|
113 |
<td></td> |
|
114 |
<td></td> |
|
115 |
<td></td> |
|
116 |
<td></td> |
|
117 |
</tr> |
|
118 |
<tr> |
|
119 |
<td></td> |
|
120 |
<td></td> |
|
121 |
<td></td> |
|
122 |
<td></td> |
|
123 |
</tr> |
|
124 |
<tr> |
|
125 |
<td></td> |
|
126 |
<td></td> |
|
127 |
<td></td> |
|
128 |
<td></td> |
|
129 |
</tr> |
|
130 |
</table> |
|
131 |
||
4 | 132 |
<div id="control-area" class="area"> |
0 | 133 |
<div> |
134 |
<button id="start">Start</button> |
|
38
d476efde7242
2 modes of value showing: 0,1,...,11 and 0,2,4,...,2048.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
37
diff
changeset
|
135 |
<span><input type="checkbox" id="2048" checked> 2048</span> |
30
1d19768ca71d
Highlight AI controls.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
27
diff
changeset
|
136 |
</div> |
1d19768ca71d
Highlight AI controls.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
27
diff
changeset
|
137 |
<div class="ai-control"> |
1d19768ca71d
Highlight AI controls.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
27
diff
changeset
|
138 |
<button id="suggest">Suggest</button> |
0 | 139 |
<button id="step">Step</button> |
140 |
<button id="finish">Finish</button> |
|
141 |
</div> |
|
31 | 142 |
<div class="clearfix"></div> |
143 |
<div class="move-control"> |
|
144 |
<table> |
|
145 |
<tr> |
|
146 |
<td></td><td><button id="up">up</button></td><td></td> |
|
147 |
</tr> |
|
148 |
<tr> |
|
149 |
<td><button id="left">left</button></td><td><button id="down">down</button></td><td><button id="right">right</button></td> |
|
150 |
</tr> |
|
151 |
</table> |
|
12
a9a44cfc3e08
Moves for 2d array board + test toolkit.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
11
diff
changeset
|
152 |
<button id="test">test</button> |
0 | 153 |
</div> |
25
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
154 |
<h1>AIs</h1> |
0 | 155 |
<div> |
35 | 156 |
<div class="ai" id="ai-blind-random"> |
25
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
157 |
<button class="ai">enable</button> |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
158 |
<h5>bling random</h5> |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
159 |
</div> |
36
a18fc2601ce8
Add blind eight random AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
35
diff
changeset
|
160 |
<div class="ai" id="ai-blind-weight-random"> |
a18fc2601ce8
Add blind eight random AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
35
diff
changeset
|
161 |
<button class="ai">enable</button> |
a18fc2601ce8
Add blind eight random AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
35
diff
changeset
|
162 |
<h5>bling weight random</h5> |
a18fc2601ce8
Add blind eight random AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
35
diff
changeset
|
163 |
<div class="option"> |
55
8ba9dc89be87
Automatically extract config from DOM.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
53
diff
changeset
|
164 |
<input type="text" name="left" class="positive" pattern="[0-9]*[.]?[0-9]*" value="1"/> left weight |
36
a18fc2601ce8
Add blind eight random AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
35
diff
changeset
|
165 |
</div> |
a18fc2601ce8
Add blind eight random AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
35
diff
changeset
|
166 |
<div class="option"> |
55
8ba9dc89be87
Automatically extract config from DOM.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
53
diff
changeset
|
167 |
<input type="text" name="right" class="positive" pattern="[0-9]*[.]?[0-9]*" value="1"/> right weight |
36
a18fc2601ce8
Add blind eight random AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
35
diff
changeset
|
168 |
</div> |
a18fc2601ce8
Add blind eight random AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
35
diff
changeset
|
169 |
<div class="option"> |
55
8ba9dc89be87
Automatically extract config from DOM.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
53
diff
changeset
|
170 |
<input type="text" name="up" class="positive" pattern="[0-9]*[.]?[0-9]*" value="1"/> up weight |
36
a18fc2601ce8
Add blind eight random AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
35
diff
changeset
|
171 |
</div> |
a18fc2601ce8
Add blind eight random AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
35
diff
changeset
|
172 |
<div class="option"> |
55
8ba9dc89be87
Automatically extract config from DOM.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
53
diff
changeset
|
173 |
<input type="text" name="down" class="positive" pattern="[0-9]*[.]?[0-9]*" value="1"/> down weight |
36
a18fc2601ce8
Add blind eight random AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
35
diff
changeset
|
174 |
</div> |
a18fc2601ce8
Add blind eight random AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
35
diff
changeset
|
175 |
<div class="clearfix"></div> |
a18fc2601ce8
Add blind eight random AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
35
diff
changeset
|
176 |
</div> |
27
8f96d09a4d94
Add Blind cycle AI. Add example of setting passing.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
26
diff
changeset
|
177 |
<div class="ai" id="ai-blind-cycle"> |
25
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
178 |
<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
|
179 |
<h5>blind cycle</h5> |
8f96d09a4d94
Add Blind cycle AI. Add example of setting passing.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
26
diff
changeset
|
180 |
<div class="option"> |
8f96d09a4d94
Add Blind cycle AI. Add example of setting passing.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
26
diff
changeset
|
181 |
<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
|
182 |
</div> |
8f96d09a4d94
Add Blind cycle AI. Add example of setting passing.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
26
diff
changeset
|
183 |
<div class="option"> |
8f96d09a4d94
Add Blind cycle AI. Add example of setting passing.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
26
diff
changeset
|
184 |
<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
|
185 |
</div> |
8f96d09a4d94
Add Blind cycle AI. Add example of setting passing.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
26
diff
changeset
|
186 |
<div class="clearfix"></div> |
25
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
187 |
</div> |
53
ee53cd2cb69a
Join all one step deep AIs into one with linear weight function.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
52
diff
changeset
|
188 |
<div class="ai" id="ai-one-step-deep"> |
25
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
189 |
<button class="ai">enable</button> |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
190 |
<h5>next merge makes max value</h5> |
53
ee53cd2cb69a
Join all one step deep AIs into one with linear weight function.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
52
diff
changeset
|
191 |
<div class="option"> |
55
8ba9dc89be87
Automatically extract config from DOM.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
53
diff
changeset
|
192 |
<input type="text" name="scoreCoef" class="positive" pattern="[0-9]*[.]?[0-9]*" value="1"/> score weight |
53
ee53cd2cb69a
Join all one step deep AIs into one with linear weight function.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
52
diff
changeset
|
193 |
</div> |
ee53cd2cb69a
Join all one step deep AIs into one with linear weight function.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
52
diff
changeset
|
194 |
<div class="option"> |
55
8ba9dc89be87
Automatically extract config from DOM.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
53
diff
changeset
|
195 |
<input type="text" name="maxValCoef" class="positive" pattern="[0-9]*[.]?[0-9]*" value="0"/> max value weight |
53
ee53cd2cb69a
Join all one step deep AIs into one with linear weight function.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
52
diff
changeset
|
196 |
</div> |
ee53cd2cb69a
Join all one step deep AIs into one with linear weight function.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
52
diff
changeset
|
197 |
<div class="option"> |
55
8ba9dc89be87
Automatically extract config from DOM.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
53
diff
changeset
|
198 |
<input type="text" name="cornerBonus" class="positive" pattern="[0-9]*[.]?[0-9]*" value="100"/> max value at corner bonus |
53
ee53cd2cb69a
Join all one step deep AIs into one with linear weight function.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
52
diff
changeset
|
199 |
</div> |
ee53cd2cb69a
Join all one step deep AIs into one with linear weight function.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
52
diff
changeset
|
200 |
<div class="option"> |
55
8ba9dc89be87
Automatically extract config from DOM.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
53
diff
changeset
|
201 |
<input type="text" name="edgeBonus" class="positive" pattern="[0-9]*[.]?[0-9]*" value="0"/> max value at edge bonus |
53
ee53cd2cb69a
Join all one step deep AIs into one with linear weight function.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
52
diff
changeset
|
202 |
</div> |
ee53cd2cb69a
Join all one step deep AIs into one with linear weight function.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
52
diff
changeset
|
203 |
<div class="option"> |
55
8ba9dc89be87
Automatically extract config from DOM.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
53
diff
changeset
|
204 |
<input type="text" name="freeBonus" class="positive" pattern="[0-9]*[.]?[0-9]*" value="10"/> free cell coefficient |
53
ee53cd2cb69a
Join all one step deep AIs into one with linear weight function.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
52
diff
changeset
|
205 |
</div> |
ee53cd2cb69a
Join all one step deep AIs into one with linear weight function.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
52
diff
changeset
|
206 |
<div class="clearfix"></div> |
25
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
207 |
</div> |
26 | 208 |
<div class="ai" id="ai-deep-max-score"> |
25
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
209 |
<button class="ai">enable</button> |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
210 |
<h5>deep merges without simulation make max score</h5> |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
211 |
</div> |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
212 |
<div class="ai" id="ai-deep-max-score-corner"> |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
213 |
<button class="ai">enable</button> |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
214 |
<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
|
215 |
</div> |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
216 |
<div class="ai" id="ai-expectimax"> |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
217 |
<button class="ai">enable</button> |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
218 |
<h5>expectimax</h5> |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
219 |
</div> |
0 | 220 |
</div> |
221 |
</div> |
|
222 |
||
4 | 223 |
<div id="report-area" class="area"> |
224 |
<h1>Reports</h1> |
|
225 |
<div class="report" class="area"> |
|
226 |
<div class="name">next max score</div> |
|
227 |
<table> |
|
228 |
<tr> |
|
229 |
<th>Max</th> |
|
230 |
<th>Count</th> |
|
231 |
<th>%</th> |
|
232 |
</tr> |
|
233 |
<tr> |
|
234 |
<td>128</td><td>8</td><td><div style="background: red; width: 100px">x</div></td> |
|
235 |
</tr> |
|
236 |
<tr> |
|
237 |
<td>256</td><td>4</td><td><div style="background: red; width: 50px">x</div></td> |
|
238 |
</tr> |
|
239 |
<tr> |
|
240 |
<td>512</td><td>12</td><td><div style="background: red; width: 150px">x</div></td> |
|
241 |
</tr> |
|
242 |
</table> |
|
243 |
</div> |
|
244 |
</div> |
|
245 |
||
0 | 246 |
<script> |
247 |
"use strict"; |
|
248 |
||
249 |
var boardDom = document.getElementById("board"); |
|
250 |
var ui = {}; |
|
251 |
ui.board = {}; |
|
38
d476efde7242
2 modes of value showing: 0,1,...,11 and 0,2,4,...,2048.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
37
diff
changeset
|
252 |
ui.board.val2048Dom = document.getElementById('2048'); |
d476efde7242
2 modes of value showing: 0,1,...,11 and 0,2,4,...,2048.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
37
diff
changeset
|
253 |
ui.board.val2048Dom.addEventListener("click", function(event) { |
45 | 254 |
ui.board.update(ui.board.position); |
43
8b1577211149
Save val2048 preference.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
42
diff
changeset
|
255 |
localStorage.val2048 = ui.board.val2048Dom.checked; |
38
d476efde7242
2 modes of value showing: 0,1,...,11 and 0,2,4,...,2048.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
37
diff
changeset
|
256 |
event.target.blur(); |
d476efde7242
2 modes of value showing: 0,1,...,11 and 0,2,4,...,2048.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
37
diff
changeset
|
257 |
}); |
d476efde7242
2 modes of value showing: 0,1,...,11 and 0,2,4,...,2048.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
37
diff
changeset
|
258 |
ui.board.val2048 = function(val) { |
d476efde7242
2 modes of value showing: 0,1,...,11 and 0,2,4,...,2048.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
37
diff
changeset
|
259 |
if (ui.board.val2048Dom.checked) |
d476efde7242
2 modes of value showing: 0,1,...,11 and 0,2,4,...,2048.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
37
diff
changeset
|
260 |
return Math.pow(2, val); |
d476efde7242
2 modes of value showing: 0,1,...,11 and 0,2,4,...,2048.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
37
diff
changeset
|
261 |
return val; |
d476efde7242
2 modes of value showing: 0,1,...,11 and 0,2,4,...,2048.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
37
diff
changeset
|
262 |
} |
15
0fa97e430a9e
ui.board.set care about 'val' argument.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
14
diff
changeset
|
263 |
/* 'val' typesafe. */ |
0 | 264 |
ui.board.set = function(i, j, val) { |
16 | 265 |
if (typeof val !== 'number') |
15
0fa97e430a9e
ui.board.set care about 'val' argument.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
14
diff
changeset
|
266 |
val = 0; |
13
b3bfa9d1b537
Add color to board depending on values..
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
12
diff
changeset
|
267 |
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
|
268 |
if (0 < val && val < 15) { |
38
d476efde7242
2 modes of value showing: 0,1,...,11 and 0,2,4,...,2048.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
37
diff
changeset
|
269 |
dom.innerHTML = ui.board.val2048(val); |
15
0fa97e430a9e
ui.board.set care about 'val' argument.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
14
diff
changeset
|
270 |
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
|
271 |
} else { |
0fa97e430a9e
ui.board.set care about 'val' argument.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
14
diff
changeset
|
272 |
dom.innerHTML = ''; |
0fa97e430a9e
ui.board.set care about 'val' argument.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
14
diff
changeset
|
273 |
var rgb = 'white'; |
0fa97e430a9e
ui.board.set care about 'val' argument.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
14
diff
changeset
|
274 |
} |
13
b3bfa9d1b537
Add color to board depending on values..
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
12
diff
changeset
|
275 |
dom.style.backgroundColor = rgb; |
0 | 276 |
} |
277 |
ui.board.update = function(brd) { |
|
278 |
for (var i = 0; i < 4; i++) { |
|
279 |
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
|
280 |
ui.board.set(i, j, brd[i][j]); |
0 | 281 |
} |
282 |
} |
|
283 |
} |
|
17 | 284 |
ui.board.handleEdit = function(tdDom, i, j) { |
285 |
if (!tdDom.firstChild || tdDom.firstChild.nodeType === Node.TEXT_NODE) { |
|
286 |
if (tdDom.firstChild) |
|
287 |
tdDom.removeChild(tdDom.firstChild); |
|
288 |
var inputDom = document.createElement('input'); |
|
45 | 289 |
inputDom.value = ui.board.position[i][j]; |
17 | 290 |
inputDom.style.width = '1em'; |
291 |
tdDom.appendChild(inputDom); |
|
292 |
inputDom.focus(); |
|
293 |
inputDom.select(); |
|
294 |
inputDom.addEventListener("blur", function(tdDom, i, j) { |
|
295 |
return function(event) { |
|
296 |
ui.board.handleEdit(tdDom, i, j); |
|
297 |
return false; |
|
298 |
} |
|
299 |
}(tdDom, i, j)); |
|
300 |
} else { |
|
301 |
var inputDom = tdDom.firstChild; |
|
302 |
var val = parseInt(inputDom.value); |
|
303 |
if (0 <= val && val < 15) { |
|
45 | 304 |
ui.board.position[i][j] = val; |
17 | 305 |
ui.board.set(i, j, val); |
306 |
} else { |
|
45 | 307 |
ui.board.set(i, j, ui.board.position[i][j]); |
17 | 308 |
} |
309 |
} |
|
310 |
} |
|
311 |
ui.board.enableEdit = function() { |
|
312 |
var trDoms = boardDom.querySelectorAll("tr"); |
|
313 |
for (var i = 0; i < 4; i++) { |
|
314 |
var tdDoms = trDoms[i].querySelectorAll("td"); |
|
315 |
for (var j = 0; j < 4; j++) { |
|
316 |
var tdDom = tdDoms[j]; |
|
317 |
tdDom.addEventListener("click", function(tdDom, i, j) { |
|
318 |
return function(event) { |
|
319 |
ui.board.handleEdit(tdDom, i, j); |
|
320 |
return false; |
|
321 |
} |
|
322 |
}(tdDom, i, j)); |
|
323 |
tdDom.addEventListener("keyup", function(tdDom, i, j) { |
|
324 |
return function(event) { |
|
325 |
if (event.keyCode === 13) |
|
326 |
ui.board.handleEdit(tdDom, i, j); |
|
327 |
} |
|
328 |
}(tdDom, i, j)); |
|
329 |
} |
|
330 |
} |
|
331 |
} |
|
332 |
ui.board.enableEdit(); |
|
333 |
||
41
359719178176
Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
40
diff
changeset
|
334 |
//////////////////////////////////////////////////////////////// |
359719178176
Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
40
diff
changeset
|
335 |
// Score statistic. |
359719178176
Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
40
diff
changeset
|
336 |
|
0 | 337 |
ui.score = {}; |
338 |
var scoreDom = document.getElementById("score"); |
|
339 |
var maxDom = document.getElementById("max"); |
|
1 | 340 |
var speedDom = document.getElementById("speed"); |
341 |
var turnDom = document.getElementById("turn"); |
|
0 | 342 |
ui.score.clear = function(brd) { |
343 |
scoreDom.innerHTML = '0'; |
|
344 |
maxDom.innerHTML = '0'; |
|
1 | 345 |
speedDom.innerHTML = '0'; |
346 |
turnDom.innerHTML = '0'; |
|
0 | 347 |
} |
46
4dadaff6eba4
Update/store/restore game turn. Show max value according to value settings.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
45
diff
changeset
|
348 |
ui.score.update = function(brd, turn) { |
0 | 349 |
var score = board.score(brd); |
350 |
scoreDom.innerHTML = '' + score.score; |
|
46
4dadaff6eba4
Update/store/restore game turn. Show max value according to value settings.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
45
diff
changeset
|
351 |
maxDom.innerHTML = '' + ui.board.val2048(score.max); |
4dadaff6eba4
Update/store/restore game turn. Show max value according to value settings.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
45
diff
changeset
|
352 |
turnDom.innerHTML = '' + turn; |
0 | 353 |
} |
1 | 354 |
ui.score.speed = function(speed, turn) { |
355 |
speedDom.innerHTML = '' + speed; |
|
356 |
turnDom.innerHTML = '' + turn; |
|
357 |
} |
|
0 | 358 |
|
39
a75cc5aa5d19
Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
38
diff
changeset
|
359 |
//////////////////////////////////////////////////////////////// |
41
359719178176
Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
40
diff
changeset
|
360 |
// Suggestion UI. |
359719178176
Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
40
diff
changeset
|
361 |
|
359719178176
Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
40
diff
changeset
|
362 |
ui.suggestion = {}; |
359719178176
Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
40
diff
changeset
|
363 |
ui.suggestion.btns = document.querySelectorAll('div.move-control button'); |
359719178176
Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
40
diff
changeset
|
364 |
ui.suggestion.clear = function() { |
359719178176
Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
40
diff
changeset
|
365 |
for (var i = 0; i < ui.suggestion.btns.length; i++) |
359719178176
Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
40
diff
changeset
|
366 |
ui.suggestion.btns[i].classList.remove('suggestion'); |
359719178176
Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
40
diff
changeset
|
367 |
} |
359719178176
Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
40
diff
changeset
|
368 |
ui.suggestion.set = function(dir) { |
359719178176
Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
40
diff
changeset
|
369 |
document.getElementById(dir).classList.add('suggestion'); |
359719178176
Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
40
diff
changeset
|
370 |
} |
359719178176
Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
40
diff
changeset
|
371 |
|
359719178176
Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
40
diff
changeset
|
372 |
//////////////////////////////////////////////////////////////// |
39
a75cc5aa5d19
Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
38
diff
changeset
|
373 |
// Game flow. |
a75cc5aa5d19
Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
38
diff
changeset
|
374 |
|
40 | 375 |
ui.game = {}; |
376 |
ui.game.dirs = ["up", "right", "down", "left"]; |
|
39
a75cc5aa5d19
Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
38
diff
changeset
|
377 |
|
a75cc5aa5d19
Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
38
diff
changeset
|
378 |
var messageDom = document.getElementById("message-area"); |
a75cc5aa5d19
Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
38
diff
changeset
|
379 |
ui.game.clearMessage = function() { |
a75cc5aa5d19
Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
38
diff
changeset
|
380 |
messageDom.innerHTML = ""; |
a75cc5aa5d19
Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
38
diff
changeset
|
381 |
} |
a75cc5aa5d19
Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
38
diff
changeset
|
382 |
ui.game.setMessage = function(msg) { |
a75cc5aa5d19
Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
38
diff
changeset
|
383 |
messageDom.innerHTML = msg; |
a75cc5aa5d19
Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
38
diff
changeset
|
384 |
} |
a75cc5aa5d19
Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
38
diff
changeset
|
385 |
|
a75cc5aa5d19
Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
38
diff
changeset
|
386 |
ui.game.checkGameOver = function() { |
45 | 387 |
if (board.gameOver(ui.board.position)) { |
39
a75cc5aa5d19
Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
38
diff
changeset
|
388 |
ui.game.setMessage("Game over!"); |
a75cc5aa5d19
Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
38
diff
changeset
|
389 |
return true; |
a75cc5aa5d19
Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
38
diff
changeset
|
390 |
} else { |
a75cc5aa5d19
Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
38
diff
changeset
|
391 |
return false; |
a75cc5aa5d19
Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
38
diff
changeset
|
392 |
} |
a75cc5aa5d19
Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
38
diff
changeset
|
393 |
} |
a75cc5aa5d19
Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
38
diff
changeset
|
394 |
ui.game.checkMoveValid = function(move) { |
a75cc5aa5d19
Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
38
diff
changeset
|
395 |
var tmpBrd = board.create(); |
45 | 396 |
board.copy(ui.board.position, tmpBrd); |
40 | 397 |
if (ui.game.dirs.indexOf(move) === -1) { |
39
a75cc5aa5d19
Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
38
diff
changeset
|
398 |
ui.game.setMessage("AI can't find move!"); |
a75cc5aa5d19
Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
38
diff
changeset
|
399 |
return false; |
a75cc5aa5d19
Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
38
diff
changeset
|
400 |
} |
a75cc5aa5d19
Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
38
diff
changeset
|
401 |
if ( ! board.move[move].call(null, tmpBrd)) { |
a75cc5aa5d19
Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
38
diff
changeset
|
402 |
ui.game.setMessage("AI move "+move+" is ivalid!"); |
a75cc5aa5d19
Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
38
diff
changeset
|
403 |
return false; |
a75cc5aa5d19
Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
38
diff
changeset
|
404 |
} |
a75cc5aa5d19
Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
38
diff
changeset
|
405 |
return true; |
a75cc5aa5d19
Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
38
diff
changeset
|
406 |
} |
a75cc5aa5d19
Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
38
diff
changeset
|
407 |
|
41
359719178176
Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
40
diff
changeset
|
408 |
ui.game.beginStep = function() { |
359719178176
Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
40
diff
changeset
|
409 |
ui.suggestion.clear(); |
359719178176
Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
40
diff
changeset
|
410 |
ui.game.clearMessage(); |
359719178176
Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
40
diff
changeset
|
411 |
if (ui.game.checkGameOver()) |
359719178176
Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
40
diff
changeset
|
412 |
return false; |
359719178176
Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
40
diff
changeset
|
413 |
return true; |
359719178176
Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
40
diff
changeset
|
414 |
} |
359719178176
Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
40
diff
changeset
|
415 |
ui.game.finishStep = function() { |
45 | 416 |
board.putRandom(ui.board.position); |
46
4dadaff6eba4
Update/store/restore game turn. Show max value according to value settings.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
45
diff
changeset
|
417 |
ui.board.turn++; |
45 | 418 |
ui.board.update(ui.board.position); |
46
4dadaff6eba4
Update/store/restore game turn. Show max value according to value settings.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
45
diff
changeset
|
419 |
ui.score.update(ui.board.position, ui.board.turn); |
45 | 420 |
localStorage.savedBoard = JSON.stringify(ui.board.position); |
46
4dadaff6eba4
Update/store/restore game turn. Show max value according to value settings.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
45
diff
changeset
|
421 |
localStorage.savedTurn = ui.board.turn; |
41
359719178176
Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
40
diff
changeset
|
422 |
} |
359719178176
Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
40
diff
changeset
|
423 |
|
39
a75cc5aa5d19
Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
38
diff
changeset
|
424 |
//////////////////////////////////////////////////////////////// |
a75cc5aa5d19
Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
38
diff
changeset
|
425 |
// Actions. |
a75cc5aa5d19
Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
38
diff
changeset
|
426 |
|
30
1d19768ca71d
Highlight AI controls.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
27
diff
changeset
|
427 |
ui.action = {}; |
1d19768ca71d
Highlight AI controls.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
27
diff
changeset
|
428 |
|
1d19768ca71d
Highlight AI controls.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
27
diff
changeset
|
429 |
ui.action.start = function() { |
0 | 430 |
ui.score.clear(); |
41
359719178176
Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
40
diff
changeset
|
431 |
ui.suggestion.clear(); |
39
a75cc5aa5d19
Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
38
diff
changeset
|
432 |
ui.game.clearMessage(); |
46
4dadaff6eba4
Update/store/restore game turn. Show max value according to value settings.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
45
diff
changeset
|
433 |
ui.board.turn = 0; |
45 | 434 |
ui.board.position = board.create(); |
435 |
board.putRandom(ui.board.position); |
|
436 |
ui.board.update(ui.board.position); |
|
0 | 437 |
} |
30
1d19768ca71d
Highlight AI controls.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
27
diff
changeset
|
438 |
document.getElementById("start").addEventListener("click", ui.action.start); |
0 | 439 |
|
40 | 440 |
for (var i = 0; i < ui.game.dirs.length; i++) { |
441 |
var dir = ui.game.dirs[i]; |
|
442 |
ui.action[dir] = function(dir) { |
|
443 |
return function() { |
|
444 |
if (ui.game.checkGameOver()) |
|
445 |
return; |
|
41
359719178176
Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
40
diff
changeset
|
446 |
ui.suggestion.clear(); |
45 | 447 |
var updated = board.move[dir](ui.board.position); |
40 | 448 |
if (updated) { |
41
359719178176
Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
40
diff
changeset
|
449 |
ui.game.finishStep(); |
40 | 450 |
ui.ai.current && ui.ai.current.cleanup(); |
451 |
} |
|
452 |
} |
|
453 |
} (dir); |
|
454 |
document.getElementById(dir).addEventListener("click", ui.action[dir]); |
|
0 | 455 |
} |
456 |
||
457 |
document.body.addEventListener("keydown", function(event) { |
|
37
fe75d168a605
Ignore board movement when user edit input.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
36
diff
changeset
|
458 |
if (document.activeElement.value) |
44
518ab579d24b
Prevent from blocking Firefox key binding.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
43
diff
changeset
|
459 |
return; |
52
e4e21d2fcbe7
Ignore moves if shift/alt/control key pressed.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
46
diff
changeset
|
460 |
if (event.altKey || event.ctrlKey || event.shiftKey || event.metaKey) |
e4e21d2fcbe7
Ignore moves if shift/alt/control key pressed.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
46
diff
changeset
|
461 |
return; |
0 | 462 |
var key = event.keyCode || event.which; |
44
518ab579d24b
Prevent from blocking Firefox key binding.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
43
diff
changeset
|
463 |
var keyMoves = {38: "up", 40: "down", 37: "left", 39: "right"}; |
518ab579d24b
Prevent from blocking Firefox key binding.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
43
diff
changeset
|
464 |
if (key in keyMoves) { |
518ab579d24b
Prevent from blocking Firefox key binding.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
43
diff
changeset
|
465 |
ui.action[keyMoves[key]](); |
518ab579d24b
Prevent from blocking Firefox key binding.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
43
diff
changeset
|
466 |
event.preventDefault(); |
0 | 467 |
} |
468 |
}); |
|
12
a9a44cfc3e08
Moves for 2d array board + test toolkit.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
11
diff
changeset
|
469 |
|
a9a44cfc3e08
Moves for 2d array board + test toolkit.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
11
diff
changeset
|
470 |
document.getElementById("test").addEventListener("click", function() { |
a9a44cfc3e08
Moves for 2d array board + test toolkit.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
11
diff
changeset
|
471 |
board.move.replaceByBoardJS(ui.brdEngine); |
a9a44cfc3e08
Moves for 2d array board + test toolkit.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
11
diff
changeset
|
472 |
}); |
34 | 473 |
|
474 |
ui.action.suggest = function() { |
|
41
359719178176
Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
40
diff
changeset
|
475 |
if ( ! ui.ai.current) { |
359719178176
Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
40
diff
changeset
|
476 |
ui.game.setMessage('Select AI!'); |
359719178176
Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
40
diff
changeset
|
477 |
return; |
359719178176
Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
40
diff
changeset
|
478 |
} |
359719178176
Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
40
diff
changeset
|
479 |
if ( ! ui.game.beginStep()) |
0 | 480 |
return; |
481 |
var tmpBrd = board.create(); |
|
45 | 482 |
board.copy(ui.board.position, tmpBrd); |
25
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
483 |
var move = ui.ai.current.analyse(tmpBrd); |
34 | 484 |
ui.ai.current.cleanup(); |
39
a75cc5aa5d19
Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
38
diff
changeset
|
485 |
if ( ! ui.game.checkMoveValid(move)) |
34 | 486 |
return; |
41
359719178176
Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
40
diff
changeset
|
487 |
ui.suggestion.set(move); |
34 | 488 |
} |
489 |
document.getElementById("suggest").addEventListener("click", ui.action.suggest); |
|
490 |
ui.action.step = function() { |
|
41
359719178176
Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
40
diff
changeset
|
491 |
if ( ! ui.ai.current) { |
359719178176
Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
40
diff
changeset
|
492 |
ui.game.setMessage('Select AI!'); |
359719178176
Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
40
diff
changeset
|
493 |
return; |
359719178176
Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
40
diff
changeset
|
494 |
} |
359719178176
Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
40
diff
changeset
|
495 |
if ( ! ui.game.beginStep()) |
0 | 496 |
return; |
34 | 497 |
var tmpBrd = board.create(); |
45 | 498 |
board.copy(ui.board.position, tmpBrd); |
34 | 499 |
var move = ui.ai.current.analyse(tmpBrd); |
500 |
ui.ai.current.cleanup(); |
|
39
a75cc5aa5d19
Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
38
diff
changeset
|
501 |
if ( ! ui.game.checkMoveValid(move)) |
34 | 502 |
return; |
45 | 503 |
board.move[move].call(null, ui.board.position); |
41
359719178176
Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
40
diff
changeset
|
504 |
ui.game.finishStep(); |
0 | 505 |
} |
34 | 506 |
document.getElementById("step").addEventListener("click", ui.action.step); |
0 | 507 |
|
30
1d19768ca71d
Highlight AI controls.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
27
diff
changeset
|
508 |
ui.action.finish = function() { |
41
359719178176
Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
40
diff
changeset
|
509 |
if ( ! ui.ai.current) { |
359719178176
Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
40
diff
changeset
|
510 |
ui.game.setMessage('Select AI!'); |
359719178176
Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
40
diff
changeset
|
511 |
return; |
359719178176
Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
40
diff
changeset
|
512 |
} |
359719178176
Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
40
diff
changeset
|
513 |
ui.game.beginStep(); |
1 | 514 |
var step = 0; |
515 |
var tsFrom = new Date().getTime(); |
|
45 | 516 |
while (!board.gameOver(ui.board.position)) { |
0 | 517 |
var tmpBrd = board.create(); |
45 | 518 |
board.copy(ui.board.position, tmpBrd); |
25
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
519 |
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
|
520 |
if (typeof move === 'undefined') { |
39
a75cc5aa5d19
Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
38
diff
changeset
|
521 |
ui.game.setMessage("I don't know how to move!"); |
0 | 522 |
return; |
523 |
} |
|
45 | 524 |
var updated = board.move[move].call(null, ui.board.position); |
0 | 525 |
if (updated) { |
45 | 526 |
board.putRandom(ui.board.position); |
0 | 527 |
} else { |
41
359719178176
Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
40
diff
changeset
|
528 |
ui.game.finishStep(); |
39
a75cc5aa5d19
Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
38
diff
changeset
|
529 |
ui.game.setMessage("Wrong move!"); |
0 | 530 |
return; |
531 |
} |
|
1 | 532 |
step++; |
0 | 533 |
} |
1 | 534 |
var tsTo = new Date().getTime(); |
41
359719178176
Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
40
diff
changeset
|
535 |
ui.game.finishStep(); |
1 | 536 |
ui.score.speed(step*1000.0/(tsTo-tsFrom), step); |
39
a75cc5aa5d19
Some renaming and reordering.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
38
diff
changeset
|
537 |
ui.game.setMessage("Game over!"); |
30
1d19768ca71d
Highlight AI controls.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
27
diff
changeset
|
538 |
ui.ai.current && ui.ai.current.cleanup(); |
0 | 539 |
} |
30
1d19768ca71d
Highlight AI controls.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
27
diff
changeset
|
540 |
document.getElementById("finish").addEventListener("click", ui.action.finish); |
0 | 541 |
|
10
70ece7f758a0
Move rule and AI engine to separate files.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
7
diff
changeset
|
542 |
//////////////////////////////////////////////////////////////// |
70ece7f758a0
Move rule and AI engine to separate files.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
7
diff
changeset
|
543 |
// Register AIs. |
0 | 544 |
|
25
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
545 |
ui.ai = {}; |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
546 |
ui.ai.current = null; |
53
ee53cd2cb69a
Join all one step deep AIs into one with linear weight function.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
52
diff
changeset
|
547 |
ui.ai.parseCfg = function(aiDom, cfg) { |
55
8ba9dc89be87
Automatically extract config from DOM.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
53
diff
changeset
|
548 |
var optDoms = aiDom.querySelectorAll("div.option > input.positive[type='text']"); |
53
ee53cd2cb69a
Join all one step deep AIs into one with linear weight function.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
52
diff
changeset
|
549 |
for (var i = 0; i < optDoms.length; i++) { |
ee53cd2cb69a
Join all one step deep AIs into one with linear weight function.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
52
diff
changeset
|
550 |
var val = parseFloat(optDoms[i].value); |
55
8ba9dc89be87
Automatically extract config from DOM.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
53
diff
changeset
|
551 |
if (val === NaN || val < 0) { |
8ba9dc89be87
Automatically extract config from DOM.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
53
diff
changeset
|
552 |
ui.game.setMessage('' + optDoms[i].name + ' is not a positive number!'); |
8ba9dc89be87
Automatically extract config from DOM.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
53
diff
changeset
|
553 |
cfg[optDoms[i].name] = 1; |
53
ee53cd2cb69a
Join all one step deep AIs into one with linear weight function.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
52
diff
changeset
|
554 |
continue; |
ee53cd2cb69a
Join all one step deep AIs into one with linear weight function.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
52
diff
changeset
|
555 |
} |
ee53cd2cb69a
Join all one step deep AIs into one with linear weight function.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
52
diff
changeset
|
556 |
cfg[optDoms[i].name] = val; |
ee53cd2cb69a
Join all one step deep AIs into one with linear weight function.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
52
diff
changeset
|
557 |
} |
ee53cd2cb69a
Join all one step deep AIs into one with linear weight function.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
52
diff
changeset
|
558 |
} |
55
8ba9dc89be87
Automatically extract config from DOM.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
53
diff
changeset
|
559 |
|
25
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
560 |
ui.ai.algList = { |
35 | 561 |
"ai-blind-random": function() { |
562 |
return new ai.blindRandom(ui.brdEngine); |
|
25
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
563 |
}, |
36
a18fc2601ce8
Add blind eight random AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
35
diff
changeset
|
564 |
"ai-blind-weight-random": function(aiDom) { |
a18fc2601ce8
Add blind eight random AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
35
diff
changeset
|
565 |
var cfg = {}; |
55
8ba9dc89be87
Automatically extract config from DOM.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
53
diff
changeset
|
566 |
ui.ai.parseCfg(aiDom, cfg); |
8ba9dc89be87
Automatically extract config from DOM.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
53
diff
changeset
|
567 |
return new ai.blindWeightRandom(ui.brdEngine, cfg); |
36
a18fc2601ce8
Add blind eight random AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
35
diff
changeset
|
568 |
}, |
27
8f96d09a4d94
Add Blind cycle AI. Add example of setting passing.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
26
diff
changeset
|
569 |
"ai-blind-cycle": function(aiDom) { |
8f96d09a4d94
Add Blind cycle AI. Add example of setting passing.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
26
diff
changeset
|
570 |
var cfg = {}; |
8f96d09a4d94
Add Blind cycle AI. Add example of setting passing.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
26
diff
changeset
|
571 |
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
|
572 |
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
|
573 |
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
|
574 |
}, |
53
ee53cd2cb69a
Join all one step deep AIs into one with linear weight function.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
52
diff
changeset
|
575 |
"ai-one-step-deep": function(aiDom) { |
ee53cd2cb69a
Join all one step deep AIs into one with linear weight function.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
52
diff
changeset
|
576 |
var cfg = {}; |
ee53cd2cb69a
Join all one step deep AIs into one with linear weight function.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
52
diff
changeset
|
577 |
ui.ai.parseCfg(aiDom, cfg); |
ee53cd2cb69a
Join all one step deep AIs into one with linear weight function.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
52
diff
changeset
|
578 |
return new ai.oneStepDeep(ui.brdEngine, cfg); |
25
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
579 |
}, |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
580 |
"ai-deep-max-score": function() { |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
581 |
return new ai.deepMaxScore(ui.brdEngine); |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
582 |
}, |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
583 |
"ai-deep-max-score-corner": function() { |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
584 |
return new ai.deepMaxScoreCorner(ui.brdEngine); |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
585 |
}, |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
586 |
"ai-expectimax": function() { |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
587 |
return new ai.expectimax(ui.brdEngine); |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
588 |
}, |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
589 |
// "": function() { |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
590 |
// return new ai.(ui.brdEngine); |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
591 |
// }, |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
592 |
}; |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
593 |
ui.ai.domList = document.querySelectorAll('div.ai'); |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
594 |
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
|
595 |
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
|
596 |
ui.ai.enable(event.target.parentNode); |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
597 |
}); |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
598 |
} |
0 | 599 |
|
25
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
600 |
ui.ai.moveToTop = function(aiDom) { |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
601 |
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
|
602 |
ui.ai.domList[i].classList.remove('enabled'); |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
603 |
} |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
604 |
var rootDom = aiDom.parentNode; |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
605 |
rootDom.removeChild(aiDom); |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
606 |
rootDom.insertBefore(aiDom, rootDom.firstChild); |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
607 |
aiDom.classList.add('enabled'); |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
608 |
} |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
609 |
ui.ai.enable = function(aiDom) { |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
610 |
if (ui.ai.current) |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
611 |
ui.ai.current.cleanup(); |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
612 |
ui.ai.moveToTop(aiDom); |
27
8f96d09a4d94
Add Blind cycle AI. Add example of setting passing.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
26
diff
changeset
|
613 |
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
|
614 |
} |
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
615 |
|
2ef10a49a28f
Prepare UI for AI configuration.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
21
diff
changeset
|
616 |
ui.brdEngine = BoardArr2d; // TODO make user selectable |
0 | 617 |
|
43
8b1577211149
Save val2048 preference.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
42
diff
changeset
|
618 |
if (localStorage.val2048) { |
8b1577211149
Save val2048 preference.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
42
diff
changeset
|
619 |
try { |
8b1577211149
Save val2048 preference.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
42
diff
changeset
|
620 |
ui.board.val2048Dom.checked = JSON.parse(localStorage.val2048); |
8b1577211149
Save val2048 preference.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
42
diff
changeset
|
621 |
} catch (ex) { } |
8b1577211149
Save val2048 preference.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
42
diff
changeset
|
622 |
} |
41
359719178176
Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
40
diff
changeset
|
623 |
if (localStorage.savedBoard) { |
42
716508d5494c
Save board between browser sessions.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
41
diff
changeset
|
624 |
try { |
45 | 625 |
ui.board.position = JSON.parse(localStorage.savedBoard); |
46
4dadaff6eba4
Update/store/restore game turn. Show max value according to value settings.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
45
diff
changeset
|
626 |
if (localStorage.savedTurn) |
4dadaff6eba4
Update/store/restore game turn. Show max value according to value settings.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
45
diff
changeset
|
627 |
ui.board.turn = JSON.parse(localStorage.savedTurn); |
4dadaff6eba4
Update/store/restore game turn. Show max value according to value settings.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
45
diff
changeset
|
628 |
else |
4dadaff6eba4
Update/store/restore game turn. Show max value according to value settings.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
45
diff
changeset
|
629 |
ui.board.turn = 0; |
45 | 630 |
ui.board.update(ui.board.position); |
46
4dadaff6eba4
Update/store/restore game turn. Show max value according to value settings.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
45
diff
changeset
|
631 |
ui.score.update(ui.board.position, ui.board.turn); |
42
716508d5494c
Save board between browser sessions.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
41
diff
changeset
|
632 |
} catch (ex) { } |
41
359719178176
Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
40
diff
changeset
|
633 |
} else { |
359719178176
Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
40
diff
changeset
|
634 |
ui.action.start(); |
359719178176
Reorganise code. Warn about uninitialised AI.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
40
diff
changeset
|
635 |
} |
32
f48151863cdf
Start game after page load.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
31
diff
changeset
|
636 |
|
0 | 637 |
</script> |
638 |
||
639 |
</body> |
|
640 |
</html> |