-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
485 lines (423 loc) · 15.3 KB
/
app.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
const X_SYMBOL = 'x';
const O_SYMBOL = 'o';
// pubsub module ======================================================================
const pubsub = (() => {
const events = {};
let subscriptionsId = -1;
function publish(event, data) {
if (!events[event]) {
return false;
}
events[event].forEach((subscription) => {
subscription.func(data);
});
return true;
}
function subscribe(event, func) {
if (!events[event]) {
events[event] = [];
}
subscriptionsId += 1;
const token = subscriptionsId.toString();
events[event].push({
token,
func,
});
return token;
}
function unsubscribe(token) {
const found = Object.keys(events).some((event) =>
events[event].some((subscription, index) => {
const areEqual = subscription.token === token.toString();
if (areEqual) {
events[event].splice(index, 1);
}
return areEqual;
})
);
return found ? token : null;
}
return {
publish,
subscribe,
unsubscribe,
};
})();
// game logic ======================================================================
let gameEngin = (function () {
let player01;
let player02;
let players;
let currentPlayer;
let gameMode;
let aiDifficulty;
let gameLocked = false;
const WINNING_COMBINATIONS = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8],
[0, 4, 8],
[2, 4, 6],
];
// players factory function
const createPlayer = (name, symbol, type) => ({ name, symbol, type });
// setup new game settings when form submitted
pubsub.subscribe('newSettings', setupNewGame);
function setupNewGame(formData) {
player01 = createPlayer(
formData.pl01Name,
formData.pl01Symbol,
formData.pl01Type
);
player02 = createPlayer(
formData.pl02Name,
formData.pl02Symbol,
formData.pl02Type
);
players = [player01, player02];
currentPlayer = players.find((player) => player.symbol === X_SYMBOL);
gameMode = formData.mode;
aiDifficulty = formData.difficultyChoice;
}
// play ai when ai is the first to go
pubsub.subscribe('stateReset', playAiOnReset);
function playAiOnReset() {
currentPlayer = players.find((player) => player.symbol === X_SYMBOL);
if (currentPlayer.type === 'COMPUTER') playAiMove(new Array(9), player02);
}
// check the new published state for an end if not give the turn to the other player
pubsub.subscribe('stateUpdated', checkForEnd);
function checkForEnd([state, index]) {
if (aWinExists(state, currentPlayer)) {
const winCombination = getWinCombination(state, index);
pubsub.publish('gameEnded', [currentPlayer, winCombination]);
} else if (isDrawEnd(state)) {
pubsub.publish('gameEnded', ['draw']);
} else {
alternateTurn();
if (currentPlayer === player02 && gameMode === 'PvE')
playAiMove(state, player02);
}
}
function alternateTurn() {
currentPlayer = players.find((player) => player !== currentPlayer);
}
function getCurrentPlayer() {
return currentPlayer;
}
function aWinExists(state, player) {
return WINNING_COMBINATIONS.some((combination) =>
combination.every((i) => state[i] === player.symbol)
);
}
function getWinCombination(state, index) {
const possibleWins = WINNING_COMBINATIONS.filter((combination) =>
combination.includes(Number(index))
);
return possibleWins.find((combination) =>
combination.every((i) => state[i] === currentPlayer.symbol)
);
}
function isDrawEnd(state) {
return !state.includes(undefined);
}
// minimax algorithm
function minimax(state, player, depth, maxDepth) {
let bestMove;
let bestScore;
if (aWinExists(state, player02)) {
return 10 - depth;
}
if (aWinExists(state, player01)) {
return depth - 10;
}
if (isDrawEnd(state) || depth === maxDepth) {
return 0;
}
if (player === player02) {
bestScore = -Infinity;
for (let i = 0; i < state.length; i++) {
if (state[i] === undefined) {
state[i] = player.symbol;
let score = minimax(state, player01, depth + 1, maxDepth);
if (score > bestScore) {
bestScore = score;
bestMove = i;
}
state[i] = undefined;
}
}
} else {
bestScore = Infinity;
for (let i = 0; i < state.length; i++) {
if (state[i] === undefined) {
state[i] = player.symbol;
let score = minimax(state, player02, depth + 1, maxDepth);
if (score < bestScore) {
bestScore = score;
bestMove = i;
}
state[i] = undefined;
}
}
}
if (depth === 0) {
return bestMove;
}
return bestScore + (player === player02 ? depth : -depth);
}
function playAiMove(state, player) {
gameLocked = true;
const maxDepth = setMaxDepth();
const aiMove = minimax(state, player, 0, maxDepth);
setTimeout(publishMove, 700);
function setMaxDepth() {
const easyDepthRange = [1, 2, 3, 4, 5];
const mediumDepthRange = [3, 4, 5, 6];
const hardDepthRange = [6, 7, 8, 9]; // hard but sometimes does a wrong move
if (aiDifficulty === 'easy') return randomDepth(easyDepthRange);
if (aiDifficulty === 'medium') return randomDepth(mediumDepthRange);
if (aiDifficulty === 'hard') return randomDepth(hardDepthRange);
function randomDepth(depthRange) {
return depthRange[Math.floor(Math.random() * depthRange.length)];
}
}
function publishMove() {
pubsub.publish('aiMove', aiMove);
gameLocked = false;
}
}
function waitForAi() {
return gameLocked;
}
return { getCurrentPlayer, waitForAi };
})();
// game board module ======================================================================
const GameBoard = (function () {
let state = new Array(9);
// update the board state from cell click or ai move
pubsub.subscribe('cellClicked', updateBoardState);
pubsub.subscribe('aiMove', updateBoardState);
function updateBoardState(index) {
const currentPlayer = gameEngin.getCurrentPlayer();
if (!state[index]) {
state[index] = currentPlayer.symbol;
pubsub.publish('stateUpdated', [state, index]);
}
}
// rest the state when reset is clicked
pubsub.subscribe('reset', resetState);
function resetState() {
state = new Array(9);
pubsub.publish('stateReset');
}
})();
// display controller ======================================================================
const displayController = (function () {
const board = document.getElementById('board');
const settingsForm = document.getElementById('settings-form');
const pl01InfoHeader = document.getElementById('pl01-info-header');
const pl01InfoName = document.getElementById('pl01-info-name');
const pl02InfoHeader = document.getElementById('pl02-info-header');
const pl02InfoName = document.getElementById('pl02-info-name');
const pl01InfoSymbol = document.getElementById('pl01-info-symbol');
const pl02InfoSymbol = document.getElementById('pl02-info-symbol');
const pl01SymbolChoice = document.getElementById('pl01-symbol-choice');
const pl02SymbolChoice = document.getElementById('pl02-symbol-choice');
const flipSymbolsBtn = document.getElementById('flip-symbols-btn');
const message = document.getElementById('message');
const boardCells = document.querySelectorAll('.board :not(#message)');
const gameModeBtns = document.querySelectorAll('input[name="game-mode"]');
const gameLevel = document.getElementById('game-level');
const player02Div = document.getElementById('player-two');
const settingsModal = document.getElementById('settings-modal');
const resetBtn = document.getElementById('reset-btn');
const settingsBtn = document.getElementById('settings-btn');
const cancelSettings = document.getElementById('cancel-settings');
// display updated cell symbol with passed state
pubsub.subscribe('stateUpdated', updateCells);
function updateCells([state, index]) {
const currentPlayer = gameEngin.getCurrentPlayer();
const cell = document.querySelector(`[data-index="${index}"]`);
cell.classList.add(state[index]);
lightCurrentSymbol(currentPlayer);
if (currentPlayer.type !== 'COMPUTER') {
setBoardHoverClass(currentPlayer);
}
}
// publish the index of the clicked cell
board.addEventListener('click', publishCellEvent);
function publishCellEvent(event) {
if (
gameEngin.getCurrentPlayer().type === 'COMPUTER' ||
!event.target.classList.contains('cell')
)
return;
const cellIndex = event.target.dataset.index;
pubsub.publish('cellClicked', cellIndex);
}
function setBoardHoverClass(player) {
if (!player) return;
board.classList.remove('x');
board.classList.remove('o');
board.classList.add(player.symbol);
}
// highlight the symbol of the current player
function lightCurrentSymbol(player) {
const playersSymbols = document.querySelectorAll('.player-symbol');
playersSymbols.forEach((symbol) => {
if (symbol.classList.contains(player.symbol)) {
symbol.classList.add('light');
} else {
symbol.classList.remove('light');
}
});
}
// toggle PvE and PvP on setting form
gameModeBtns.forEach((btn) => {
btn.addEventListener('change', (event) => {
const mode = event.target.value;
if (mode === 'PvP') {
gameLevel.style.display = 'none';
player02Div.style.display = 'block';
document
.getElementById('player02Name')
.setAttribute('required', true);
} else if (mode === 'PvE') {
gameLevel.style.display = 'block';
player02Div.style.display = 'none';
document.getElementById('player02Name').removeAttribute('required');
}
});
});
// flip the symbols on settings form
flipSymbolsBtn.addEventListener('click', flipSymbols);
function flipSymbols(e) {
e.preventDefault();
[pl01SymbolChoice, pl02SymbolChoice].forEach((symbol) => {
symbol.classList.toggle(X_SYMBOL);
symbol.classList.toggle(O_SYMBOL);
});
}
// get the submitted settings
function getGameSettings() {
const gameMode = document.querySelector(
'input[name="game-mode"]:checked'
).value;
let formData = {};
function getSymbol(elm) {
if (elm.classList.contains(X_SYMBOL)) return X_SYMBOL;
if (elm.classList.contains(O_SYMBOL)) return O_SYMBOL;
}
if (gameMode === 'PvP') {
formData = {
mode: gameMode,
pl01Name: settingsForm.elements.player01Name.value,
pl02Name: settingsForm.elements.player02Name.value,
pl01Symbol: getSymbol(pl01SymbolChoice),
pl02Symbol: getSymbol(pl02SymbolChoice),
pl01Type: 'PLAYER-01',
pl02Type: 'PLAYER-02',
};
} else if (gameMode === 'PvE') {
const difficultyChoice = document.querySelector(
'input[name="game-level"]:checked'
).value;
formData = {
mode: gameMode,
pl01Name: settingsForm.elements.player01Name.value,
pl02Name: `${difficultyChoice.toUpperCase()} AI`,
pl01Symbol: getSymbol(pl01SymbolChoice),
pl02Symbol: getSymbol(pl02SymbolChoice),
pl01Type: 'PLAYER',
pl02Type: 'COMPUTER',
difficultyChoice,
};
}
return formData;
}
// display players info on the game and publish the new settings
settingsForm.addEventListener('submit', (e) => {
e.preventDefault();
let formData = getGameSettings();
[pl01InfoSymbol, pl02InfoSymbol].forEach((symbol) => {
symbol.classList.remove(X_SYMBOL);
symbol.classList.remove(O_SYMBOL);
});
pl01InfoName.innerText = formData.pl01Name.toUpperCase();
pl02InfoName.innerText = formData.pl02Name.toUpperCase();
pl01InfoHeader.innerText = formData.pl01Type;
pl02InfoHeader.innerText = formData.pl02Type;
pl01InfoSymbol.classList.add(formData.pl01Symbol);
pl02InfoSymbol.classList.add(formData.pl02Symbol);
pubsub.publish('newSettings', formData);
resetBoard();
let currentPlayer = gameEngin.getCurrentPlayer();
setBoardHoverClass(currentPlayer);
lightCurrentSymbol(currentPlayer);
settingsModal.close();
});
// visual effects when game reaches an end
pubsub.subscribe('gameEnded', endGame);
function endGame([winner, winCombination]) {
if (winner !== 'draw') {
message.innerText = `${winner.name.toUpperCase()} Won!`;
winCombination.forEach((index) => {
const cell = document.querySelector(`[data-index="${index}"]`);
if (winner.symbol === X_SYMBOL) {
cell.style.backgroundColor = 'var(--dark-red)';
message.style.backgroundColor = 'var(--red)';
} else {
cell.style.backgroundColor = 'var(--dark-blue)';
message.style.backgroundColor = 'var(--blue)';
}
});
board.removeEventListener('click', publishCellEvent);
} else {
message.style.backgroundColor = 'var(--theme-medium)';
message.innerText = 'Tie Game';
}
message.style.display = 'block';
boardCells.forEach((cell) => {
cell.classList.add('dimmed');
});
}
// reset the display and publish the reset event for other modules
resetBtn.addEventListener('click', resetBoard);
function resetBoard() {
if (gameEngin.waitForAi()) return;
message.style.display = 'none';
boardCells.forEach((cell) => {
cell.style.backgroundColor = 'var(--theme-dark)';
cell.classList.remove('dimmed');
if (cell.classList.contains(O_SYMBOL)) cell.classList.toggle(O_SYMBOL);
if (cell.classList.contains(X_SYMBOL)) cell.classList.toggle(X_SYMBOL);
});
board.addEventListener('click', publishCellEvent);
pubsub.publish('reset');
const currentPlayer = gameEngin.getCurrentPlayer();
lightCurrentSymbol(currentPlayer);
if (currentPlayer.type !== 'COMPUTER') {
setBoardHoverClass(currentPlayer);
}
}
// open and close settings
settingsBtn.addEventListener('click', showSettings);
function showSettings() {
if (gameEngin.waitForAi()) return;
cancelSettings.style.display = 'block';
settingsModal.showModal();
}
cancelSettings.addEventListener('click', (e) => {
e.preventDefault();
settingsModal.close();
});
// open modal at page load and prevent the user from closing the setting modal with ESC
settingsModal.showModal();
settingsModal.addEventListener('cancel', (event) => {
event.preventDefault();
});
})();