-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
143 lines (117 loc) · 3.27 KB
/
script.js
File metadata and controls
143 lines (117 loc) · 3.27 KB
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
const CELL_COUNT = 9;
let currentPlayer = 'O';
let gameMode = '';
const gameBoard = document.getElementById("board");
const gameOverMessage = document.getElementById('game-over-message');
const restartButton = document.getElementById('restart');
const startDialog = document.getElementById('start-game-dialog');
const endDialog = document.getElementById('game-over-dialog');
// Init Game Board
for (let i = 0; i < CELL_COUNT; i++) {
const cell = document.createElement('li');
gameBoard.appendChild(cell);
}
const cells = [...gameBoard.querySelectorAll('li')];
const PLAYER_COLORS = {
O: "#ff006b",
X: "#00e0ff",
}
const markCell = (cell, symbol) => {
if (cell.textContent !== '') return;
cell.textContent = symbol;
cell.style.setProperty('--player-color', PLAYER_COLORS[symbol]);
cell.classList.add('current-player');
}
const playerMove = (cell, symbol) => {
if (cell.textContent !== '') return;
markCell(cell, symbol);
}
const computerMove = () => {
const emptyCells = cells.filter(cell => cell.textContent === '');
if (emptyCells.length === 0) return;
const randomNumber = Math.floor(Math.random() * emptyCells.length);
markCell(emptyCells[randomNumber], 'X');
}
const onePlayerHandler = (cell) => {
playerMove(cell, 'O');
if (checkGameOver()) return;
disableGameBoard();
setTimeout(() => {
computerMove();
checkGameOver();
enableGameBoard();
}, 300);
}
const twoPlayersHandler = (cell) => {
markCell(cell, currentPlayer);
currentPlayer = currentPlayer === 'O' ? 'X' : 'O';
}
const checkGameOver = () => {
const winCombinations = [
[0, 1, 2], [3, 4, 5], [6, 7, 8],
[0, 3, 6], [1, 4, 7], [2, 5, 8],
[0, 4, 8], [2, 4, 6]
];
for (const [a, b, c] of winCombinations) {
const valA = cells[a].textContent;
const valB = cells[b].textContent;
const valC = cells[c].textContent;
if (valA && valA === valB && valB === valC) {
renderMessage(valA === 'O' ? `Yay, player ${valA} won 🥳` : `Hehe X has won 🚀`)
disableGameBoard();
setTimeout(() => {
endDialog.open = true;
}, 1200);
return true;
}
}
const isDraw = [...cells].every(cell => cell.textContent !== '');
if (isDraw) {
renderMessage("Ops... it's a draw 🤷♀️")
disableGameBoard();
setTimeout(() => {
endDialog.open = true;
}, 1200);
return true;
}
return false;
}
const restartGame = () => {
cells.forEach((cell) => {
cell.textContent = '';
cell.classList.remove('current-player');
});
enableGameBoard();
renderMessage('');
endDialog.open = false;
startDialog.open = true;
}
const renderMessage = (message) => {
gameOverMessage.textContent = message;
};
const disableGameBoard = () => {
cells.forEach(cell => {
cell.style.pointerEvents = 'none';
})
}
const enableGameBoard = () => {
cells.forEach(cell => {
cell.style.pointerEvents = 'auto';
})
}
gameBoard.addEventListener('click', function (e) {
const cell = e.target.closest('li');
// avoid fast double click on same cell
if (cell.textContent !== '') return;
if (gameMode === 'one-player') {
onePlayerHandler(cell)
} else if (gameMode === 'two-players') {
twoPlayersHandler(cell);
}
checkGameOver();
})
// Assegna il gameMode quando il dialog viene chiuso
startDialog.addEventListener('close', () => {
gameMode = startDialog.returnValue;
})
restartButton.addEventListener('click', restartGame);