-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
76 lines (64 loc) · 1.88 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
const cells = document.querySelectorAll('[data-cell]');
const resultScreen = document.getElementById('resultScreen');
const resultText = document.getElementById('resultText');
const newGameButton = document.getElementById('newGameButton');
const restartButton = document.getElementById('restartButton');
let currentPlayer = 'X';
let board = Array(9).fill(null);
const 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],
];
function handleClick(cell, index) {
if (board[index] || resultScreen.style.display === 'flex') return;
board[index] = currentPlayer;
cell.textContent = currentPlayer;
if (checkWin(currentPlayer)) {
showResult(`${currentPlayer} Wins!`);
} else if (board.every(cell => cell !== null)) {
showResult("It's a Draw!");
} else {
currentPlayer = currentPlayer === 'X' ? 'O' : 'X';
}
}
function checkWin(player) {
return winningCombinations.some(combination =>
combination.every(index => board[index] === player)
);
}
function showResult(message) {
resultText.textContent = message;
resultScreen.style.display = 'flex';
}
function resetGame() {
board.fill(null);
currentPlayer = 'X';
resultScreen.style.display = 'none';
cells.forEach(cell => {
cell.textContent = '';
});
}
// Add event listeners to cells and buttons
function initializeGame() {
cells.forEach((cell, index) => {
cell.textContent = '';
cell.removeEventListener('click', () => handleClick(cell, index));
cell.addEventListener('click', () => handleClick(cell, index), { once: true });
});
}
newGameButton.addEventListener('click', () => {
resetGame();
initializeGame();
});
restartButton.addEventListener('click', () => {
resetGame();
initializeGame();
});
// Initialize the game on page load
initializeGame();