-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
107 lines (95 loc) · 2.88 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
const wins = [
[0, 4, 8],
[2, 4, 6],
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8],
];
let currentTurn = "X";
let gameOver = false;
let xScore = 0;
let oScore = 0;
// let tScore = 0;
const changeTurn = () => {
currentTurn = currentTurn === "X" ? "O" : "X";
let turnDisplay = document.querySelector(".turn-display");
turnDisplay.innerText = currentTurn;
};
const checkWin = () => {
let boxSymbol = document.getElementsByClassName("box-symbol");
wins.forEach((combination) => {
if (
boxSymbol[combination[0]].innerText ===
boxSymbol[combination[1]].innerText &&
boxSymbol[combination[1]].innerText ===
boxSymbol[combination[2]].innerText &&
boxSymbol[combination[0]].innerText !== ""
) {
gameOver = true;
if (boxSymbol[combination[0]].innerText === "X") {
xScore++;
let xWon = document.querySelector(".xwon");
xWon.innerText = xScore;
let textwin=document.querySelector(".winner");
textwin.innerText="X Won !!!";
} else if (boxSymbol[combination[0]].innerText === "O") {
oScore++;
let oWon = document.querySelector(".owon");
oWon.innerText = oScore;
let textwin=document.querySelector(".winner");
textwin.innerText="O Won !!!";
}
}
});
};
var boxes = document.getElementsByClassName("box");
Array.from(boxes).forEach((ele) => {
let boxSymbol = ele.querySelector(".box-symbol");
ele.addEventListener("click", () => {
if (boxSymbol.innerText === "" && gameOver === false) {
boxSymbol.innerText = currentTurn;
changeTurn();
checkWin();
}
});
});
let ResetButton = document.querySelector("#reset-btn");
ResetButton.addEventListener("click", () => {
let symbolBox = document.getElementsByClassName("box-symbol");
Array.from(symbolBox).forEach((Symbol) => {
Symbol.innerText = "";
});
// let turnDisplay = document.querySelector(".turn-display");
// turnDisplay.innerText = "X";
let turnDisplay = document.querySelector(".turn-display");
turnDisplay.innerText = "X";
currentTurn = "X";
let textwin=document.querySelector(".winner");
textwin.innerText="";
gameOver = false;
});
let Restartbtn = document.querySelector("#restart-btn");
Restartbtn.addEventListener("click", () => {
let symbolBox = document.getElementsByClassName("box-symbol");
Array.from(symbolBox).forEach((Symbol) => {
Symbol.innerText = "";
});
let xWon = document.querySelector(".xwon");
xScore=0;
xWon.innerText = xScore;
let oWon = document.querySelector(".owon");
oScore=0;
oWon.innerText = oScore;
// let tie = document.querySelector(".tie");
// tScore=0;
// tie.innerText = tScore;
let turnDisplay = document.querySelector(".turn-display");
turnDisplay.innerText = "X";
currentTurn = "X";
let textwin=document.querySelector(".winner");
textwin.innerText="";
gameOver = false;
});