-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGUI.html
337 lines (310 loc) · 8.46 KB
/
GUI.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
<!DOCTYPE html>
<!-- This works with "node ws.mjs" running as Websocket server -->
<html>
<head>
<title>Tic Tac Toe GUI</title>
<style>
p, label {
font-size: 25px;
}
table {
border: 2px solid black;
table-layout: fixed;
}
td.sq {
text-align: center;
vertical-align: middle;
border: 2px solid black;
width: 50px;
height: 50px;
overflow: hidden;
font-size: 25px;
}
td.SQ {
text-align: center;
vertical-align: middle;
border: 2px solid black;
border-bottom-style: none;
width: 90px;
height: 50px;
overflow: hidden;
font-size: 25px;
}
td.Value, td.Q {
text-align: right;
border: 1px solid black;
width: 70px;
height: 20px;
}
td.mem {
text-align: center;
border: 2px solid black;
width: 30px;
height: 30px;
}
td.Mem {
text-align: center;
vertical-align: middle;
border: 2px solid black;
border-bottom-style: none;
width: 70px;
height: 50px;
}
input[type=radio] {
transform: scale(2);
}
input[type=checkbox] {
transform: scale(2);
}
button {
font-size: 20px;
}
</style>
</head>
<body>
<h1><!-- input type="radio" id="TTT" name="selector" -->
TicTacToe GUI</h1>
<!--h1><input type="radio" id="QQQ" name="selector" checked>
Visualization of Q values</h1-->
<p>Render:
<input type="checkbox" name="render"><label> end states</label>
<input type="checkbox" name="render"><label> all states</label>
<input type="checkbox" name="render"><label> actions</label>
<input type="checkbox" name="render"><label> Q values</label>
</p>
<p>Click on square to change piece. <font color="green">Green shades</font> indicate probability.</p>
<table id="board2">
<tr>
<td class='SQ' id='SQ0'></td>
<td class='SQ' id='SQ1'></td>
<td class='SQ' id='SQ2'></td>
</tr>
<tr>
<td class='Value' id='v0'></td>
<td class='Value' id='v1'></td>
<td class='Value' id='v2'></td>
</tr>
<tr>
<td class='Q' id='q0'></td>
<td class='Q' id='q1'></td>
<td class='Q' id='q2'></td>
</tr>
<tr>
<td class='SQ' id='SQ3'></td>
<td class='SQ' id='SQ4'></td>
<td class='SQ' id='SQ5'></td>
</tr>
<tr>
<td class='Value' id='v3'></td>
<td class='Value' id='v4'></td>
<td class='Value' id='v5'></td>
</tr>
<tr>
<td class='Q' id='q3'></td>
<td class='Q' id='q4'></td>
<td class='Q' id='q5'></td>
</tr>
<tr>
<td class='SQ' id='SQ6'></td>
<td class='SQ' id='SQ7'></td>
<td class='SQ' id='SQ8'></td>
</tr>
<tr>
<td class='Value' id='v6'></td>
<td class='Value' id='v7'></td>
<td class='Value' id='v8'></td>
</tr>
<tr>
<td class='Q' id='q6'></td>
<td class='Q' id='q7'></td>
<td class='Q' id='q8'></td>
</tr>
</table>
<h3>Intermediate thoughts:</h3>
<table id="memory2">
<tr>
<td class='Mem' id='M0'>0</td>
<td class='Mem' id='M1'>0</td>
<td class='Mem' id='M2'>0</td>
<td class='Mem' id='M3'>0</td>
<td class='Mem' id='M4'>0</td>
<td class='Mem' id='M5'>0</td>
<td class='Mem' id='M6'>0</td>
<td class='Mem' id='M7'>0</td>
<td class='Mem' id='M8'>0</td>
</tr>
<tr>
<td class='Value' id='mv0'></td>
<td class='Value' id='mv1'></td>
<td class='Value' id='mv2'></td>
<td class='Value' id='mv3'></td>
<td class='Value' id='mv4'></td>
<td class='Value' id='mv5'></td>
<td class='Value' id='mv6'></td>
<td class='Value' id='mv7'></td>
<td class='Value' id='mv8'></td>
</tr>
</table>
<br>
<button onclick="clearBoard()">Clear board and memories</button>
<br>
<button onclick="exitVisualization()">Exit visualization</button>
<!-- p id="gameOver" style="color:red; font-size:40px; display:none">Game Over</p-->
<ul id="messages"></ul>
<p>To visualize Q values:</p>
<ul>
<li>In terminal: node ws.mjs &</li>
<li>Open GUI.html in web browser</li>
<li>Ctrl-C in Python TicTacToe, choose option 'v'</li>
</ul>
<p>For debugging web socket: <button onclick="sendTestMsg()">Test Websocket</button></p>
<script>
const ws = new WebSocket("ws://127.0.0.1:5678/");
// const messages = document.getElementById('messages');
// const gameOver = document.getElementById('gameOver');
var state = new Array(9).fill(0);
var memory = new Array(9).fill(0);
var data = [];
ws.onmessage = function (event) {
// gameOver.style.display = 'none';
[tag, data] = JSON.parse(event.data);
// console.log("data =", data);
if (tag == 'ask') {
boxes = document.getElementsByName('render');
var flag = (boxes[0].checked ? 1 : 0) +
(boxes[1].checked ? 2 : 0) +
(boxes[2].checked ? 4 : 0) +
(boxes[3].checked ? 8 : 0);
ws.send(JSON.stringify(flag));
return;
}
if (tag == 'Q-vals') {
for (let i = 0; i < 9; i++) {
const q = document.getElementById('q' + i.toString());
// const sq = document.getElementById('SQ' + i.toString());
q.innerText = data[i].toFixed(5).toString();
// var shade = ((100 - data[i] * 100) * .6 + 40).toString();
// sq.style.backgroundColor = "hsl(128,100%," + shade + "%)";
// q.style.backgroundColor = "hsl(128,100%," + shade + "%)";
}
}
if (tag == 'probs') {
for (let i = 0; i < 9; i++) {
const v = document.getElementById('v' + i.toString());
// const sq = document.getElementById('SQ' + i.toString());
v.innerText = data[i].toFixed(5).toString();
var shade = ((100 - data[i] * 100) * .8 + 20).toString();
// sq.style.backgroundColor = "hsl(128,100%," + shade + "%)";
v.style.backgroundColor = "hsl(128,100%," + shade + "%)";
}
}
if (data.length == 18) {
for (let i = 0; i < 9; i++) {
const v = document.getElementById('v' + i.toString());
const sq = document.getElementById('SQ' + i.toString());
v.innerText = data[i].toFixed(5).toString();
var shade = ((100 - data[i] * 100) * .6 + 40).toString();
sq.style.backgroundColor = "hsl(128,100%," + shade + "%)";
v.style.backgroundColor = "hsl(128,100%," + shade + "%)";
}
for (let i = 0; i < 9; i++) {
const m = document.getElementById('M' + i.toString());
const mv = document.getElementById('mv' + i.toString());
mv.innerText = data[9 + i].toFixed(5).toString();
var shade = ((100 - data[9 + i] * 100) * .6 + 40).toString();
m.style.backgroundColor = "hsl(128,100%," + shade + "%)";
mv.style.backgroundColor = "hsl(128,100%," + shade + "%)";
}
}
if (tag == 'move') {
if (data > 8) {
const i = data - 9;
// console.log(i);
const m = document.getElementById('M' + i.toString());
m.textContent = '1';
m.style.backgroundColor = 'green';
}
else {
for (let j = 0; j < 9; j++) {
const m = document.getElementById('M' + j.toString());
m.textContent = '0';
m.style.backgroundColor = 'white';
}
const i = data;
const sq = document.getElementById('SQ' + i.toString());
sq.textContent = '⨯';
sq.style.backgroundColor = 'green';
}
}
if (tag == 'board') {
for (let i = 0; i < 9; i++) {
const sq = document.getElementById('SQ' + i.toString());
sq.textContent = data[i] == -1 ? '⨯' :
data[i] == 1 ? '⭘' :
data[i] == 2 ? '!' : ' ';
sq.style.backgroundColor = data[i] == 2 ? 'red' :
data[i] == 1 ? 'white' :
data[i] == -1 ? 'green' : 'white';
}
}
/* var message = document.createElement('li'),
content = document.createTextNode(event.data);
message.appendChild(content);
messages.appendChild(message); */
};
function sendTestMsg() {
ws.send(JSON.stringify(0));
}
function clearBoard() {
for (let i = 0; i < 9; i++) {
const sq = document.getElementById('SQ' + i.toString());
state[i] = 0;
sq.innerText = '';
const m = document.getElementById('M' + i.toString());
memory[i] = 0;
m.innerText = '0';
}
ws.send(JSON.stringify([state, memory]));
}
for (let i = 0; i < 9; i++) {
const sq = document.getElementById('SQ' + i.toString());
sq.addEventListener("click", function() {
if (sq.innerText == '') {
state[i] = -1;
sq.innerText = '⨯';
}
else if (sq.innerText == '⨯') {
state[i] = 1;
sq.innerText = '⭘';
}
else if (sq.innerText == '⭘') {
state[i] = 0;
sq.innerText = '';
}
// send state to RL algorithm:
ws.send(JSON.stringify([state, memory]));
// then RL sends logits to us...
} );
}
for (let i = 0; i < 9; i++) {
const m = document.getElementById('M' + i.toString());
m.addEventListener("click", function() {
if (m.innerText == '1') {
memory[i] = 0;
m.innerText = '0';
}
else {
memory[i] = 1;
m.innerText = '1';
}
// send state to RL algorithm:
ws.send(JSON.stringify([state, memory]));
// then RL sends logits to us...
} );
}
function exitVisualization() {
ws.send(JSON.stringify([0, 0]));
}
</script>
</body>
</html>