-
Notifications
You must be signed in to change notification settings - Fork 2
/
script.js
336 lines (295 loc) · 10.2 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
// Main Menu DOM
const changeThemeEl = document.querySelector('.changeTheme');
const lightModeIconEl = document.querySelector('#lightTheme');
const darkModeIconEl = document.querySelector('#darkTheme');
const gameMenuEl = document.querySelector('.game-menu');
const pvpModeEl = document.querySelector('#pvp-mode');
const pvcModeEl = document.querySelector('#pvc-mode');
const gameModesEls = [pvpModeEl, pvcModeEl];
// Game DOM
let game;
const gameBoxEl = document.querySelector('.game-box');
const gameBoardEl = document.querySelector('.game-board');
const scoresEl = document.querySelector('#scores');
const playerXBadgeEL = document.querySelector('#player-x');
const playerOBadgeEL = document.querySelector('#player-o');
const btnNewGameEl = document.querySelector('#btnNewGame');
const btnNextRoundEl = document.querySelector('#btnNextRound');
class Game {
constructor(computerPlays = false) {
this.computerPlays = computerPlays;
if (computerPlays) this.humanMark = 'X';
this.scores = [0, 0];
this.prepareGame();
}
/**
* Prepare variables needed for a game, e.g. logical interface of a game board.
*/
prepareGame() {
this.grid = new Array(9).fill('');
this.winningCombinations = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8],
[0, 4, 8],
[2, 4, 6],
];
this.actions = 0;
this.previousPlayer = 'O';
this.currentPlayer = 'X';
this.playable = true;
this.prepareGameDOM();
}
/**
* Prepare graphical interface of the game.
*/
prepareGameDOM() {
this.gameTileElements = [];
// Reset Game board DOM
gameBoardEl.innerHTML = '';
// Create game tiles
for (let i = 1; i <= 9; i++) {
const gameTileEl = document.createElement('div');
const gameTileTextEl = document.createElement('p');
gameTileEl.appendChild(gameTileTextEl);
gameTileEl.classList.add('game-tile');
gameTileEl.id = `box${i}`;
gameBoardEl.appendChild(gameTileEl);
this.gameTileElements.push(gameTileEl);
}
// DOM
scoresEl.textContent = `${this.scores[0]}:${this.scores[1]}`;
playerXBadgeEL.classList.add('active');
playerOBadgeEL.classList.remove('active');
btnNextRoundEl.classList.remove('active-btn');
}
placeMark(tileID, computerMove = false) {
// Check if the game is still playable
if (!this.playable) {
return;
}
// Check if it's the computer's turn and the move is not forced
if (this.computerPlays && this.currentPlayer !== this.humanMark && computerMove === false) {
return;
}
const tile = document.getElementById(`box${tileID}`);
// Check if the selected tile is already marked
if (tile.firstChild.textContent !== '') {
return;
}
// Place the mark on the logical and graphical interface of the game board
this.grid[tileID - 1] = this.currentPlayer;
this.actions++;
tile.firstChild.textContent = this.currentPlayer;
playerXBadgeEL.classList.toggle('active');
playerOBadgeEL.classList.toggle('active');
[this.currentPlayer, this.previousPlayer] = [this.previousPlayer, this.currentPlayer];
// Resolve the game state after the move
this.resolveGame();
if (this.computerPlays && this.currentPlayer === 'O') {
this.findCombinations(this.previousPlayer);
}
}
/**
* Find combinations of the enemy and available combinations to decide the next move.
* @param {string} enemy - The symbol of the enemy player ('X' or 'O').
* @returns {number} - The tile ID to place the next mark.
*/
findCombinations(enemy) {
const dangerCombinations = [];
const availableCombinations = [];
// Iterate over all winning combinations
for (const combination of this.winningCombinations) {
let enemyCounter = 0;
// Check if the combination contains enemy marks, decide if the combination is a "danger"
for (const i of combination) {
if (this.grid[i] === enemy) {
enemyCounter++;
if (enemyCounter === 2) {
dangerCombinations.push(combination);
break;
}
}
}
// If no enemy marks are found in the combination, add it to available combinations
if (enemyCounter === 0) {
availableCombinations.push(combination);
}
}
return this.determineNextMove(dangerCombinations, availableCombinations);
}
/**
* Decide the next move based on danger combinations and available combinations.
* @param {Array} dangerCombinations - Combinations where the enemy has two marks.
* @param {Array} availableCombinations - Combinations where no enemy marks are present.
* @returns {number} - The tile ID to place the next mark.
*/
determineNextMove(dangerCombinations, availableCombinations) {
const toBlock = [];
const toWin = {};
// Decide which tiles are a great choice to block enemy from winning
for (const combination of dangerCombinations) {
for (const move of combination) {
if (this.grid[move] === '' && !toBlock.includes(move)) {
toBlock.push(move);
}
}
}
// If no danger combinations to block, search for moves which can bring a win
if (toBlock.length === 0) {
for (const combination of availableCombinations) {
for (const move of combination) {
if (this.grid[move] === '') {
toWin[move] = (toWin[move] || 0) + 1;
}
}
}
let bestToWinValue = 0;
let bestToWinTile = null;
// Find the best tile to choose based on the highest occurrence in available combinations
if (Object.keys(toWin).length > 0) {
for (const [key, value] of Object.entries(toWin)) {
if (value > bestToWinValue) {
bestToWinValue = value;
bestToWinTile = key;
}
}
} else {
// If no available tiles, choose the first empty tile in the grid
for (let i = 0; i < this.grid.length; i++) {
if (this.grid[i] === '') {
bestToWinTile = i;
break;
}
}
}
// If no best tile found, select the first available tile
if (bestToWinTile === null) {
bestToWinTile = Object.keys(toWin)[0];
}
return this.simulatePlaceMark(Number(bestToWinTile) + 1);
} else {
// If danger combinations exist, but it is possible to win in one move, just do it
for (const combination of availableCombinations) {
const emptyTiles = combination.filter((i) => this.grid[i] === '');
if (emptyTiles.length === 1) {
return this.simulatePlaceMark(Number(emptyTiles[0]) + 1);
}
}
return this.simulatePlaceMark(Number(toBlock[0]) + 1);
}
}
/**
* Simulate placing a mark on the game board after a certain delay (1000ms).
* @param {number} tileID - The ID of the tile to place the mark.
*/
simulatePlaceMark(tileID) {
setTimeout(() => {
this.placeMark(tileID, true);
}, 1000);
}
/**
* Resolve the game state and check for a winner or a tie.
*/
resolveGame() {
// Check if the maximum number of actions has been reached
if (this.actions === 9) {
this.playable = false;
btnNextRoundEl.classList.add('active-btn');
}
// Check if there are enough actions for a potential win
if (this.actions >= 5) {
for (const combination of this.winningCombinations) {
// Check if the current combination is a winning combination
if (this.checkWinner(combination)) {
this.playable = false;
// Highlight the winning tiles on the game board
combination.forEach((i) => {
this.gameTileElements[i].classList.add('winner');
});
this.grid[combination[0]] === 'X' ? this.scores[0]++ : this.scores[1]++;
scoresEl.textContent = `${this.scores[0]}:${this.scores[1]}`;
btnNextRoundEl.classList.add('active-btn');
}
}
}
}
/**
* Check if a combination of tiles results in a win.
* @param {Array} combination - The combination of tile indices to check.
* @returns {boolean} - True if the combination is a winning combination, false otherwise.
*/
checkWinner(combination) {
const [x, y, z] = combination;
return this.grid[x] !== '' && this.grid[x] === this.grid[y] && this.grid[y] === this.grid[z];
}
/**
* Only for DEV purposes! Print all data from App instance.
*/
_printData() {
console.clear();
Object.entries(this).forEach(([key, value]) => {
console.log(`${key}: ${value}`);
});
}
}
document.addEventListener('DOMContentLoaded', () => {
document.body.classList.remove('hidden');
});
changeThemeEl.addEventListener('click', () => {
lightModeIconEl.classList.toggle('hidden');
darkModeIconEl.classList.toggle('hidden');
const toggleDarkMode = !lightModeIconEl.classList.contains('hidden');
if (toggleDarkMode) {
console.log('Dark Theme');
} else {
console.log('Light Theme');
}
document.body.classList.toggle('body-dark', toggleDarkMode);
changeThemeEl.classList.toggle('dark', toggleDarkMode);
document.querySelectorAll('.player-badge').forEach((badge) => {
badge.classList.toggle('dark', toggleDarkMode);
});
document.querySelector('h5').firstChild.classList.toggle('author-dark', toggleDarkMode);
document.querySelectorAll('.btn').forEach((btn) => {
btn.classList.toggle('dark', toggleDarkMode);
});
const CheckModeTiles = setInterval(() => {
const gameTiles = document.querySelectorAll('.game-tile');
if (gameTiles.length > 0) {
gameTiles.forEach((tile) => {
tile.classList.toggle('dark', toggleDarkMode);
});
}
if (
(toggleDarkMode && lightModeIconEl.classList.contains('hidden')) ||
(!toggleDarkMode && !lightModeIconEl.classList.contains('hidden'))
) {
clearInterval(CheckModeTiles);
}
}, 100);
});
gameModesEls.forEach((el) => {
el.addEventListener('click', (e) => {
e.target.id === 'pvp-mode' ? (game = new Game(false)) : (game = new Game(true));
gameMenuEl.classList.add('hidden');
gameBoxEl.classList.remove('hidden');
});
});
gameBoardEl.addEventListener('click', function (e) {
if (e.target.matches('.game-tile') || e.target.matches('.game-tile p')) {
const tileID = Number(e.target.closest('.game-tile').id.substr(3));
game.placeMark(tileID);
}
});
btnNewGameEl.addEventListener('click', () => {
game = undefined;
gameBoxEl.classList.add('hidden');
gameMenuEl.classList.remove('hidden');
});
btnNextRoundEl.addEventListener('click', () => {
game.prepareGame();
});