-
Notifications
You must be signed in to change notification settings - Fork 0
/
app1.js
275 lines (227 loc) · 6.14 KB
/
app1.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
let score ;
var high1 ;
var n;
n = window.localStorage.length;
if (n==0 || n==1)
{
localStorage.setItem("highscore1",0);
}
var high1 = localStorage.getItem("highscore1");
document.querySelector('#highscore').innerHTML = `${localStorage.getItem("highscore1")}`;
console.log(high1);
let colors = ["green","red","blue","orange","yellow","brown","grey"]
var ctr=0;
let boxColors = colors.concat(colors)
boxColors = boxColors.concat(boxColors);
boxColors = boxColors.concat(colors);
console.log(boxColors);
function shuffle(array) {
var currentIndex = array.length, randomIndex;
while (0 !== currentIndex) {
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex--;
[array[currentIndex], array[randomIndex]] = [
array[randomIndex], array[currentIndex]];
}
return array;
}
shuffle(boxColors);
var y = document.getElementById("mouse");
function playSound(){
y.play();
}
let upboxes = document.getElementById("up4x4").querySelectorAll('.upbox');
let upColors=boxColors.slice(16);
for (let i = 0; i < 16; i++)
{
upboxes[i].style.backgroundColor = upColors[i];
}
shuffle(boxColors);
function boxClicked(i)
{
ctr++;
let x1=Math.floor(i/6),y1=i%6,x2=Math.floor(eNum/6),y2=eNum%6;
if(setMover(x1,y1,x2,y2))
{
playSound();
boxNum=i;
elem=downboxes[boxNum];
clearInterval(animateId);
animateId = setInterval(frame, 6);
document.querySelector('.moves').innerHTML = `${ctr}`;
}
else
{
}
}
let downboxes = document.getElementById("down6x6").querySelectorAll('.downbox');
for (let i = 0; i < 36; i++)
{
downboxes[i].style.backgroundColor = boxColors[i];
downboxes[i].onclick=function(){
boxClicked(i);
// ctr++;
};
}
downboxes[35].onclick=function(){
boxClicked(35);
};
let animateId=null,pos=0,elem=null,boxNum=null,topVal=0,leftVal=0,eBox=downboxes[35],eNum=35,mover=null;
function moveLeft()
{
leftVal+=5;
elem.style.left=leftVal+"px";
}
function moveRight()
{
leftVal-=5;
elem.style.left=leftVal+"px";
}
function moveTop()
{
topVal+=5;
elem.style.top=topVal+"px";
}
function moveBottom()
{
topVal-=5;
elem.style.top=topVal+"px";
}
let downSelect = document.querySelector('.whole');
function frame() {
if (pos == 60) {
clearInterval(animateId);
eBox.style.backgroundColor=elem.style.backgroundColor;
elem.style.backgroundColor="transparent";
elem.style.top="0px";
elem.style.left="0px";
eNum=boxNum;
eBox=downboxes[eNum];
pos=0;
topVal=0;
leftVal=0;
if(isGameWon())
{
for (let i = 0; i < 36; i++)
{
downboxes[i].onclick=function(){
return false;
}
}
downSelect.style.opacity = "0.1";
showModal1();
playAudio();
//console.log(score);
var score = 3000 -(500-c) -ctr;
var hc = parseInt(localStorage.getItem("highscore1"));
if (score > hc)
{
localStorage.setItem("highscore1",score);
}
else
{
localStorage.setItem("highscore1",hc);
}
high1 = localStorage.getItem("highscore1");
document.querySelector('#highscore').innerHTML = `${high1}`;
clearInterval(update);
}
}
else
{
pos+=10;
mover();
}
}
function setMover(x1,y1,x2,y2)
{
if(x1==x2)
{
if(y1+1==y2)
{
mover=moveLeft;
}
else if(y1-1==y2)
{
mover=moveRight;
}
else
{
return false;
}
}
else if(y1==y2)
{
if(x1+1==x2)
{
mover=moveTop;
}
else if(x1-1==x2)
{
mover=moveBottom;
}
else
{
return false;
}
}
else
{
return false;
}
return true;
}
let centre3x3=[7,8,9,10,13,14,15,16,19,20,21,22,25,26,27,28];
function isGameWon()
{
for (let i = 0; i < 16; i++) {
let rhs=downboxes[centre3x3[i]].style.backgroundColor, lhs=upColors[i];
//console.log(rhs,lhs);
if(rhs!=lhs)
return false;
}
console.log(ctr);
return true;
}
let c = 500;
let timer = document.querySelector('.time');
function timing(){
c = c-1;
if(c<500)
{
timer.innerHTML = c;
}
if (c<1)
{
timer.innerHTML = 0;
clearInterval(update);
downSelect.style.opacity = "0.1";
showModal();
for (let i = 0; i < 36; i++)
{
downboxes[i].onclick=function(){
return false;
}
}
}
}
const strBtn = document.querySelector('.newGame');
strBtn.addEventListener('click',function(){
update = setInterval('timing()' ,1000)
})
function showModal(){
document.getElementById('message').innerHTML = '      You lost' + '<br>' + '      Try again';
document.getElementById('modal').classList.remove("hide");
}
function showModal1(){
score = 3000 -(500-c) -ctr;
document.getElementById('message').innerHTML = '       You Won!' +'<br>' + `Your score : ${score}` ; //your Score = ${ctr}`;
document.getElementById('modal').classList.remove("hide");
}
const hideModal = () => {
document.getElementById('modal').classList.add("hide");
}
var x = document.getElementById("myAudio");
function playAudio() {
x.play();
}