-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathscript.js
72 lines (64 loc) · 1.95 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
/* eslint-disable no-use-before-define */
// Get elements
const board = document.getElementById('board');
const restartBtn = document.getElementById('restartBtn');
const message = document.getElementById('message');
let currentPlayer = 'X';
let gameBoard = Array(9).fill(null); // 3x3 grid, initialized to null (empty)
const winPatterns = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8], // Rows
[0, 3, 6],
[1, 4, 7],
[2, 5, 8], // Columns
[0, 4, 8],
[2, 4, 6], // Diagonals
];
// Check for a winner or draw
const checkWinner = () => {
const winner = winPatterns.some(([a, b, c]) => {
if (
gameBoard[a]
&& gameBoard[a] === gameBoard[b]
&& gameBoard[a] === gameBoard[c]
) {
message.textContent = `${gameBoard[a]} wins!`;
board.style.pointerEvents = 'none'; // Disable clicks after game ends
return true;
}
return false;
});
if (!winner && !gameBoard.includes(null)) {
message.textContent = "It's a draw!";
}
};
// Create the board cells
const createBoard = () => {
board.innerHTML = ''; // Clear any existing cells
gameBoard.forEach((cell, index) => {
const cellElement = document.createElement('div');
cellElement.classList.add('cell');
cellElement.textContent = cell;
cellElement.addEventListener('click', () => handleCellClick(index));
board.appendChild(cellElement);
});
};
// Handle cell click
const handleCellClick = (index) => {
if (gameBoard[index] !== null) return; // If cell is already filled, return
gameBoard[index] = currentPlayer;
currentPlayer = currentPlayer === 'X' ? 'O' : 'X'; // Switch player
createBoard();
checkWinner();
};
// Restart the game
restartBtn.addEventListener('click', () => {
gameBoard = Array(9).fill(null); // Reset the game board
currentPlayer = 'X'; // Reset to Player X
createBoard();
message.textContent = ''; // Clear the message
board.style.pointerEvents = 'auto'; // Enable clicks again
});
// Initialize the game
createBoard();