-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
237 lines (210 loc) · 8.17 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
document.addEventListener('DOMContentLoaded', () => {
let currentPlayer = 'X';
let gameActive = true;
let playWithPC = false;
const cells = document.querySelectorAll('.cell');
const currentPlayerDisplay = document.getElementById('currentPlayer');
const winnerDisplay = document.getElementById('winnerDisplay');
const playAgainButton = document.getElementById('playAgain');
const playWithPCButton = document.getElementById('playWithPC');
const winnerAudio = document.getElementById('winnerAudio');
const countdownDisplay = document.getElementById('countdownDisplay');
const countdownElement = document.getElementById('countdown');
const soundControlButton = document.getElementById('soundControl');
const soundIcon = document.getElementById('soundIcon');
const backgroundMusic = document.getElementById('backgroundMusic')
const winningConditions = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8],
[0, 4, 8],
[2, 4, 6]
];
// Set initial state of sound icon
soundIcon.src = 'soundoff.png';
// Ensure background music starts playing on load
backgroundMusic.play();
// Sound Control
let isSoundOn = false; // Initially sound is off
soundControlButton.addEventListener('click', () => {
isSoundOn = !isSoundOn;
if (isSoundOn) {
soundIcon.src = 'soundplay.png';
backgroundMusic.play();
} else {
soundIcon.src = 'soundoff.png';
backgroundMusic.pause();
}
});
const updatePlayerDisplay = () => {
currentPlayerDisplay.textContent = playWithPC && currentPlayer === 'O' ? 'PC (O)' : currentPlayer === 'X' ? 'Player 1 (X)' : 'Player 2 (O)';
};
const handleCellPlayed = (cell) => {
cell.innerHTML = currentPlayer;
cell.setAttribute('data-player', currentPlayer);
cell.style.backgroundColor = currentPlayer === 'X' ? 'bisque' : 'rgb(236, 188, 129)';
};
const handlePlayerChange = () => {
currentPlayer = currentPlayer === 'X' ? 'O' : 'X';
updatePlayerDisplay();
};
const displayWinner = (winner) => {
setTimeout(() => {
if (winner === 'Draw') {
winnerDisplay.textContent = "It's a Draw!";
winnerAudio.play();
} else {
winnerDisplay.textContent = playWithPC && winner === 'O' ? 'PC Wins!' : `Player ${winner === 'X' ? '1 (X)' : '2 (O)'} Wins!`;
winnerAudio.play();
}
winnerDisplay.classList.add('show');
console.log('Winner Display: ', winnerDisplay.textContent); winnerAudio.play();
}, 100);
};
const checkWinner = () => {
let roundWon = false;
for (let i = 0; i < winningConditions.length; i++) {
const winCondition = winningConditions[i];
const a = cells[winCondition[0]].getAttribute('data-player');
const b = cells[winCondition[1]].getAttribute('data-player');
const c = cells[winCondition[2]].getAttribute('data-player');
if (a && a === b && a === c) {
roundWon = true;
break;
}
}
if (roundWon) {
gameActive = false;
displayWinner(currentPlayer);
return;
}
if ([...cells].every(cell => cell.getAttribute('data-player') !== null)) {
gameActive = false;
displayWinner('Draw');
}
};
const handleCellClick = (event) => {
const cell = event.target;
if (cell.getAttribute('data-player') || !gameActive) {
return;
}
handleCellPlayed(cell);
checkWinner();
if (gameActive) {
handlePlayerChange();
if (playWithPC && currentPlayer === 'O') {
setTimeout(pcPlay, 500);
}
}
};
const pcPlay = () => {
const bestMove = getBestMove();
if (bestMove !== null) {
handleCellPlayed(cells[bestMove]);
checkWinner();
if (gameActive) {
handlePlayerChange();
}
}
};
const getBestMove = () => {
let bestMove = null;
let bestValue = -Infinity;
for (let i = 0; i < cells.length; i++) {
if (!cells[i].getAttribute('data-player')) {
cells[i].setAttribute('data-player', 'O');
const moveValue = minimax(false);
cells[i].removeAttribute('data-player');
if (moveValue > bestValue) {
bestValue = moveValue;
bestMove = i;
}
}
}
return bestMove;
};
const minimax = (isMaximizing) => {
let winner = checkWinnerMinimax();
if (winner === 'O') return 1;
if (winner === 'X') return -1;
if (winner === 'Draw') return 0;
if (isMaximizing) {
let bestValue = -Infinity;
for (let i = 0; i < cells.length; i++) {
if (!cells[i].getAttribute('data-player')) {
cells[i].setAttribute('data-player', 'O');
const moveValue = minimax(false);
cells[i].removeAttribute('data-player');
bestValue = Math.max(bestValue, moveValue);
}
}
return bestValue;
} else {
let bestValue = Infinity;
for (let i = 0; i < cells.length; i++) {
if (!cells[i].getAttribute('data-player')) {
cells[i].setAttribute('data-player', 'X');
const moveValue = minimax(true);
cells[i].removeAttribute('data-player');
bestValue = Math.min(bestValue, moveValue);
}
}
return bestValue;
}
};
const checkWinnerMinimax = () => {
for (let i = 0; i < winningConditions.length; i++) {
const [a, b, c] = winningConditions[i];
const cellsArray = [...cells];
if (cellsArray[a].getAttribute('data-player') &&
cellsArray[a].getAttribute('data-player') === cellsArray[b].getAttribute('data-player') &&
cellsArray[a].getAttribute('data-player') === cellsArray[c].getAttribute('data-player')) {
return cellsArray[a].getAttribute('data-player');
}
}
if ([...cells].every(cell => cell.getAttribute('data-player') !== null)) {
return 'Draw';
}
return null;
};
const resetGame = () => {
currentPlayer = 'X';
gameActive = true;
playWithPC = false;
cells.forEach(cell => {
cell.innerHTML = '';
cell.removeAttribute('data-player');
cell.style.backgroundColor = ' rgb(235, 235, 183';
});
winnerDisplay.textContent = '';
winnerDisplay.classList.remove('show');
updatePlayerDisplay();
};
const startGameWithPC = () => {
resetGame();
playWithPC = true;
updatePlayerDisplay();
// Display countdown overlay
countdownDisplay.style.display = 'flex';
let count = 3;
countdownElement.textContent = count.toString();
const countdownInterval = setInterval(() => {
count--;
countdownElement.textContent = count.toString();
if (count === 0) {
clearInterval(countdownInterval);
countdownDisplay.style.display = 'none';
if (playWithPC && currentPlayer === 'O') {
setTimeout(pcPlay, 500);
}
}
}, 1000);
};
cells.forEach(cell => cell.addEventListener('click', handleCellClick));
playAgainButton.addEventListener('click', resetGame);
playWithPCButton.addEventListener('click', startGameWithPC);
updatePlayerDisplay();
});