-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
85 lines (75 loc) · 2.31 KB
/
script.js
File metadata and controls
85 lines (75 loc) · 2.31 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
const board = document.getElementById('board');
const cells = document.querySelectorAll('.cell');
let currentPlayer = 'X';
let gameState = ['', '', '', '', '', '', '', '', ''];
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]
];
function handleCellClick(event) {
const clickedCell = event.target;
const clickedCellIndex = parseInt(clickedCell.getAttribute('data-index'));
if (gameState[clickedCellIndex] !== '' || !isGameActive()) {
return;
}
// Update game state and UI
gameState[clickedCellIndex] = currentPlayer;
clickedCell.textContent = currentPlayer;
clickedCell.classList.add(currentPlayer);
// Check for a winner
const winningCombination = checkForWinner();
if (winningCombination) {
// Highlight the winning cells
highlightWinningCells(winningCombination);
// Delay the winner announcement to allow the UI to update
setTimeout(() => {
alert(`Player ${currentPlayer} has won!`);
resetGame();
}, 10); // Small delay to ensure the UI updates
} else if (isBoardFull()) {
setTimeout(() => {
alert('Game is a draw!');
resetGame();
}, 10);
} else {
// Switch players
currentPlayer = currentPlayer === 'X' ? 'O' : 'X';
}
}
function checkForWinner() {
for (let condition of winningConditions) {
const [a, b, c] = condition;
if (gameState[a] !== '' && gameState[a] === gameState[b] && gameState[a] === gameState[c]) {
return condition; // Return the winning combination
}
}
return null; // No winner yet
}
function highlightWinningCells(winningCombination) {
winningCombination.forEach(index => {
cells[index].classList.add('winning-cell');
});
}
function isBoardFull() {
return gameState.every(cell => cell !== '');
}
function isGameActive() {
return !checkForWinner() && !isBoardFull();
}
function resetGame() {
gameState = ['', '', '', '', '', '', '', '', ''];
currentPlayer = 'X';
cells.forEach(cell => {
cell.textContent = '';
cell.classList.remove('X', 'O', 'winning-cell');
});
}
cells.forEach(cell => {
cell.addEventListener('click', handleCellClick);
});