-
Notifications
You must be signed in to change notification settings - Fork 0
/
app2.js
197 lines (194 loc) · 4.86 KB
/
app2.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
let order=[];
let started=0;
var startG= document.querySelector('#startG');
var topLeft=document.querySelector('#topLeft');
var bottomLeft=document.querySelector('#bottomLeft');
var topRight=document.querySelector('#topRight');
var bottomRight=document.querySelector('#bottomRight');
var isStrict= document.querySelector('#strict');
let on= true;
var round=0;
var turn=0;
var cnt=0;
var comp;
let strictt= false;
startG.addEventListener('click',startGame);
// isStrict.addEventListener('click',strict);
// function strict(){
// if(isStrict.checked==true){strictt=true;}
// }
function startGame(){
clearGame();
// isStrict.disabled=true;
started++; round=1; turn=0;
// isStrict.disabled=true;
playGame();
}
function clearGame(){
order=[];
round=0;
// isStrict.disabled=false;
}
// Computer's Turn
function compTurn(){
document.querySelector('#demo').innerHTML="Round: "+round;
document.querySelector('#result').innerHTML="Memorize the pattern!";
let temp= Math.floor(Math.random()*4)+1;
order.push(temp);
setTimeout(()=>{
addSound(temp);
addAnimation(temp);
},200)
turn=1;
setTimeout(playGame,400);
// playGame();
}
function playGame(){
if(round>20){
document.querySelector('#result').innerHTML="Congratulations! You won🎊✨";
// round=0;
// isStrict.disabled=false;
}
else if(turn==0){compTurn();}
else if(turn==1){
cnt=0;
document.querySelector('#result').innerHTML="Repeat the pattern!";
// turn=0;
}
}
topLeft.addEventListener('click',one);
topRight.addEventListener('click',two);
bottomRight.addEventListener('click',three);
bottomLeft.addEventListener('click',four);
function check(value,count){
if(order.length==0){alert("Wait for computer's turn!"); return 2;}
else if(order[count-1]==value){return 1;}
else{return 0;}
}
function wrongAnswer(){
alert('Wrong Pattern!')
document.querySelector('#result').innerHTML="Wrong Pattern ❌";
round=1;
turn=0;
order=[];
setTimeout(playGame,1200);
}
function one(){
cnt++;
addAnimation(1);
let ans=check(1,cnt);
if(ans==1){
addSound(1);
if(cnt==order.length){
round++;
turn=0;
// order=[];
setTimeout(playGame,500);
} }
else if(ans==0){
addSound(5);
wrongAnswer();
}
}
function two(){
cnt++;
addAnimation(2);
let ans= check(2,cnt);
if(ans==1){
addSound(2);
if(cnt==order.length){
round++;
turn=0;
// order=[];
setTimeout(playGame,500);
} }
else if(ans==0){
addSound(5);
wrongAnswer();
}
}
function three(){
cnt++;
addAnimation(3);
let ans=check(3,cnt);
if(ans==1){
addSound(3);
if(cnt==order.length){
round++;
turn=0;
// order=[];
setTimeout(playGame,500);
} }
else if(ans==0){
addSound(5);
wrongAnswer();
}
}
function four(){
cnt++;
addAnimation(4);
let ans= check(4,cnt);
if(ans==1){
addSound(4);
if(cnt==order.length){
round++;
turn=0;
// order=[];
setTimeout(playGame,500);
} }
else if(ans==0){
addSound(5);
wrongAnswer();
}
}
function addAnimation(value){
console.log(value);
switch(value){
case 1:
topLeft.classList.add('animation1');
setTimeout(function(){
topLeft.classList.remove('animation1');
},200)
break;
case 2:
topRight.classList.add('animation2');
setTimeout(function(){
topRight.classList.remove('animation2');
},200)
break;
case 3:
bottomRight.classList.add('animation3');
setTimeout(function(){
bottomRight.classList.remove('animation3');
},200)
break;
case 4:
bottomLeft.classList.add('animation4');
setTimeout(function(){
bottomLeft.classList.remove('animation4');
},200)
break;
// default:
// console.log(value);
}
}
function addSound(value){
switch(value){
case 1:
document.querySelector('#sound1').play();
break;
case 2:
document.querySelector('#sound2').play();
break;
case 3:
document.querySelector('#sound3').play();
break;
case 4:
document.querySelector('#sound4').play();
break;
case 5:
document.querySelector('#sound5').play();
// default:
// console.log(value);
}
}