-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoe.js
57 lines (50 loc) · 1.41 KB
/
toe.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
let currentPlayer = 'X';
let gameActive = true;
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]
];
const cells = document.querySelectorAll('.cell');
const status = document.querySelector('.status');
function checkWinner() {
for (const combo of winningCombos) {
const [a, b, c] = combo;
if (cells[a].textContent === cells[b].textContent &&
cells[b].textContent === cells[c].textContent &&
cells[a].textContent !== '') {
gameActive = false;
status.textContent = `Player ${currentPlayer} wins!`;
break;
}
}
}
function checkDraw() {
if ([...cells].every(cell => cell.textContent !== '')) {
gameActive = false;
status.textContent = "It's a draw!";
}
}
function makeMove(cellIndex) {
if (!gameActive || cells[cellIndex].textContent !== '')
return;
cells[cellIndex].textContent = currentPlayer;
checkWinner();
if (gameActive) {
currentPlayer = currentPlayer === 'X' ? 'O' : 'X';
status.textContent = `Player ${currentPlayer}'s turn`;
checkDraw();
}
}
function resetGame() {
cells.forEach(cell => cell.textContent = '');
currentPlayer = 'X';
gameActive = true;
status.textContent = "Player X's turn";
}
resetGame();