-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
81 lines (69 loc) · 2.33 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
let ImagesArray = ['Game/1.jpg','Game/2.jpg','Game/3.jpg','Game/4.jpg','Game/5.jpg','Game/6.jpg','Game/7.jpg','Game/8.jpg','Game/1.jpg','Game/2.jpg','Game/3.jpg','Game/4.jpg','Game/5.jpg','Game/6.jpg','Game/7.jpg','Game/8.jpg'];
let started = false;
let firstIndex = -1, secondIndex = -1, matches = 0;
function hideAll(){
for (let i = 0; i < ImagesArray.length; i++)
{
document.images[i].src = "Game/0.jpg";
}
}
let btn = document.getElementById('btnStart');
btn.onclick = function(){
//GAME Start
started = true;
//Show Images from Array
for (let i = 0; i < ImagesArray.length; i++) {
document.images[i].src = ImagesArray[i];
}
//Hide Images After 3 Seconds
setTimeout('hideAll();',3000)
}
let startDate = new Date();
function hideTwo()
{
document.images[firstIndex].src = "Game/0.jpg";
document.images[secondIndex].src = "Game/0.jpg";
firstIndex = secondIndex = -1;
}
//FLIP Image on Click Event
function flip(index)
{ if(started)
{ if(document.images[index].src.indexOf ("Game/0.jpg") != -1)
{
//CHANGE Picture
if (firstIndex == -1) //First Click
{
document.images[index].src = ImagesArray[index];
firstIndex = index;
}
else
{ //Second Click
document.images[index].src = ImagesArray[index];
secondIndex = index;
//COMPARE
if(document.images[firstIndex].src == document.images[secondIndex].src)
{ //Equal
firstIndex = secondIndex = -1;
matches++;
//Game Ended
if(matches == 8)
{
endDate = new Date();
let diff = endDate - startDate;
alert('Congratulations! You finished it in' + diff/1000 + 'Seconds');
}
}
else
{ //HIDE 2 Images
setTimeout('hideTwo();', 500);
}
}
}else{
alert('You clicked This Image Before!');
}
}
else
{
alert('Please Press Start to Start (;-_-)');
}
}