-
Notifications
You must be signed in to change notification settings - Fork 1
/
virus-worker.js
367 lines (338 loc) · 10.7 KB
/
virus-worker.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
/**
This is the worker. It is used by virus.html to perform all the CPU-intensive
processing, so the GUI will remain responsive. This worker is given the state of the grid,
and performs the computations that are used to find the best move.
The worker is initialized and given instructions by virus.html.
The worker sends the state information back to virus.html to be drawn on the screen
so the user can see what the new state is.
**/
//Virus class, used to track which player has control of a square, and how much life is left.
function Virus(player){
this.player = player;
this.life = 5;
}
//Node class, used nodes in a tree.
function Node(parent,grid,player,children){
this.parent = parent;
this.grid = grid;
this.player = player;
this.children = children;
}
//some local variables used by the worker to track it's state.
var config = new Object();
var grid;
//this is the function that is called whenever the worker receives a message.
//based on the content of the message (event.data.do), do the appropriate action.
onmessage = function(event) {
switch(event.data.do){
case 'move':
grid = event.data.grid;
move();
break;
case 'init':
config = event.data.config;
break;
}
}
//this function generates a node based on the input given
//move is from x,y to new_x,new_y. Generate a new state based on this move.
function generate_node(node,nextPlayer,x,y,new_x,new_y,jump){
//create a new board
var newGrid = new Array();
for(var i=0;i<config.grid_size;i++){
newGrid[i] = new Array();
}
for(var i=0;i<config.grid_size;i++){
for(var j=0;j<config.grid_size;j++){
if(node.grid[i][j] != undefined){
//create a new Virus instance, because objects are pass by reference.
newGrid[i][j] = new Virus(node.grid[i][j].player);
//set life to one less than last turn.
newGrid[i][j].life = node.grid[i][j].life-1;
//if no life left, virus dies.
if(newGrid[i][j].life <= 0)
newGrid[i][j] = undefined;
}
}
}
//update the game with the new move
newGrid[new_x][new_y] = new Virus(config.player);
if(jump){
newGrid[x][y] = undefined;
}
//as name suggests, infect ajacent blocks
claim_adjacent_blocks(newGrid,new_x,new_y,config.player);
return new Node(undefined,newGrid,nextPlayer,new Array());
}
//given a state, generate all possible next states.
function generate_children(node){
var children = new Array();
for(var x=0;x<config.grid_size;x++){
for(var y=0;y<config.grid_size;y++){
if(node.grid[x][y] != undefined && node.grid[x][y].player == node.player){
var nextPlayer;
if(node.player == 1)
nextPlayer = 2;
else
nextPlayer = 1;
//Single Moves
//N
if(y-1 >= 0 && grid[x][y-1] == undefined){
children.push(generate_node(node,nextPlayer,x,y,x,y-1));
}
//E
if(x+1 < config.grid_size && grid[x+1][y] == undefined){
children.push(generate_node(node,nextPlayer,x,y,x+1,y));
}
//S
if(y+1 < config.grid_size && grid[x][y+1] == undefined){
children.push(generate_node(node,nextPlayer,x,y,x,y+1));
}
//W
if(x-1 >= 0 && grid[x-1][y] == undefined){
children.push(generate_node(node,nextPlayer,x,y,x-1,y));
}
//NE
if(x+1 < config.grid_size && y-1 >= 0 && grid[x+1][y-1] == undefined){
children.push(generate_node(node,nextPlayer,x,y,x+1,y-1));
}
//SE
if(x+1 < config.grid_size && y+1 < config.grid_size && grid[x+1][y+1] == undefined){
children.push(generate_node(node,nextPlayer,x,y,x+1,y+1));
}
//SW
if(x-1 >= 0 && y+1 < config.grid_size && grid[x-1][y+1] == undefined){
children.push(generate_node(node,nextPlayer,x,y,x-1,y+1));
}
//NW
if(x-1 >= 0 && y-1 >= 0 && grid[x-1][y-1] == undefined){
children.push(generate_node(node,nextPlayer,x,y,x-1,y-1));
}
//Double Moves, Removes the original block
//N
if(y-2 >= 0 && grid[x][y-2] == undefined){
children.push(generate_node(node,nextPlayer,x,y,x,y-2,true));
}
//NNE
if(x+1 < config.grid_size && y-2 >= 0 && grid[x+1][y-2] == undefined){
children.push(generate_node(node,nextPlayer,x,y,x+1,y-2,true));
}
//NE
if(x+2 < config.grid_size && y-2 >= 0 && grid[x+2][y-2] == undefined){
children.push(generate_node(node,nextPlayer,x,y,x+2,y-2,true));
}
//ENE
if(x+2 < config.grid_size && y-1 >= 0 && grid[x+2][y-1] == undefined){
children.push(generate_node(node,nextPlayer,x,y,x+2,y-1,true));
}
//E
if(x+2 < config.grid_size && grid[x+2][y] == undefined){
children.push(generate_node(node,nextPlayer,x,y,x+2,y,true));
}
//ESE
if(x+2 < config.grid_size && y+1 < config.grid_size && grid[x+2][y+1] == undefined){
children.push(generate_node(node,nextPlayer,x,y,x+2,y+1,true));
}
//SE
if(x+2 < config.grid_size && y+2 < config.grid_size && grid[x+2][y+2] == undefined){
children.push(generate_node(node,nextPlayer,x,y,x+2,y+2,true));
}
//SSE
if(x+1 < config.grid_size && y+2 < config.grid_size && grid[x+1][y+2] == undefined){
children.push(generate_node(node,nextPlayer,x,y,x+1,y+2,true));
}
//S
if(y+2 < config.grid_size && grid[x][y+2] == undefined){
children.push(generate_node(node,nextPlayer,x,y,x,y+2,true));
}
//SSW
if(x-1 >= 0 && y+2 < config.grid_size && grid[x-1][y+2] == undefined){
children.push(generate_node(node,nextPlayer,x,y,x-1,y+2,true));
}
//SW
if(x-2 >= 0 && y+2 < config.grid_size && grid[x-2][y+2] == undefined){
children.push(generate_node(node,nextPlayer,x,y,x-2,y+2,true));
}
//WSW
if(x-2 >= 0 && y+1 < config.grid_size && grid[x-2][y+1] == undefined){
children.push(generate_node(node,nextPlayer,x,y,x-2,y+1,true));
}
//W
if(x-2 >= 0 && grid[x-2][y] == undefined){
children.push(generate_node(node,nextPlayer,x,y,x-2,y,true));
}
//WNW
if(x-2 >= 0 && y-1 >= 0 && grid[x-2][y-1] == undefined){
children.push(generate_node(node,nextPlayer,x,y,x-2,y-1,true));
}
//NW
if(x-2 >= 0 && y-2 >= 0 && grid[x-2][y-2] == undefined){
children.push(generate_node(node,nextPlayer,x,y,x-2,y-2,true));
}
//NNW
if(x-2 >= 0 && y-1 >= 0 && grid[x-2][y-1] == undefined){
children.push(generate_node(node,nextPlayer,x,y,x-2,y-1,true));
}
}
}
}
//Check Children for duplicates
var temp = new Array();
for(var i=0;i<children.length;i++){
if(!contains(temp, children[i])){
temp.length+=1;
temp[temp.length-1]=children[i];
}
}
var message = new Object();
message.type = 'debug';
message.children = children.length;
postMessage(message);
return children;
}
//check if array a contains element e
function contains(a, e) {
for(var j=0;j<a.length;j++)
if(same_state(a[j].grid,e.grid))
return true;
return false;
}
//check if the two grids have the exact same state.
function same_state(grid1,grid2){
for(var i=0;i<config.grid_size;i++){
for(var j=0;j<config.grid_size;j++){
if(grid1[i][j] != grid2[i][j])
return false;
}
}
return true;
}
//infect adjacent blocks - check if they are owned by opponent, if they are, infect.
function claim_adjacent_blocks(grid,x,y,player){
//N
if(y-1 >= 0 && grid[x][y-1] != undefined){
if(grid[x][y-1].player != player)
grid[x][y-1] = new Virus(player);
}
//E
if(x+1 < config.grid_size && grid[x+1][y] != undefined){
if(grid[x+1][y].player != player)
grid[x+1][y] = new Virus(player);
}
//S
if(y+1 < config.grid_size && grid[x][y+1] != undefined){
if(grid[x][y+1].player != player)
grid[x][y+1] = new Virus(player);
}
//W
if(x-1 >= 0 && grid[x-1][y] != undefined){
if(grid[x-1][y].player != player)
grid[x-1][y] = new Virus(player);
}
//NE
if(x+1 < config.grid_size && y-1 >= 0 && grid[x+1][y-1] != undefined){
if(grid[x+1][y-1].player != player)
grid[x+1][y-1] = new Virus(player);
}
//SE
if(x+1 < config.grid_size && y+1 < config.grid_size && grid[x+1][y+1] != undefined){
if(grid[x+1][y+1].player != player)
grid[x+1][y+1] = new Virus(player);
}
//SW
if(x-1 >= 0 && y+1 < config.grid_size && grid[x-1][y+1] != undefined){
if(grid[x-1][y+1].player != player)
grid[x-1][y+1] = new Virus(player);
}
//NW
if(x-1 >= 0 && y-1 >= 0 && grid[x-1][y-1] != undefined){
if(grid[x-1][y-1].player != player)
grid[x-1][y-1] = new Virus(player);
}
}
//initialize the decision tree, call max_value() on each child, and return the best state that has the best move.
function alpha_beta_search(){
var node = new Node(null,grid,config.player,new Array());
var bestMove = undefined;
var bestAlpha = -99999;
node.children = generate_children(node);
//var outBest = "";
for(var i = 0;i<node.children.length;i++){
var current_value = max_value(node.children[i], config.depth, -99999, 99999);
if(current_value > bestAlpha){
bestAlpha = current_value;
bestMove = node.children[i].grid;
}
//debugging
//outBest+="(:"+current_value+")";
}
//postMessage(outBest);
return bestMove;
}
function max_value(node, depth, alpha, beta){
node.children = generate_children(node);
if(node.children.length == 0 || depth == 0)
return heuristic_estimate(node);
for(var i = 0;i<node.children.length;i++){
alpha = Math.max(alpha,min_value(node.children[i],depth-1,alpha,beta));
if(alpha >= beta){
return alpha;
}
}
delete node.children;
return alpha;
}
function min_value(node, depth, alpha, beta){
node.children = generate_children(node);
if(node.children.length == 0 || depth == 0)
return heuristic_estimate(node);
for(var i = 0;i<node.children.length;i++){
beta = Math.min(beta,max_value(node.children[i],depth-1,alpha,beta));
if(beta <= alpha){
return beta;
}
}
delete node.children;
return beta;
}
//heuristic_estimate interface, calls the heuristic function appropriate for the playertype
function heuristic_estimate(node){
switch(config.playertype){
case "H1":
return heuristic_estimate_h1(node);
case "H2":
return heuristic_estimate_h2(node);
}
}
//Heuristic 1 - count the number of squares owned, minus number of squares owned by opponent.
function heuristic_estimate_h1(node){
return Math.random();
}
//Heuristic 2 - count the number of squares owned, minus number of squares owned by opponent.
//Give each count the weight of the amount of life left instead of just 1.
function heuristic_estimate_h2(node){
var me = 0;
var opp = 0
for(var i=0;i<config.grid_size;i++){
for(var j=0;j<config.grid_size;j++){
if(node.grid[i][j] != undefined){
if(node.grid[i][j].player == config.player)
me = me+node.grid[i][j].life;
else
opp+node.grid[i][j].life;
}
}
}
return me - opp;
}
//find a move, and send the new state to the browser
function move(){
//find a move
var grid = alpha_beta_search();
//send the new state to browser
var message = new Object();
message.type = 'move';
message.grid = grid;
message.player = config.player;
postMessage(message);
}