-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.js
422 lines (353 loc) · 11.7 KB
/
game.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
class Game {
constructor(darkDifficulty, lightDifficulty) {
// Create a 2D array of 64 cells to represent the board
this.board = [];
for (let i = 0; i < 8; i++) {
this.board[i] = [];
for (let j = 0; j < 8; j++) {
this.board[i][j] = new Cell(i, j);
}
}
// Place the four centre disks
this.turn = 1;
this.place(3, 3);
this.place(4, 4);
// Set starting turn to dark
this.turn = -1;
this.place(3, 4);
this.place(4, 3);
this.darkDifficulty = darkDifficulty;
this.lightDifficulty = lightDifficulty;
// Add the initial state to the game history
this.history = [{
board: this.board,
move: null
}];
}
static inBoardBoundary = (x, y) => x >= 0 && x < 8 && y >= 0 && y < 8;
static minimax = (position, move, depth, alpha, beta) => {
// If the maximum depth is reached, or there are no valid moves left,
// return the static evaluation of the current position
if (depth === 0 || position.getValidMoves(position.turn).length === 0) {
return {
evaluation: position.getStaticEvaluation(),
move: move
}
}
let bestEvaluation;
let moves = position.getValidMoves(position.turn);
for (const nextMove of moves) {
let nextPosition = position.copy();
nextPosition.move(nextMove.x, nextMove.y);
// Evaluate the new position
let newEvaluation = Game.minimax(nextPosition, nextMove, depth - 1, alpha, beta);
// Compare the new evaluation to the current best evaluation
if (position.turn === -1) {
if (!bestEvaluation || newEvaluation.evaluation > bestEvaluation.evaluation) {
bestEvaluation = {
evaluation: newEvaluation.evaluation,
move: nextMove
}
}
// Update alpha value
alpha = Math.max(alpha, newEvaluation.evaluation);
} else {
if (!bestEvaluation || newEvaluation.evaluation < bestEvaluation.evaluation) {
bestEvaluation = {
evaluation: newEvaluation.evaluation,
move: nextMove
}
}
// Update beta value
beta = Math.min(beta, newEvaluation.evaluation);
}
// Prune the current branch if an equal or better evaluation has already been found
if (beta <= alpha) {
break;
}
}
return bestEvaluation;
}
copy() {
let newGame = new Game;
// Copy information from game to newGame
for (let i = 0; i < 8; i++) {
for (let j = 0; j < 8; j++) {
newGame.board[i][j].state = this.board[i][j].state;
}
}
newGame.turn = this.turn;
return newGame;
}
place(x, y) {
this.board[x][y].state = this.turn;
}
flip(x, y) {
this.board[x][y].flip();
}
updateHistory(x, y) {
// Add the current board state and most recent move to the game history
this.history.push({
board: this.board,
move: {
x: x,
y: y
},
turn: this.turn
})
}
move(x, y) {
if (this.isValidMove(x, y, this.turn)) {
this.place(x, y);
// For each cell surrounding the target cell
for (let i = x - 1; i <= x + 1; i++) {
for (let j = y - 1; j <= y + 1; j++) {
// If the cell contains a disk of the opposite colour
if (Game.inBoardBoundary(i, j) && this.board[i][j].state === this.turn * -1) {
// Calculate the horizontal and vertical distance to the disk
let dx = i - x;
let dy = j - y;
let newX = x + dx;
let newY = y + dy;
// Traverse the board in the direction of the disk until there is no disk of the
// opposite colour occupying the cell
while (Game.inBoardBoundary(newX, newY) && this.board[newX][newY].state === this.turn * -1) {
newX += dx;
newY += dy;
}
// If the cell contains a disk of the current player's colour, traverse back to the target cell,
// flipping each disk to the current player's side
if (Game.inBoardBoundary(newX, newY) && this.board[newX][newY].state === this.turn) {
while (this.board[newX - dx][newY - dy].state === this.turn * -1) {
this.flip(newX - dx, newY - dy);
newX -= dx;
newY -= dy;
}
}
}
}
}
this.updateHistory(x, y);
this.turn *= -1;
} else {
console.error('Invalid move');
}
}
isValidMove(x, y, side) {
if (this.board[x][y].isEmpty()) {
// For each cell surrounding the target cell
for (let i = x - 1; i <= x + 1; i++) {
for (let j = y - 1; j <= y + 1; j++) {
// If the cell contains a disk of the opposite colour
if (Game.inBoardBoundary(i, j) && this.board[i][j].state === side * -1) {
// Calculate the horizontal and vertical distance to the disk
let dx = i - x;
let dy = j - y;
let newX = x + dx;
let newY = y + dy;
// Traverse the board in the direction of the disk until there is no disk of the
// opposite colour occupying the cell
while (Game.inBoardBoundary(newX, newY) && this.board[newX][newY].state === side * -1) {
newX += dx;
newY += dy;
}
// If the new cell contains a disk of the current player's colour, the move is valid
if (Game.inBoardBoundary(newX, newY) && this.board[newX][newY].state === side) {
return true;
}
}
}
}
}
return false;
}
getValidMoves(side) {
let validMoves = [];
for (let i = 0; i < 8; i++) {
for (let j = 0; j < 8; j++) {
if (this.isValidMove(i, j, side)) {
validMoves.push({
x: i,
y: j
});
}
}
}
return validMoves;
}
isGameOver = () => this.getValidMoves(-1).length === 0 && this.getValidMoves(1).length === 0;
get diskCount() {
let darkCount = 0;
let lightCount = 0;
for (let i = 0; i < 8; i++) {
for (let j = 0; j < 8; j++) {
if (this.board[i][j].state === -1) {
darkCount++;
} else if (this.board[i][j].state === 1) {
lightCount++;
}
}
}
return {
dark: darkCount,
light: lightCount,
total: darkCount + lightCount
}
}
getStaticEvaluation = () => this.diskCount.dark - this.diskCount.light;
userMove(x, y) {
this.move(x, y);
}
computerMove() {
// Set search depth
let difficulty = this.turn === -1 ? this.darkDifficulty : this.lightDifficulty;
// Use the Minimax algorithm to determine coordinates of next move
let move = Game.minimax(this, null, difficulty, -64, 64).move;
// Apply the move
this.move(move.x, move.y);
}
}
class DisplayGame extends Game {
constructor(darkPlayerType, lightPlayerType, darkDifficulty, lightDifficulty, timer, timerLength) {
super(darkDifficulty, lightDifficulty);
if (timer) {
this.timer = true;
this.darkTimer = new Timer('dark', timerLength, this);
this.lightTimer = new Timer('light', timerLength, this);
this.darkTimer.updateDisplay();
this.lightTimer.updateDisplay();
this.darkTimer.start();
}
this.computerDelay = 300;
this.darkPlayer = darkPlayerType;
this.lightPlayer = lightPlayerType;
if (this.darkPlayer === 'user') {
this.showValidMoves();
} else {
setTimeout(this.computerMove.bind(this), this.computerDelay);
}
this.running = true;
}
showValidMoves() {
let moves = this.getValidMoves(this.turn);
for (const move of moves) {
let cell = document.getElementById(`${move.x}-${move.y}`);
let disk = (this.turn === -1 ? darkMove : lightMove).cloneNode(true);
cell.appendChild(disk);
}
}
displayScore() {
document.getElementById('dark-score').innerHTML = this.diskCount.dark;
document.getElementById('light-score').innerHTML = this.diskCount.light;
}
place(x, y) {
super.place(x, y);
let cell = document.getElementById(`${x}-${y}`);
let disk = (this.turn === -1 ? darkDisk : lightDisk).cloneNode(true);
// Apply placing animation to disk
disk.style.animation = 'place 200ms ease-out'
disk.onanimationend = () => {
disk.style.animation = '';
}
cell.appendChild(disk);
}
flip(x, y) {
super.flip(x, y);
let cell = document.getElementById(`${x}-${y}`);
let disk = cell.lastElementChild;
// Apply flipping animation to disk
disk.className.baseVal += this.board[x][y].state === -1 ? ' flip-dark' : ' flip-light';
disk.onanimationend = () => {
disk.className.baseVal = this.board[x][y].state === -1 ? 'disk-dark' : 'disk-light';
}
}
clearValidMoves() {
for (let i = 0; i < 8; i++) {
for (let j = 0; j < 8; j++) {
if (this.board[i][j].isEmpty()) {
const cell = document.getElementById(`${i}-${j}`);
// Avoid removing cell references
if (cell.lastElementChild && cell.lastElementChild.classList.contains('disk')) {
cell.removeChild(cell.lastElementChild);
}
}
}
}
}
addMoveToHistoryTable(cellReference) {
const historyContainer = document.getElementById('history-container');
let historyData = document.createElement('td');
historyData.id = `move-${this.history.length - 1}`;
historyData.innerHTML = cellReference;
historyTable.lastElementChild.appendChild(historyData);
// Create a new table row after every other turn
if (this.turn === -1 && !this.isGameOver()) {
historyTable.appendChild(document.createElement('tr'));
historyTable.lastElementChild.appendChild(document.createElement('td'));
historyTable.lastElementChild.firstElementChild.innerHTML = Math.ceil(this.history.length / 2);
}
historyContainer.scrollTop = historyContainer.scrollHeight;
}
move(x, y) {
if (this.isValidMove(x, y, this.turn)) {
super.move(x, y);
this.addMoveToHistoryTable(String.fromCharCode(y + 97) + (x + 1));
} else {
console.error('Invalid move')
}
}
userMove(x, y) {
if (timer) {
this.turn === -1 ? this.darkTimer.stop() : this.lightTimer.stop();
}
this.clearValidMoves();
super.userMove(x, y);
this.startNextTurn();
}
computerMove() {
if (this.running) {
super.computerMove();
this.startNextTurn();
}
}
startNextTurn() {
this.displayScore();
if (this.running) {
if (!this.isGameOver()) {
if (this.getValidMoves(this.turn).length > 0) {
if (this.turn === -1) {
if (this.darkPlayer === 'user') {
information.innerHTML = `Dark's turn to move`;
this.showValidMoves();
if (this.timer) {
this.darkTimer.start();
}
} else {
information.innerHTML = 'Dark is thinking of a move...';
setTimeout(this.computerMove.bind(this), this.computerDelay);
}
} else {
if (this.lightPlayer === 'user') {
information.innerHTML = `Light's turn to move`;
this.showValidMoves();
if (this.timer) {
this.lightTimer.start();
}
} else {
information.innerHTML = 'Light is thinking of a move...';
setTimeout(this.computerMove.bind(this), this.computerDelay);
}
}
} else {
// If the player has no available moves
this.turn *= -1;
this.updateHistory(null, null);
this.addMoveToHistoryTable('-');
this.startNextTurn();
}
} else {
gameOver();
}
}
}
}