-
Notifications
You must be signed in to change notification settings - Fork 0
/
as.js
211 lines (181 loc) · 4.39 KB
/
as.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
//object formation
var element=
[
{name:'♡♡♡'},
{name:'♡♡♡'},
{name:'♤♤♤'},
{name:'☆☆☆'},
{name:'◇◇◇'},
{name:'◇◇◇'},
{name:'♤♤♤'},
{name:'♤♤♤'},
{name:'♤♤♤'},
{name:'☆☆☆'},
{name:'☆☆☆'},
{name:'☆☆☆'},
{name:'♤♤♤'},
{name:'♤♤♤'},
{name:'☆☆☆'},
{name:'☆☆☆'},
{name:'♡♡♡'},
{name:'♡♡♡'}
];
//for random arrangement of array
element.sort(() => 0.5 - Math.random());
//declaration and initilisation
let eledisplay=document.querySelector(".box");
let cardchose =[];
let cardchoseid = [];
var n=5;
var s=0;
var stop;
var t=0;
var won= element.length /2;
let current;
// const hi= 100/(18/3);
//board formation with block
function board()
{
for(let i=0;i<element.length;i++)
{
//var r= Math.floor(Math.random() * 10)
const card=
document.createElement("div");
card.setAttribute("data-id",i);
card.innerHTML=element[i].name;
// cardchose.style.display="none";
card.addEventListener("click",fun);
eledisplay.appendChild(card);
}
}board();
//main algo
function fun()
{
const cardid= this.innerHTML ;
const id=this.getAttribute("data-id");
//change
var auudio=document.getElementById("clicksound");
auudio.play();
cardchose.push(cardid);
cardchoseid.push(id);
// alert(id);
// cardchose.style.display="block";
this.style.background ="white";
if(cardchose.length === 2)
{
setTimeout(checkmatch,400);
}
}
//match checking algo
function checkmatch()
{
current= document.querySelectorAll(".box div");
//for same block choose
if(cardchoseid[0]=== cardchoseid[1])
{
// n=n-2
current[cardchoseid[0]].style.background ="black";
current[cardchoseid[1]].style.background ="black";
// document.querySelector("#score").innerHTML=n;
}
//for matching
else if(cardchose[0] === cardchose[1])
{
n=n+20;
current[cardchoseid[0]].style.visibility ="hidden ";
current[cardchoseid[1]].style.visibility ="hidden";
// alert("dudhd");
var right=document.getElementById("right");
right.play();
document.querySelector("#score").innerHTML=n; current[cardchoseid[0]].removeEventListener("click",fun);
current[cardchoseid[1]].removeEventListener("click",fun);
s++;
}
//for mismatch
else
{
n=n-5;
current[cardchoseid[0]].style.background="black";
current[cardchoseid[1]].style.background="black";
var wrongg=document.getElementById("wrong");
wrongg.play();
document.querySelector("#score").innerHTML=n;
}
cardchoseid =[];
cardchose =[];
//for win completed
if(s===won) {
document.querySelector("#re").innerHTML="Congratulation bro! <br> ";
var audioo=document.getElementById("winsound");
audioo.play();
document.querySelector("#se").innerHTML= "you completeted in "+t +"sec and your score is " +n;
var ele= document.querySelector(".circle");
ele.onclick="" ;
document.querySelector(".circle").innerHTML =" ";
}
}
//timer
stop=setInterval(time,1010);
function time()
{
document.querySelector("#timer").innerHTML =t;
t++;
}
//lifeline
function help()
{
n=n-15;
document.querySelector("#score").innerHTML=n;
var audio=document.getElementById("helpsound");
audio.play();
setTimeout(ggg,200);
}
function ggg()
{
cardchose =[];
const curren= document.querySelectorAll(".box div");
for(let j=0; j< element.length;j++)
{
curren[j].style.background="white";
}
//document.querySelector(".circle").innerHTML =" ";
setTimeout(gggg,2000);
}
function gggg()
{
const curren= document.querySelectorAll(".box div");
for(let j=0; j< element.length;j++)
{
curren[j].style.background="black";
}
}
function restart()
{
const curre= document.querySelectorAll(".box div");
if(confirm("do you want to restart "))
{
s=0;
t=0;
n=0;
element.sort(() => 0.5 - Math.random());
eledisplay.innerHTML="";
board();
for(let k=0;k<element.length;k++)
{
curre[k].style.visibility ="visible ";
curre[k].style.background="black";
curre[k].addEventListener("click",fun);
}
document.querySelector("#score").innerHTML=n;
document.querySelector("#re").innerHTML=" ";
document.querySelector("#se").innerHTML= "";
var ele= document.querySelector(".circle");
ele.onclick=help ;
document.querySelector(".circle").innerHTML ="♥ ";
element.sort(() => 0.4 - Math.random());
}
}
function goback()
{
window.history.back();
}