-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
109 lines (93 loc) · 3.25 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
const PLAYER_X = 'X';
const PLAYER_O = 'O';
const board = ['', '', '', '', '', '', '', '', ''];
let currentPlayer = PLAYER_X;
let scoreX = 0;
let scoreO = 0;
let gameOver = false;
const boardElement = document.getElementById('board');
const messageElement = document.getElementById('message');
const scoreXElement = document.getElementById('score-x');
const scoreOElement = document.getElementById('score-o');
function checkWin() {
const winningCombos = [
[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 combo of winningCombos) {
const [a, b, c] = combo;
if (board[a] && board[a] === board[b] && board[a] === board[c]) {
return true;
}
}
return false;
}
function checkDraw() {
return !board.includes('');
}
function handleCellClick(event) {
if (gameOver) return;
const cell = event.target;
const cellIndex = parseInt(cell.getAttribute('data-index'));
if (board[cellIndex] === '') {
board[cellIndex] = currentPlayer;
cell.textContent = currentPlayer;
cell.classList.add(currentPlayer);
if (checkWin()) {
messageElement.textContent = `${currentPlayer} wins!`;
updateScore(currentPlayer);
gameOver = true;
// Apply the winning animation to the winning cells
const winningCombos = [
[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 combo of winningCombos) {
const [a, b, c] = combo;
const winningCells = document.querySelectorAll(`.cell[data-index="${a}"], .cell[data-index="${b}"], .cell[data-index="${c}"]`);
winningCells.forEach(cell => cell.classList.add('winner'));
}
} else if (checkDraw()) {
messageElement.textContent = "It's a draw!";
gameOver = true;
} else {
currentPlayer = currentPlayer === PLAYER_X ? PLAYER_O : PLAYER_X;
messageElement.textContent = `Player ${currentPlayer}'s Turn`;
}
}
}
function handleResetClick() {
board.fill('');
const cells = document.querySelectorAll('.cell');
cells.forEach(cell => {
cell.textContent = '';
cell.classList.remove(PLAYER_X, PLAYER_O, 'winner');
});
messageElement.textContent = `Player ${currentPlayer}'s Turn`;
currentPlayer = PLAYER_X;
gameOver = false;
boardElement.classList.remove('game-over');
}
function updateScore(player) {
if (player === PLAYER_X) {
scoreX++;
scoreXElement.textContent = `Player X: ${scoreX}`;
} else {
scoreO++;
scoreOElement.textContent = `Player O: ${scoreO}`;
}
}
function initializeBoard() {
for (let i = 0; i < 9; i++) {
const cell = document.createElement('div');
cell.className = 'cell';
cell.setAttribute('data-index', i);
cell.addEventListener('click', handleCellClick);
boardElement.appendChild(cell);
}
}
document.getElementById('reset-button').addEventListener('click', handleResetClick);
initializeBoard();
messageElement.textContent = `Player ${currentPlayer}'s Turn`;