-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
64 lines (48 loc) · 1.6 KB
/
script.js
File metadata and controls
64 lines (48 loc) · 1.6 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
let currentPlayer = 'X';
let boardState = ['', '', '', '', '', '', '', '', ''];
let isGameOver = false;
const cells = document.querySelectorAll('.cell');
const statusDisplay = document.querySelector('#status p');
cells.forEach(cell => {
cell.addEventListener('click', () => {
if (isGameOver || cell.textContent !== '') return;
const cellIndex = cell.getAttribute('data-index');
boardState[cellIndex] = currentPlayer;
cell.textContent = currentPlayer;
if (checkWinner()) {
isGameOver = true;
statusDisplay.textContent = `Player ${currentPlayer} wins!`;
} else if (boardState.every(cell => cell !== '')) {
isGameOver = true;
statusDisplay.textContent = "It's a draw!";
} else {
currentPlayer = (currentPlayer === 'X') ? 'O' : 'X';
statusDisplay.textContent = `Player ${currentPlayer}'s turn`;
}
});
});
function checkWinner() {
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]
];
return winningCombinations.some(combination => {
const [a, b, c] = combination;
return boardState[a] && boardState[a] === boardState[b] && boardState[a] === boardState[c];
});
}
function resetGame() {
boardState = ['', '', '', '', '', '', '', '', ''];
isGameOver = false;
currentPlayer = 'X';
statusDisplay.textContent = `Player ${currentPlayer}'s turn`;
cells.forEach(cell => {
cell.textContent = '';
});
}