forked from TTVPoopooumgood/Games
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
138 lines (76 loc) · 2.85 KB
/
app.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
let music = new Audio("jim-yosef-x-roy-knox-sun-goes-down-ncs-release_KB96fVy4.mp3")
let audioTurn = new Audio("ting.mp3")
let gameOver = new Audio("game-over-sound-effects-high-quality_zpZvgEQk.mp3")
let gameStart = new Audio("Game Start Sound Effect.mp3")
let turn = "X"
let draw = document.getElementsByClassName("boxtext").innerText;
let isGameOver = false;
let checkwinner = true
// function to chnage the turn
const changeTurn = ()=>{
return turn === "X"? "O" : "X";
}
// function to check the win
const checkwin = ()=>{
let boxtext = document.getElementsByClassName("boxtext")
let wins = [
[0,1,2],
[3,4,5],
[6,7,8],
[0,3,6],
[1,4,7],
[2,5,8],
[0,4,8],
[2,4,6]
]
wins.forEach(e => {
if(checkwinner){
if((boxtext[e[0]].innerText === boxtext[e[1]].innerText)&& (boxtext[e[2]].innerText === boxtext[e[1]].innerText)&&(boxtext[e[0]].innerText !== '')){
gameOver.play();
checkwinner = false
document.getElementsByTagName("img")[0].src = "excited-spin.gif";
document.querySelector(".info").innerText = "you win " + boxtext[e[0]].innerText;
document.querySelector(".info").style.backgroundColor = "brown"
isGameOver = true;
document.getElementsByTagName("img")[0].style.width = "200px";
}}
})}
//main game logic
let boxes = document.getElementsByClassName("box")
Array.from(boxes).forEach((element) =>{
let boxtext = element.querySelector(".boxtext")
element.addEventListener('click',()=>{
if(boxtext.innerText ===""){
boxtext.innerText = turn;
turn = changeTurn();
audioTurn.play();
checkwin();
if(!isGameOver){
document.getElementsByClassName("info")[0].innerText = "your turn " + turn;
;}
}
})
})
document.querySelector(".playsong").addEventListener('click',(e)=>{
if(e.target.innerText === "?"){
e.target.innerText = '????';
music.play();
}else{
e.target.innerText = '?'
music.pause()
}
})
document.getElementById("reset").addEventListener('click',(e)=>{
gameStart.play();
let boxtexts = document.querySelectorAll('.boxtext');
Array.from(boxtexts).forEach(element => {
element.innerText = ""
});
isGameOver = false;
turn = "X";
checkwinner = true;
if(!isGameOver){
document.getElementsByClassName("info")[0].innerText = "your turn "+ turn;}
document.getElementsByTagName("img")[0].style.width = "0px"
document.querySelector(".info").style.backgroundColor = "white"
})