-
Notifications
You must be signed in to change notification settings - Fork 11
/
sketch.js
216 lines (180 loc) · 6.52 KB
/
sketch.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
/// <reference path="./lib/p5.global-mode.d.ts" />
let TTT_BOARD;
let canvas;
var whoseTurn;
var hoveredTile = -1;
var tileSize;
var whoseTurnSpan;
var machineControlsArea;
var whoseturnArea;
var startingPlayerArea;
var gameOverArea;
var gameOverWinner;
var mctsTimeoutSlider;
var mctsTimeoutSpan;
const GameStates = Object.freeze({
SELECT_STARTING_PLAYER: 0,
WAITING_HUMAN_MOVE: 1,
WAITING_MACHINE_MOVE: 2,
GAME_OVER: 3,
RUNNING_VIS: 4
});
var currentGameState;
const s = (sketch) => {
sketch.setup = () => {
canvas = sketch.createCanvas(200, 200);
tileSize = (sketch.width - 20)/3;
whoseTurnSpan = sketch.select("#whoseturn");
machineControlsArea = sketch.select("#machine_controls_area");
whoseturnArea = sketch.select("#whoseturn_area");
startingPlayerArea = sketch.select("#starting_player_area");
gameOverArea = sketch.select("#game_over_area");
gameOverWinner = sketch.select("#game_over_winner");
mctsTimeoutSlider = sketch.select("#mcts_timeout_slider");
mctsTimeoutSpan = sketch.select("#mcts_timeout_span");
sketch.reset();
};
sketch.draw = () => {
sketch.handleHover();
sketch.drawBoard();
mctsTimeoutSpan.html(mctsTimeoutSlider.value());
};
sketch.reset = () => {
TTT_BOARD = new TicTacToeBoard();
whoseTurn = sketch.round(sketch.random(0, 1));
sketch.stateTransition(GameStates.SELECT_STARTING_PLAYER);
}
sketch.drawBoard = () => {
sketch.translate(10, 10);
for (var i = 0; i < 3; i++) {
for (var j = 0; j < 3; j++) {
let tile = TTT_BOARD.grid[j + i*3];
if (hoveredTile == (j + i*3) && currentGameState == GameStates.WAITING_HUMAN_MOVE
&& TTT_BOARD.isLegalPosition(j + i*3)) {
sketch.fill(200, 200, 200);
} else if (tile == "h") {
sketch.fill(100, 100, 240);
} else if (tile == "m") {
sketch.fill(240, 100, 100);
} else {
sketch.fill(255, 255, 255);
}
sketch.rect(j * tileSize, i * tileSize, tileSize, tileSize);
sketch.fill(0);
sketch.textSize(40);
sketch.textAlign(sketch.CENTER, sketch.CENTER);
sketch.text(tile, j * tileSize + tileSize/2, i * tileSize + tileSize/2);
}
}
}
sketch.disableEverythingHTML = () => {
startingPlayerArea.hide();
whoseturnArea.hide();
machineControlsArea.hide();
gameOverArea.hide();
}
sketch.stateTransition = (newGameState) => {
sketch.disableEverythingHTML();
switch (newGameState) {
case GameStates.SELECT_STARTING_PLAYER:
startingPlayerArea.show();
break;
case GameStates.WAITING_HUMAN_MOVE:
whoseTurn = PLAYER.HUMAN;
whoseturnArea.show();
whoseTurnSpan.html(whoseTurn == PLAYER.HUMAN ? "HUMAN" : "MACHINE");
break;
case GameStates.WAITING_MACHINE_MOVE:
whoseTurn = PLAYER.MACHINE;
whoseturnArea.show();
whoseTurnSpan.html(whoseTurn == PLAYER.HUMAN ? "HUMAN" : "MACHINE");
machineControlsArea.show();
break;
case GameStates.RUNNING_VIS:
machineControlsArea.hide();
break;
case GameStates.GAME_OVER:
gameOverArea.show();
let winner = TTT_BOARD.checkWin();
switch (winner) {
case "h": winner = "HUMAN"; break;
case "m": winner = "MACHINE"; break;
case "v": winner = "DRAW"; break;
}
gameOverWinner.html(winner);
break;
}
currentGameState = newGameState;
}
sketch.handleHover = () => {
let mouseX = sketch.mouseX;
let mouseY = sketch.mouseY;
for (var i = 0; i < 3; i++) {
for (var j = 0; j < 3; j++) {
if (mouseX > (j * tileSize) && mouseX < ((j+1) * tileSize)
&& mouseY > (i * tileSize) && mouseY < ((i+1) * tileSize)) {
hoveredTile = j + i*3;
return;
}
}
}
hoveredTile = -1;
}
sketch.selectStartingPlayer = (player) => {
whoseTurn = player;
sketch.stateTransition(
whoseTurn == PLAYER.HUMAN ?
GameStates.WAITING_HUMAN_MOVE :
GameStates.WAITING_MACHINE_MOVE);
}
sketch.mouseClicked = () => {
if (hoveredTile != -1 && whoseTurn == PLAYER.HUMAN && TTT_BOARD.isLegalPosition(hoveredTile)) {
TTT_BOARD.humanMakeMove(hoveredTile);
sketch.endMove(PLAYER.HUMAN);
}
}
sketch.machineRandomMove = () => {
TTT_BOARD.makeRandomMove(PLAYER.MACHINE);
sketch.endMove(PLAYER.MACHINE);
}
sketch.machineMctsMove = () => {
let monteCarlo = new MCTS(TTT_BOARD.copy(), PLAYER.MACHINE);
let MCTS_search = monteCarlo.runSearch(mctsTimeoutSlider.value());
setMCTS(monteCarlo, MCTS_search);
sketch.stateTransition(GameStates.RUNNING_VIS);
}
sketch.endMove = (player) => {
if (TTT_BOARD.checkWin() != "") {
sketch.stateTransition(GameStates.GAME_OVER);
} else {
sketch.stateTransition(player == PLAYER.HUMAN ?
GameStates.WAITING_MACHINE_MOVE :
GameStates.WAITING_HUMAN_MOVE);
}
}
sketch.makeMove = (move) => {
TTT_BOARD.makeMove(move);
}
};
let myp5 = new p5(s, "canvascontainer");
function testMCTS() {
let results = {"h": 0, "m": 0, "v": 0};
for (var i = 0; i < 100; i++) {
let board = new TicTacToeBoard();
let player = myp5.int(myp5.random(2)) % 2 == 1 ? PLAYER.MACHINE : PLAYER.HUMAN;
while (board.checkWin() == "") {
if (player == PLAYER.MACHINE) {
let mcts_model = new MCTS(board.copy(), player);
let mcts_search = mcts_model.runSearch(0.4);
board.makeMove(mcts_search.move);
} else {
board.makeRandomMove(player);
}
// board.print();
player = player == PLAYER.MACHINE ? PLAYER.HUMAN : PLAYER.MACHINE;
}
if (i % 10 == 0) { console.log("i: " + i); };
results[board.checkWin()] += 1;
}
console.log(results);
}