-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
107 lines (96 loc) · 2.89 KB
/
Copy pathscript.js
File metadata and controls
107 lines (96 loc) · 2.89 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
const container = document.getElementById("container");
const indicator = document.getElementById("indicator");
const resetButton = document.getElementById("reset");
// false = X, True = O
let Switch = false;
const X = "X";
const O = "O";
let gameOver = false;
indicator.textContent = "Player X's turn";
createGrid(3, 3);
// Event listener for clicks on the container
container.addEventListener('click', function() {
if (!gameOver) {
switchValue();
const winner = checkWin();
if (winner === null && isDraw()) {
indicator.textContent = `Draw!`;
gameOver = true;
} else if (winner) {
gameOver = true;
indicator.textContent = `Player ${winner} wins!`;
}
}
});
// Event listener for the reset button
resetButton.addEventListener('click', function() {
reset();
});
// Function to create the grid
function createGrid(rows, cols) {
container.innerHTML = '';
for (let i = 0; i < rows * cols; i++) {
const gridItem = document.createElement('div');
gridItem.classList.add('grid-item');
container.appendChild(gridItem);
}
}
// Function to handle switching between X and O
function switchValue() {
if (!event.target.classList.contains('grid-item') || event.target.textContent) {
return;
}
Switch = !Switch;
console.log("Switch Value: " + Switch);
if (Switch) {
if (event.target.classList.contains('grid-item') && !event.target.textContent) {
indicator.textContent = "Player O's turn";
event.target.textContent = X;
}
} else {
if (event.target.classList.contains('grid-item') && !event.target.textContent) {
indicator.textContent = "Player X's Turn";
event.target.textContent = O;
}
}
}
// Function to check if there's a winner
function checkWin() {
const gridItems = document.querySelectorAll('.grid-item');
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 (let combo of winningCombos) {
const [a, b, c] = combo;
if (gridItems[a].textContent && gridItems[a].textContent === gridItems[b].textContent && gridItems[a].textContent === gridItems[c].textContent) {
return gridItems[a].textContent;
}
}
return null;
}
// Function to reset the game
function reset() {
const gridItems = document.querySelectorAll('.grid-item');
indicator.textContent = "Player X's turn";
gameOver = false;
gridItems.forEach(item => {
item.textContent = '';
});
Switch = false;
}
function isDraw() {
const gridItems = document.querySelectorAll('.grid-item');
for (let item of gridItems) {
if (!item.textContent) {
return false;
}
}
return true;
}