-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
153 lines (129 loc) · 3.67 KB
/
index.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// // console.log("Test...");
// let currentTurn = "X";
// let xScore = 0,
// tScore = 0,
// oScore = 0;
// let gameOver = false;
// let wins = [
// [0, 1, 2],
// [0, 3, 6],
// [0, 4, 8],
// [1, 4, 7],
// [2, 4, 6],
// [2, 5, 8],
// [3, 4, 5],
// [6, 7, 8],
// ];
// let filledBoxes = 0;
// const changeTurn = () => {
// return currentTurn === "X" ? "O" : "X";
// };
// // Functions to check the winner
// const checkWinner = () => {
// let boxSymbol = document.getElementsByClassName("box-symbol");
// wins.forEach((e) => {
// if (
// boxSymbol[e[0]].innerText === boxSymbol[e[1]].innerText &&
// boxSymbol[e[1]].innerText === boxSymbol[e[2]].innerText &&
// boxSymbol[e[0]].innerText !== "" &&
// gameOver === false
// ) {
// if (boxSymbol[e[0]].innerText === "X") {
// xScore++;
// } else if (boxSymbol[e[0]].innerText === "O") {
// oScore++;
// } else if (filledBoxes === 9) tScore++;
// gameOver = true;
// }
// document.querySelector(".x-score").innerText = xScore;
// document.querySelector(".t-score").innerText = tScore;
// document.querySelector(".o-score").innerText = oScore;
// });
// };
// let boxes = document.getElementsByClassName("box");
// Array.from(boxes).forEach((element) => {
// let boxSymbol = element.querySelector(".box-symbol");
// element.addEventListener("click", () => {
// if (boxSymbol.innerText === "") {
// boxSymbol.innerText = currentTurn;
// currentTurn = changeTurn();
// filledBoxes++;
// }
// checkWinner();
// if (!gameOver) {
// document.querySelector(".game-turn").innerText = currentTurn;
// }
// });
// });
let currentTurn = "X";
let gameOver = false;
let wins = [
[0, 1, 2],
[0, 3, 6],
[0, 4, 8],
[1, 4, 7],
[2, 4, 6],
[2, 5, 8],
[3, 4, 5],
[6, 7, 8],
];
let xScore = 0;
let oScore = 0;
const changeTurn = () => {
return currentTurn === "X" ? "O" : "X";
};
const checkWin = () => {
let boxes = document.getElementsByClassName("box-symbol");
wins.forEach((combination) => {
if (
boxes[combination[0]].innerText === boxes[combination[1]].innerText &&
boxes[combination[1]].innerText === boxes[combination[2]].innerText &&
boxes[combination[0]].innerText !== ""
) {
gameOver = true;
// for (var i = 0; i < 3; i++) {
// boxes[combination[i]].parentNode.style.cssText =
// "\
// transform: scale(1.1); \
// transition: 1s; \
// border: 2px solid red; \
// ";
// if (boxes[combination[i]].innerText === "X") {
// boxes[combination[i]].parentNode.style.backgroundColor = "#00b2c6";
// } else {
// boxes[combination[i]].parentNode.style.backgroundColor = "#ff581a";
// }
// }
if (boxes[combination[0]].innerText === "X") {
xScore++;
} else if (boxes[combination[0]].innerText === "O") {
oScore++;
}
}
document.querySelector(".x-score").innerText = xScore;
document.querySelector(".o-score").innerText = oScore;
});
};
let boxes = document.getElementsByClassName("box");
Array.from(boxes).forEach((element) => {
let boxSymbol = element.querySelector(".box-symbol");
element.addEventListener("click", () => {
if (boxSymbol.innerText === "" && gameOver === false) {
boxSymbol.innerText = currentTurn;
currentTurn = changeTurn();
checkWin();
let turnDisplay = document.querySelector(".game-turn");
turnDisplay.innerText = currentTurn;
}
});
});
let ResetButton = document.querySelector(".reset");
ResetButton.addEventListener("click", () => {
let symbolBox = document.getElementsByClassName("box-symbol");
Array.from(symbolBox).forEach((Symbol) => {
Symbol.innerText = "";
});
let turnDisplay = document.querySelector(".game-turn");
turnDisplay.innerText = "X";
gameOver = false;
});