-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathman_vs_machine.js
More file actions
326 lines (292 loc) · 9.94 KB
/
man_vs_machine.js
File metadata and controls
326 lines (292 loc) · 9.94 KB
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
// Builds a convnet.js Vol encoding the current board, ko value, and
// ko value in the liberties format. Also performs edge encoding for
// a pad of 3
function buildLibertyEncoding(jboard, forPlayer, ko) {
var data = new convnetjs.Vol(25, 25, 8, 0);
// Set all squares in the edge channel to 1
for (x = 0; x < 25; x++) {
for (y = 0; y < 25; y++) {
if (x < 3 || x >= 22 || y < 3 || y >= 22) {
data.set(x, y, 7, 1);
}
}
}
// Now iterate through the non-edge squares and encode the liberties
for (x = 3; x < 22; x++) {
for (y = 3; y < 22; y++) {
var coor = new JGO.Coordinate(x - 3, y - 3);
var type = jboard.getType(coor);
if (type != JGO.CLEAR) {
var groups = jboard.getGroup(coor);
var neighbors = groups.neighbors;
var numLiberties = 0;
for (var i = 0; i < neighbors.length; i++) {
if (jboard.getType(neighbors[i]) == JGO.CLEAR) {
numLiberties += 1
if (numLiberties == 3) {
break;
}
}
}
if (numLiberties == 0) {
throw "Liberties should never be zero!";
}
var depth = numLiberties - 1;
if (type != forPlayer) {
depth += 3;
}
data.set(x, y, depth, 1);
}
}
}
// Finally set the ko value
if (ko) {
data.set(ko.i + 3, ko.j + 3, 6, 1);
}
return data;
};
// Logs a Go position encoding inside a convjs.Vol in the liberties format to
// the console in human readonable format, just for debugging
function logLibertyData(data) {
var names =["P1", "P2", "P3", "O1", "O2", "O3", "KO", "ED", "__"];
var str = ""
for (y = 0; y < 25; y++) {
for (x = 0; x < 25; x++) {
var setIndex = 8;
for (i = 0; i < 8; i++) {
var point = data.get(x, y, i);
// Sanity check to ensure we have one value per coordinate
if (point != 0 && point != 1) {
throw "Got point " + point;
}
if (point != 0 && setIndex != 8) {
throw "Got two points at position " + x + "," + y +
" points: " + i + " " + setIndex;
}
if (point == 1) {
setIndex = i;
}
}
str += names[setIndex] + " ";
}
str += "\n";
}
console.log(str);
}
var firstNode = new JGO.Node(jboard, null,
{ko_i: false, ko_j: false, i: false,
blackCaptures: 0, whiteCaptures: 0,
j: false, eval: false});
var nodes = [firstNode]; // Nodes reflecting the moves made so far
var onMove = 0; // which node within `nodes` we are displaying currently
var showAnalysis = false; // Are we showing the conv net's analysis or not?
var showHover = false;
var lastHover = false, lastX = -1, lastY = -1; // hover helper vars
var net = new convnetjs.Net();
net.fromJSON(json_net);
var jboard = new JGO.Board(19, 19);
var boardCanvas = false;
var jsetup = new JGO.Setup(jboard, JGO.BOARD.medium);
jsetup.setOptions({stars: {points:5}});
function autoMove() {
return document.getElementById("autoMoveCheckbox").checked;
}
function getCurrentPlayer() {
return (onMove % 2 == 0) ? JGO.BLACK : JGO.WHITE;
}
function getCurrentKo() {
var info = nodes[onMove].info;
if (info.ko_i === false) {
return false;
} else {
return new JGO.Coordinate(info.ko_i, info.ko_j);
}
}
function getLastMove() {
var info = nodes[onMove].info;
if (info.i === false) {
return false;
} else {
return new JGO.Coordinate(info.i, info.j);
}
}
function showAnalysisClicked() {
showAnalysis = document.getElementById("showAnalysis").checked;
if (showAnalysis) {
p = getConvPrediction();
jboard.shades = p;
} else {
jboard.shades = false;
}
boardCanvas.draw(jboard, 0, 0, 19, 19);
}
function goToMove(increment) {
if (increment > 0) {
var target = Math.min(onMove + increment, nodes.length - 1);
while(onMove < target) {
onMove += 1
nodes[onMove].apply();
}
} else {
var target = Math.max(onMove + increment, 0);
while(onMove > target) {
nodes[onMove].revert();
onMove -= 1;
}
}
if (showAnalysis) {
var p = getConvPrediction();
jboard.shades = p;
boardCanvas.draw(jboard, 0, 0, 19, 19);
}
var info = nodes[onMove].info;
document.getElementById("move").innerHTML = onMove;
document.getElementById("black_captures").innerHTML = info.blackCaptures;
document.getElementById("white_captures").innerHTML = info.whiteCaptures;
}
function makeMove(coord) {
var player = getCurrentPlayer();
var ko = getCurrentKo();
var lastMove = getLastMove();
var play = jboard.playMove(coord, player, ko);
if(play.success) {
if (onMove != nodes.length - 1) {
nodes = nodes.slice(0, onMove + 1);
}
var previous = nodes[onMove].info;
var blackCaptures = previous.blackCaptures;
var whiteCaptures = previous.whiteCaptures;
if (player == JGO.BLACK) {
blackCaptures += play.captures.length;
} else {
whiteCaptures += play.captures.length;
}
var node = new JGO.Node(jboard, null,
{i: coord.i, ko_i: false, ko_j: false,
blackCaptures: blackCaptures, whiteCaptures: whiteCaptures,
j: coord.j, eval: false});
node.setType(coord, player);
node.setType(play.captures, JGO.CLEAR); // clear opponent's stones
if(lastMove)
node.setMark(lastMove, JGO.MARK.NONE); // clear previous mark
if(ko)
node.setMark(ko, JGO.MARK.NONE); // clear previous ko mark
node.setMark(coord, JGO.MARK.CIRCLE); // mark move
lastMove = coord;
if (play.ko) {
// jgoboard has a bug with snapbacks meaning moves that capture more than one
// one stone might get marked as KO, so we manually check it here.
var next_player = (player == JGO.WHITE) ? JGO.BLACK : JGO.WHITE
var ko_play = jboard.playMove(play.ko, next_player, false);
if (ko_play.success) {
if (ko_play.captures.length == 1) {
node.info["ko_i"] = play.ko.i;
node.info["ko_j"] = play.ko.j;
node.setMark(play.ko, JGO.MARK.CIRCLE);
}
}
}
onMove += 1;
nodes.push(node);
document.getElementById("move").innerHTML = onMove;
document.getElementById("moves").innerHTML = nodes.length - 1;
node.apply();
if (showAnalysis) {
jboard.shades = getConvPrediction();
boardCanvas.draw(jboard, 0, 0, 19, 19);
}
document.getElementById("black_captures").innerHTML = blackCaptures;
document.getElementById("white_captures").innerHTML = whiteCaptures;
return true;
} else {
alert('Illegal move: ' + play.errorMsg);
return false;
}
}
/** Scale an array so the maximum value is 1 */
function scaleArray(arr) {
var m = Math.max.apply(null, arr);
for (var i = 0; i < arr.length; i++) {
arr[i] /= m;
}
}
/** Return the DCNN predictions with illegals moves masked for the current position
the output is pre-normalized so the largest value is 1.0 */
function getConvPrediction() {
cur = nodes[onMove];
if (cur.info.convPredictions) {
// We have already pre-computed this for our node
return cur.info.convPredictions;
}
var ko = getCurrentKo();
var player = getCurrentPlayer();
var encoding = buildLibertyEncoding(jboard, player, ko);
var p = net.forward(encoding).w;
var maxv = encoding[0];
var maxi = 0;
for(var i=1;i<p.length;i++) {
var y = Math.floor(i / 19);
var x = i % 19;
var coord = new JGO.Coordinate(x, y);
// We need to check playMove here to make sure this move is not a suicide
if (!jboard.playMove(coord, player, ko).success) {
p[i] = 0;
}
}
scaleArray(p);
// Cache our predictions
nodes[onMove].info.convPredictions = p;
return p;
}
/** Have the conv. net make a move */
function convMove() {
var p = getConvPrediction();
var maxi = 0;
var maxv = -1;
for(var i=0;i<p.length;i++) {
var y = Math.floor(i / 19);
var x = i % 19;
var coord = new JGO.Coordinate(x, y);
if(p[i] > maxv) { maxv = p[i]; maxi = i;}
}
var p_y = Math.floor(maxi / 19);
var p_x = maxi % 19;
coord = new JGO.Coordinate(p_x, p_y);
makeMove(coord);
}
jsetup.create('board', function(canvas) {
boardCanvas = canvas;
canvas.addListener('click', function(coord, ev) {
var player = getCurrentPlayer();
var ko = getCurrentKo();
var opponent = (player == JGO.BLACK) ? JGO.WHITE : JGO.BLACK;
// clear hover away - it'll be replaced or then it will be an illegal move
// in any case so no need to worry about putting it back afterwards
if(lastHover)
jboard.setType(new JGO.Coordinate(lastX, lastY), JGO.CLEAR);
lastHover = false;
var legalMove = makeMove(coord);
if (legalMove && autoMove()) {
convMove();
}
});
canvas.addListener('mousemove', function(coord, ev) {
var player = getCurrentPlayer();
if(coord.i == -1 || coord.j == -1 || (coord.i == lastX && coord.j == lastY))
return;
if(lastHover) // clear previous hover if there was one
jboard.setType(new JGO.Coordinate(lastX, lastY), JGO.CLEAR);
lastX = coord.i;
lastY = coord.j;
if(jboard.getType(coord) == JGO.CLEAR && jboard.getMark(coord) == JGO.MARK.NONE) {
jboard.setType(coord, player == JGO.WHITE ? JGO.DIM_WHITE : JGO.DIM_BLACK);
lastHover = true;
} else
lastHover = false;
});
canvas.addListener('mouseout', function(ev) {
if(lastHover)
jboard.setType(new JGO.Coordinate(lastX, lastY), JGO.CLEAR);
lastHover = false;
});
});