-
Notifications
You must be signed in to change notification settings - Fork 0
/
board.js
409 lines (380 loc) · 12.4 KB
/
board.js
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
class Node {
constructor(parent, available_moves) {
this.ri = 0;
this.ni = 0;
this.num_unexpand = available_moves.slice(0);
this.parent = parent;
this.children = {};
}
}
class Game {
constructor(board, available_moves) {
// 0: free cell, 1: black, 2: white
// Access the board cells: row * 19(number of columns) + column
this.board = ((board == undefined)? [] : board.slice(0));
this.available_moves = ((available_moves == undefined)? [] : available_moves.slice(0));
this.height = 11;
this.width = 11;
this.terminal = 0;
if(board == undefined){
for(var i = 0; i < this.height; i++){
for(var j = 0; j < this.width; j++){
this.board.push(0);
}
}
}
if(available_moves == undefined){
for(var i = 0; i < this.height; i++){
for(var j = 0; j < this.width; j++){
this.available_moves.push(i * this.height + j);
}
}
}
}
is_terminal(position) {
let row = Math.floor(position / this.height);
let column = position % this.width;
let vertical = 0, horizontal = 0, top_left = 0, top_right = 0;
for(var y = row-1; y >= 0; y--){
if(this.board[position] != this.board[y*this.height + column])
break;
else
vertical++;
}
for(var y = row+1; y < this.height; y++){
if(this.board[position] != this.board[y*this.height + column])
break;
else
vertical++;
}
if(vertical >= 4){
this.terminal = this.board[position];
return this.board[position];
}
for(var x = column-1; x >= 0; x--){
if(this.board[position] != this.board[row*this.height + x])
break;
else
horizontal++;
}
for(var x = column+1; x < this.width; x++){
if(this.board[position] != this.board[row*this.height + x])
break;
else
horizontal++;
}
if(horizontal >= 4){
this.terminal = this.board[position];
return this.board[position];
}
x = column - 1;
y = row - 1;
while(x >= 0 && y >= 0){
if(this.board[position] != this.board[y*this.height + x])
break;
else
top_left++;
x--;
y--;
}
x = column + 1;
y = row + 1;
while(x < this.width && y < this.height){
if(this.board[position] != this.board[y*this.height + x])
break;
else
top_left++;
x++;
y++;
}
if(top_left >= 4){
this.terminal = this.board[position];
return this.board[position];
}
x = column + 1;
y = row - 1;
while(x < this.width && y >= 0){
if(this.board[position] != this.board[y*this.height + x])
break;
else
top_right++;
x++;
y--;
}
x = column - 1;
y = row + 1;
while(x >= 0 && y < this.height){
if(this.board[position] != this.board[y*this.height + x])
break;
else
top_right++;
x--;
y++;
}
if(top_right >= 4){
this.terminal = this.board[position];
return this.board[position];
}
this.terminal = 0;
return 0;
}
step(position, player) {
this.board[position] = player;
this.available_moves = this.available_moves.filter(pos => pos !== position);
}
print_board() {
for(var i = 0; i < this.height; i++){
var row = []
for(var j = 0; j < this.width; j++){
row.push(this.board[i * this.width + j]);
}
console.log(row);
}
}
is_valid_step(position) {
return (position >= 0 && position < (this.width * this.height) && this.board[position] == 0);
}
get_height() {
return this.height;
}
get_width() {
return this.width;
}
clone() {
return new Game(this.board, this.available_moves);
}
}
class MCTS {
constructor(num_sim, C, chess) {
this.num_sim = ((num_sim == undefined)? 500 : num_sim);
this.C = ((C == undefined)? 1.41 : C);
this.N = 0;
this.chess = ((chess == undefined)? 2 : chess);
}
search(game, cur_root_node) {
for(var itr = 0; itr < this.num_sim; itr++){
let pack = this.select_and_expand(cur_root_node, game); // pack[0]: expanded node, pack[1]: game after expanding, pack[2]: player
let result = this.simulate(pack[1], pack[2]);
this.backprop(result, pack[0]);
}
return parseInt(this.get_best_child(cur_root_node, false));
}
select_and_expand(node, game) {
let clone_game = game.clone();
this.N = node.ni;
let player = true; // true: AI itself, false: human (its enemy)
while(clone_game.available_moves.length > 0){//node.num_unexpand.length == 0){
// expand
if(node.num_unexpand.length != 0){
let idx = Math.floor(Math.random() * node.num_unexpand.length);
let position = node.num_unexpand[idx];
if(player) clone_game.step(position, this.chess);
else clone_game.step(position, 3 - this.chess);
player = !player;
let new_child = new Node(node, clone_game.available_moves);
node.children['' + position] = new_child;
node.num_unexpand = node.num_unexpand.filter(pos => pos !== position);
clone_game.is_terminal(position);
return [new_child, clone_game, player];
}
if(player){ // AI's turn
let key = this.get_best_child(node, true);
node = node.children[key];
player = false;
clone_game.step(parseInt(key), this.chess);
if(clone_game.is_terminal(parseInt(key)))
return [node, clone_game, player];
}
else{ // human's turn
let key = this.get_worse_child(node);
node = node.children[key];
player = true;
clone_game.step(parseInt(key), 3 - this.chess);
if(clone_game.is_terminal(parseInt(key)))
return [node, clone_game, player];
}
}
return [node, clone_game, player];
}
simulate(expanded_game, player) {
if(expanded_game.terminal == this.chess) {
// expanded_game.print_board();
return 1;
}
else if(expanded_game.terminal == (3 - this.chess)) {
// expanded_game.print_board();
return -1;
}
while(expanded_game.available_moves.length > 0){
let idx = Math.floor(Math.random() * expanded_game.available_moves.length);
let position = expanded_game.available_moves[idx];
if(player){ // AI's turn
expanded_game.step(position, this.chess);
player = false;
}
else{ // human's turn
expanded_game.step(position, 3 - this.chess);
player = true;
}
let terminal = expanded_game.is_terminal(position);
if(terminal == this.chess) {
// expanded_game.print_board();
return 1;
}
else if(terminal == (3 - this.chess)) {
// expanded_game.print_board();
return -1;
}
}
return 0; // draw
}
backprop(result, expanded_node) {
// this.N += 1;
let node = expanded_node;
while(node !== null){
node.ri += result;
node.ni += 1;
node = node.parent;
}
}
get_best_child(node, explore) {
// return the key of the best child
let max_key = "500";
let max_ucb = -Infinity;
for(let child_key in node.children){
let child = node.children[child_key];
let cur_ucb;
if(explore) cur_ucb = (child.ri / child.ni) + this.C * Math.sqrt(((Math.log(this.N)) / child.ni));
else cur_ucb = (child.ri / child.ni);
if(cur_ucb > max_ucb){
max_ucb = cur_ucb;
max_key = child_key;
}
}
return max_key;
}
get_worse_child(node) {
// return the key of the worse child
let min_key = "500";
let min_ucb = Infinity;
for(let child_key in node.children){
let child = node.children[child_key];
let cur_ucb = (child.ri / child.ni) + this.C * Math.sqrt(((Math.log(this.N)) / child.ni));
if(cur_ucb < min_ucb){
min_ucb = cur_ucb;
min_key = child_key;
}
}
return min_key;
}
}
let player = 1;
let human_position = -1;
let cells = [];
let game;
let mcts;
let toggle_color = false;
let interval;
let msg_box;
function initialize_board(){
if(cells.length == (game.width * game.height)){
for(let i = 0; i < (game.width * game.height); i++){
cells[i].classList.remove("black");
cells[i].classList.remove("white");
}
return;
}
let B = document.getElementById("board");
for(let i = 0; i < game.height; i++){
let row = document.createElement("div");
row.classList.add("row");
B.appendChild(row);
for(let j = 0; j < game.width; j++){
let cell = document.createElement("div");
cell.addEventListener("click", function(e){
if(player == 2){
return -1;
}
// e.target.classList.add("black");
human_position = cells.indexOf(e.target);
});
cell.classList.add("cell");
row.appendChild(cell);
cells.push(cell);
}
}
}
function game_exe() {
let row;
let column;
let position;
if(player == 1){
msg_box.innerHTML = "";
msg_box.innerHTML = "Your turn";
if(human_position == -1)
return;
else
position = human_position;
}
else if(player == 2){
msg_box.innerHTML = "";
msg_box.innerHTML = "AI's turn 🤖";
let node = new Node(null, game.available_moves);
position = mcts.search(game, node);
if(mcts.C >= 1) mcts.C -= 0.1;
}
if(game.is_valid_step(position)){
game.step(position, player);
game.print_board();
if(player == 1){
player = 2;
if(!toggle_color) cells[position].classList.add("black");
else cells[position].classList.add("white");
msg_box.innerHTML = "";
msg_box.innerHTML = "AI's turn 🤖";
}
else{
player = 1;
if(!toggle_color) cells[position].classList.add("white");
else cells[position].classList.add("black");
human_position = -1;
msg_box.innerHTML = "";
msg_box.innerHTML = "Your turn";
}
if(game.is_terminal(position)){
if(player == 2) {
msg_box.innerHTML = "";
msg_box.innerHTML = "You Win 🎉";
}
else if(player == 1) {
msg_box.innerHTML = "";
msg_box.innerHTML = "AI Wins 👁";
}
clearInterval(interval);
return;
}
}
else if(player == 1){
// alert("Invalid move!");
}
}
function play(turn){
player = turn;
msg_box = document.getElementById("msg-box");
if(player == 1){
toggle_color = false;
msg_box.innerHTML = "";
msg_box.innerHTML = "Your turn";
}
else{
toggle_color = true;
msg_box.innerHTML = "";
msg_box.innerHTML = "AI's turn 🤖";
}
human_position = -1;
clearInterval(interval);
game = new Game();
mcts = new MCTS(50000, 2, 2);
initialize_board();
interval = setInterval(game_exe, 50);
}