-
Notifications
You must be signed in to change notification settings - Fork 0
/
js.js
144 lines (116 loc) · 3.54 KB
/
js.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
let audioTurn = new Audio("ting.mp3")
let gameover = new Audio("gameover.mp3")
let i=10;
let isgameover=false;
let turn="X";
document.querySelector(".turn").innerText="Turn For :"+ turn
// alert()
// function to change turn
let changeturn=()=>{
if(turn=="X")
return "O"
if (turn=="O")
return "X"
}
// function to check winning condition
const checkwinner=()=>{
wins=[
[0, 1, 2, 5, 5, 0],
[3, 4, 5, 5, 15, 0],
[6, 7, 8, 5, 25, 0],
[0, 3, 6, -5, 15, 90],
[1, 4, 7, 5, 15, 90],
[2, 5, 8, 15, 15, 90],
[0, 4, 8, 5, 15, 45],
[2, 4, 6, 5, 15, 135],
]
insidebox =document.querySelectorAll(".insidebox")
console.log(insidebox)
wins.forEach(e=>{
if((insidebox[e[0]].innerText === insidebox[e[1]].innerText) && (insidebox[e[2]].innerText === insidebox[e[1]].innerText) && (insidebox[e[0]].innerText !== "") )
{
document.querySelector(".turn").innerText="winner is :" + turn
isgameover=true;
document.querySelector(".line").style.transform = `translate(${e[3]}vw, ${e[4]}vw) rotate(${e[5]}deg)`
document.querySelector(".line").style.width = "20vw";
setTimeout(gameisover,1400)
}
else{isgameover=false}
})
if(!isgameover)
{i--;}
}
// if(i==1)
// {
// document.querySelector(".turn").innerText=" Match Draws !"}
// starts here;
// main logic - game starts from here
insidebox =document.querySelectorAll(".insidebox")
Array.from(insidebox).forEach(element =>{ //also working for- insidebox.forEach()....
element.addEventListener('click',()=>{
if(element.innerText == "")
{
element.innerText=turn;
audioTurn.play();
checkwinner();
turn = changeturn(); //ye check winner ke bad me rahega
console.log(isgameover)
console.log(i)
if (!isgameover && i!==1){
// alert()
document.querySelector(".turn").innerText ="Turn For:" + turn
}
if(i===1 && !isgameover){
document.querySelector(".turn").innerText ="Match Draws!"
setTimeout(gameisover,1400)
}
}
}
)
})
// event listener to reset all
let reset=document.querySelector('.reset-btn')
reset.addEventListener('click',()=>{
insidebox=document.querySelectorAll(".insidebox")
Array.from(insidebox).forEach(element =>{
element.innerText = ""
})
turn="X";
document.querySelector(".turn").innerText ="Tern For :"+turn
document.querySelector(".line").style.width = "0"
isgameover=false;
i=10;
})
let resetall=()=>{
insidebox=document.querySelectorAll(".insidebox")
Array.from(insidebox).forEach(element =>{
element.innerText = ""
})
turn="X";
document.querySelector(".turn").innerText ="Tern For :"+turn
document.querySelector(".line").style.width = "0"
isgameover=false;
i=10;
}
let gameisover=()=>{
resetall();
}
// function resetfn() {
// insidebox = document.querySelectorAll(".insidebox");
// Array.from(insidebox).forEach(element => {
// element.innerText = "";
// }
// );
// // turn="X";
// // document.querySelector(".turn").innerText ="Turn For :"+turn
// }
// let insidebox = document.querySelectorAll(".insidebox")
// Array.from(insideboxes).forEach( element =>{
// element.addEventListener('click', ()=>{
// let boxes= document.querySelectorAll(".box")
// element.innerText =turn
// }
// )
// // alert()
// })
// console.log(boxes)