-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
536 lines (465 loc) · 14.8 KB
/
script.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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
document.getElementById('add-col').addEventListener('click', addCol);
document.getElementById('remove-col').addEventListener('click', removeCol);
document.getElementById('add-row').addEventListener('click', addRow);
document.getElementById('remove-row').addEventListener('click', removeRow);
document.getElementById('reset').addEventListener('click', drawPlayfield);
document
.getElementById('difficulty')
.addEventListener('change', changeDifficulty);
let playfield = document.getElementById('playfield');
// Game settings
const BORDER_THICKNESS = 10;
let colCount = 3;
let rowCount = 3;
let difficulty = 3;
function addCol() {
if (colCount === 6) {
return;
}
colCount += 1;
drawPlayfield();
}
function removeCol() {
if (colCount === 2) {
return;
}
colCount -= 1;
drawPlayfield();
}
function addRow() {
if (rowCount === 6) {
return;
}
rowCount += 1;
drawPlayfield();
}
function removeRow() {
if (rowCount === 2) {
return;
}
rowCount -= 1;
drawPlayfield();
}
function drawPlayfield() {
var element = document.getElementById('playfield');
if (element != null) {
element.parentNode.removeChild(element);
}
var container = document.getElementById('playfieldContainer');
playfield = document.createElement('table');
playfield.id = 'playfield';
for (let i = 0; i < rowCount; i++) {
let row = document.createElement('tr');
row.id = i;
for (let j = 0; j < colCount; j++) {
const indices = Object.values(getBorderIndices(i, j));
const col = document.createElement('td');
for (index in indices) {
col.classList.add(indices[index]);
}
col.id = j;
col.addEventListener('click', tileClicked);
row.appendChild(col);
}
playfield.appendChild(row);
}
container.appendChild(playfield);
game = new Game(rowCount, colCount);
}
function changeDifficulty(event) {
difficulty = event.currentTarget.value;
}
function tileClicked(event) {
if (game.getPossibleMoves().length === 0 || game.currentPlayer === 1) return;
game.tileClicked(event.currentTarget);
if (game.currentPlayer === 1) {
playAgent();
}
}
function playAgent() {
if (game.getPossibleMoves().length === 0) return;
setTimeout(() => {
const moves = game.minimaxRoot(difficulty);
const move = moves[Math.floor(Math.random()*moves.length)]
game.playMove(move, true);
if (game.currentPlayer === 1) {
playAgent();
}
}, 500);
}
function getBorderIndices(row, col) {
let indices = {};
indices.top = +((colCount * 2 + 1) * row + col);
indices.left = indices.top + colCount;
indices.right = indices.left + 1;
indices.bottom = indices.right + colCount;
return indices;
}
class Game {
rows = 0;
cols = 0;
indices = [];
board = [];
excludeTop = [];
excludeBottom = [];
excludeLeft = [];
excludeRight = [];
currentPlayer = -1;
currentPlayerColor = 'blue';
playerScores = {
'1': 0,
'-1': 0
};
constructor(rows, cols) {
(this.rows = rows),
(this.cols = cols),
(this.board = this.initBoard()),
this.createIndices()
}
initBoard() {
const length = (this.rows * 2 + 1) * this.cols + this.rows;
return Array.from({ length: length }, () => {
return 0;
});
}
createIndices() {
for (let i = 0; i < this.board.length; i++) {
this.indices.push(i);
}
}
getPossibleMoves() {
const possibleMoves = [];
for (let i = 0; i < this.board.length; i++) {
if (this.board[i] === 0) {
possibleMoves.push(i);
}
}
return possibleMoves;
}
changePlayer() {
this.currentPlayer *= -1;
this.currentPlayer === 1
? (this.currentPlayerColor = 'red')
: (this.currentPlayerColor = 'blue');
}
isMoveValid(move) {
if (this.getPossibleMoves().includes(move)) {
return true;
} else {
return false;
}
}
isGameOver() {
if (this.getPossibleMoves().length === 0) {
if (this.playerScores[1] === this.playerScores[-1]) {
return 0;
}
return this.playerScores[1] > this.playerScores[-1] ? 1 : -1;
} else {
return false;
}
}
evaluateBoardState() {
let score = this.playerScores[1] - this.playerScores[-1];
if (score < 0) {
score = score ** 2 * -1;
} else {
score = score ** 2;
}
return score;
}
tileClicked(elem) {
const col = +elem.id;
const row = +elem.parentNode.id;
const borderIndices = getBorderIndices(row, col);
let border_top = BORDER_THICKNESS;
let border_right = BORDER_THICKNESS;
let border_bottom = BORDER_THICKNESS;
let border_left = BORDER_THICKNESS;
if (row == 0) {
border_top = 20;
} else if (row == rowCount) {
border_bottom = 20;
}
if (col == 0) {
border_left = 20;
} else if (col == colCount) {
border_right = 20;
}
const x = event.pageX;
const y = event.pageY;
const top = event.currentTarget.getBoundingClientRect().top;
const right = event.currentTarget.getBoundingClientRect().right;
const bottom = event.currentTarget.getBoundingClientRect().bottom - 10;
const left = event.currentTarget.getBoundingClientRect().left + 10;
let tileTaken = false;
let borderTaken = false;
if (x < left + border_left) {
console.log('left border is clicked');
// Check if border is already taken.
if (event.currentTarget.classList.value.indexOf('left') !== -1) {
return;
}
borderTaken = true;
event.currentTarget.classList.add(`${this.currentPlayerColor}-left`);
this.board[borderIndices.left] = this.currentPlayer;
if (col > 0) {
const sibling = playfield.children[row].children[col - 1];
sibling.classList.add(`${this.currentPlayerColor}-right`);
if (sibling.classList.length === 8) {
sibling.classList.add(`${this.currentPlayerColor}`);
this.currentPlayer === 1
? (this.playerScores[1] += 1)
: (this.playerScores[-1] += 1);
tileTaken = true;
}
}
} else if (x > right - border_right) {
console.log('right border is clicked');
// Check if border is already taken.
if (event.currentTarget.classList.value.indexOf('right') !== -1) {
return;
}
borderTaken = true;
event.currentTarget.classList.add(`${this.currentPlayerColor}-right`);
this.board[borderIndices.right] = this.currentPlayer;
if (col < this.cols - 1) {
const sibling = event.currentTarget.nextSibling;
sibling.classList.add(`${this.currentPlayerColor}-left`);
if (sibling.classList.length === 8) {
sibling.classList.add(`${this.currentPlayerColor}`);
this.currentPlayer === 1
? (this.playerScores[1] += 1)
: (this.playerScores[-1] += 1);
tileTaken = true;
}
}
} else if (y < top + border_top) {
console.log('top border is clicked');
// Check if border is already taken.
if (event.currentTarget.classList.value.indexOf('top') !== -1) {
return;
}
borderTaken = true;
event.currentTarget.classList.add(`${this.currentPlayerColor}-top`);
this.board[borderIndices.top] = this.currentPlayer;
if (row > 0) {
const sibling = playfield.children[row - 1].children[col];
sibling.classList.add(`${this.currentPlayerColor}-bottom`);
if (sibling.classList.length === 8) {
sibling.classList.add(`${this.currentPlayerColor}`);
this.currentPlayer === 1
? (this.playerScores[1] += 1)
: (this.playerScores[-1] += 1);
tileTaken = true;
}
}
} else if (y > bottom - border_bottom) {
console.log('bottom border is clicked');
// Check if border is already taken.
if (event.currentTarget.classList.value.indexOf('bottom') !== -1) {
return;
}
borderTaken = true;
event.currentTarget.classList.add(`${this.currentPlayerColor}-bottom`);
this.board[borderIndices.bottom] = this.currentPlayer;
if (row < this.rows - 1) {
const sibling =
event.currentTarget.parentNode.nextSibling.childNodes[col];
sibling.classList.add(`${this.currentPlayerColor}-top`);
if (sibling.classList.length === 8) {
sibling.classList.add(`${this.currentPlayerColor}`);
this.currentPlayer === 1
? (this.playerScores[1] += 1)
: (this.playerScores[-1] += 1);
tileTaken = true;
}
}
}
const totalBordersTaken = event.currentTarget.classList.length;
if (totalBordersTaken === 8) {
event.currentTarget.classList.add(`${this.currentPlayerColor}`);
this.currentPlayer === 1
? (this.playerScores[1] += 1)
: (this.playerScores[-1] += 1);
tileTaken = true;
return;
} else if (tileTaken === true) {
return;
}
if (borderTaken) {
this.changePlayer();
}
}
colorBorders(move) {
let tiles = document.getElementsByClassName(move);
for (let tile of tiles) {
for (let i = 0; i < 4; i++) {
if (tile.classList.item(i) == move) {
switch (i) {
case 0:
tile.classList.add(`${this.currentPlayerColor}-top`);
if (tile.classList.length === 8) {
tile.classList.add(`${this.currentPlayerColor}`);
}
break;
case 1:
tile.classList.add(`${this.currentPlayerColor}-left`);
if (tile.classList.length === 8) {
tile.classList.add(`${this.currentPlayerColor}`);
}
break;
case 2:
tile.classList.add(`${this.currentPlayerColor}-right`);
if (tile.classList.length === 8) {
tile.classList.add(`${this.currentPlayerColor}`);
}
break;
case 3:
tile.classList.add(`${this.currentPlayerColor}-bottom`);
if (tile.classList.length === 8) {
tile.classList.add(`${this.currentPlayerColor}`);
}
break;
default:
break;
}
}
}
}
}
playMove(move, color) {
if (color) this.colorBorders(move);
this.board[move] = this.currentPlayer;
let tiles = document.getElementsByClassName(move);
let tileCaptured = false;
for (let tile of tiles) {
let availableBorders = 4
for (let i = 0; i < 4; i++) {
const index = tile.classList.item(i);
if (this.board[index] !== 0) {
availableBorders -= 1;
}
}
if (availableBorders === 0) {
this.playerScores[this.currentPlayer] += 1;
tileCaptured = true;
}
}
if (!tileCaptured) this.changePlayer();
}
minimax(depth, alpha, beta, player) {
// If end of tree is reached, or no more moves available, return the board evaluation
if (depth === 0 || this.getPossibleMoves().length === 0)
return this.evaluateBoardState();
const moves = this.getPossibleMoves();
// AI plays (maximizing player 1)
if (player == 1) {
let highestValue = -999;
for (let i = 0; i < moves.length; i++) {
// Save the current game state
const playerScores = JSON.parse(JSON.stringify(this.playerScores));
const currentPlayer = JSON.parse(JSON.stringify(this.currentPlayer));
const currentPlayerColor = JSON.parse(
JSON.stringify(this.currentPlayerColor)
);
const board = JSON.parse(JSON.stringify(this.board));
this.playMove(moves[i], false);
let value = 0;
// Check who's turn it is after the played move
if (this.currentPlayer === 1) {
value = this.minimax(depth - 1, alpha, beta, 1);
} else {
value = this.minimax(depth - 1, alpha, beta, -1);
}
highestValue = Math.max(highestValue, value);
alpha = Math.max(alpha, value);
// Undo the move
this.playerScores = playerScores;
this.currentPlayer = currentPlayer;
this.currentPlayerColor = currentPlayerColor;
this.board = board;
// Stop searching
if (alpha >= beta) {
break;
}
}
return highestValue;
} else {
// Human plays (minimizing player -1)
let lowestValue = 999;
for (let i = 0; i < moves.length; i++) {
// Save the current game state
const playerScores = JSON.parse(JSON.stringify(this.playerScores));
const currentPlayer = JSON.parse(JSON.stringify(this.currentPlayer));
const currentPlayerColor = JSON.parse(
JSON.stringify(this.currentPlayerColor)
);
const board = JSON.parse(JSON.stringify(this.board));
this.playMove(moves[i], false);
let value = 0;
// Check who's turn it is after the played move
if (this.currentPlayer === 1) {
value = this.minimax(depth - 1, alpha, beta, 1);
} else {
value = this.minimax(depth - 1, alpha, beta, -1);
}
lowestValue = Math.min(lowestValue, value);
beta = Math.min(beta, value);
// Undo the move
this.playerScores = playerScores;
this.currentPlayer = currentPlayer;
this.currentPlayerColor = currentPlayerColor;
this.board = board;
// Stop searching
if (alpha >= beta) {
break;
}
}
return lowestValue;
}
}
minimaxRoot(depth) {
const moves = this.getPossibleMoves();
let bestMove = 0;
this.currentPlayer === 1 ? (bestMove = -999) : (bestMove = 999);
let bestMovesFound = [];
let movesAndValues = []
moves.forEach(move => {
// Save the current game state by copying the variables into placeholders
const playerScores = JSON.parse(JSON.stringify(this.playerScores));
const currentPlayer = JSON.parse(JSON.stringify(this.currentPlayer));
const currentPlayerColor = JSON.parse(
JSON.stringify(this.currentPlayerColor)
);
const board = JSON.parse(JSON.stringify(this.board));
let value = 0;
this.playMove(move, false);
// Check who's turn it is after the played move
if (this.currentPlayer == 1) {
value = this.minimax(depth - 1, -999, 999, 1);
} else {
value = this.minimax(depth - 1, -999, 999, -1);
}
// Undo the move
this.playerScores = playerScores;
this.currentPlayer = currentPlayer;
this.currentPlayerColor = currentPlayerColor;
this.board = board;
movesAndValues.push({move, value})
if (value >= bestMove) {
bestMove = value;
bestMovesFound = [move]
}
});
for (let pair of movesAndValues) {
if (pair.value === bestMove) {
bestMovesFound.push(pair.move)
}
}
return bestMovesFound;
}
}
// Initialize the board
game = new Game(rowCount, colCount);
drawPlayfield();