-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
169 lines (146 loc) · 4 KB
/
script.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
var ccnt=0;
var gamerunning=1,mtime=0,prev=0,newcard=0,all=1;
var winsound = new Audio("audio/win.mp3");
var right = new Audio("audio/right.mp3");
var wrong = new Audio("audio/wrong.mp3");
var time=new Date();
$(document).ready(function()
{
if(screen.width<=400)
{
$("#restartbtn").html("<i class='fa fa-repeat' aria-hidden='true'></i>");
}
else{
$("#restartbtn").text("Restart Game");
}
timer();
arr=setCards();
console.log(arr);
var cardimages=document.getElementsByClassName("cardimage");
for (var img = 0; img < cardimages.length ; img++)
{
$(cardimages[img]).attr("src","images/card.png")
}
});
function clicked(e)
{
prev=newcard;
newcard = $(e).attr("id");
if(arr[prev-1]==arr[newcard-1])
{
setTimeout(function(){
$("#"+prev).find("img").attr("src","images/done.png")
$("#"+prev).addClass("animate__flipInY");
},700);
setTimeout(function(){
$("#"+newcard).find("img").attr("src","images/done.png")
$("#"+newcard).addClass("animate__flipInY");
updcomp();
win();
},700);
right.play();
}
else
{
if(prev!=0)
{
if(!(($("#"+prev).find("img").attr("src")=="images/done.png")||($("#"+newcard).find("img").attr("src")=="images/done.png")))
{
setTimeout(function(){
$("#"+prev).find("img").attr("src","images/card.png")
$("#"+prev).addClass("animate__flipInY");
},700);
setTimeout(function(){
$("#"+newcard).find("img").attr("src","images/card.png")
$("#"+newcard).addClass("animate__flipInY");
prev=0;
newcard=0;
},700);
wrong.play();
}
}
}
classname=$(e).find("img").attr("src");
if(classname=="images/card.png")
{
$(e).addClass("animate__flipInY");
setTimeout(function(){
$(e).removeClass("animate__flipInY");
}, 500);
var id=$(e).attr("id");
$(e).find("img").attr("src","images/"+arr[id-1]+".png")
ccnt=ccnt+1;
$("#turns").text(('0'+ccnt).slice(-2));
}
}
function timer()
{
setInterval(function(){
if(gamerunning==1)
{
var nowtime = new Date();
timerem=nowtime-time;
timerem=Math.round(timerem/1000);
mtime=Math.floor(timerem/60);
timerem=timerem%60;
$("#min").text(('0' + mtime).slice(-2));
$("#sec").text(('0' + timerem).slice(-2));
}
},1000)
}
function win()
{
all=1;
var cardimages=document.getElementsByClassName("cardimage");
for (var img = 0; img < cardimages.length ; img++)
{
if($(cardimages[img]).attr("src")!="images/done.png")
{
all=0;
break;
}
}
if(all===1)
{
gamerunning=0;
winsound.play();
$(".wincupdiv").css("display","block");
confetti.start(3000);
}
}
function setCards()
{
var arr=[];
var i=0,icount=0;
while(arr.length<=11)
{
i=Math.floor(Math.random() * 6)+1;
icount=0;
for (var c = 0;c<arr.length;c++)
{
if(arr[c]==i)
icount=icount+1;
}
if(icount<2)
arr.push(i)
}
return arr;
}
function reload()
{
location.reload();
}
function updcomp()
{
var comp=0;
var cardimages=document.getElementsByClassName("cardimage");
for (var img = 0; img < cardimages.length ; img++)
{
if($(cardimages[img]).attr("src")=="images/done.png")
{
comp=comp+1;
}
}
var per=Math.ceil((comp*100)/12);
$("#comp").text(per+"%");
}