-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproject.js
More file actions
72 lines (60 loc) · 1.87 KB
/
project.js
File metadata and controls
72 lines (60 loc) · 1.87 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
let boxes = document.querySelectorAll(".button")
let reset = document.querySelector(".reset")
let winning_line = document.querySelector(".winning")
let new_btn = document.querySelector(".new_game")
let turnX = true;
let winner = [[0,1,2],[0,3,6],[0,4,8],[1,4,7],[2,5,8],[2,4,6],[3,4,5],[6,7,8]];
reset.addEventListener("click",() =>{
turnX = true;
for(let b of boxes){
b.innerText="";
b.disabled=false;
winning_line.innerText = "";
}
})
new_btn.addEventListener("click",() =>{
turnX = true;
for(let b of boxes){
b.innerText="";
b.disabled=false;
}
winning_line.innerText = "";
})
boxes.forEach((box) => {
box.addEventListener("click",() =>{
console.log("box was clicked")
if(turnX){
box.innerText = "X";
turnX = false;
}
else{
box.innerText = "0";
turnX = true;
}
box.disabled = true;
checkwin();
});
})
const checkwin = () => {
for (let win of winner) {
// console.log(win[0],win[1],win[2]);
// console.log(boxes[win[0]].innerText, boxes[win[1]].innerText, boxes[win[2]].innerText);
let posval1 = boxes[win[0]].innerText;
let posval2 = boxes[win[1]].innerText;
let posval3 = boxes[win[2]].innerText;
if(posval1!=""&&posval2!=""&&posval3!=""){
if(posval1==posval2&&posval2==posval3){
if(posval1=="0")
{console.log(`Congratulation O won the game`);
winning_line.innerText=`Congratulation O won the game`}
if(posval1=="X"){
{console.log(`Congratulation X won the game`);
winning_line.innerText=`||Congratulations!! X won the game||`}
}
for(b of boxes){
b.disabled = true;
}
}
}
}
}