-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
executable file
·125 lines (78 loc) · 2.74 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
var cards1 = ["cat1.jpg", "cat2.jpg", "cat3.jpg", "cat3.jpg", "cat4.jpg", "cat5.jpg", "cat2.jpg", "cat6.jpg", "cat1.jpg", "cat5.jpg", "cat6.jpg", "cat4.jpg"];
var cards2 = ["cat1.jpg", "cat1.jpg", "cat3.jpg", "cat4.jpg", "cat2.jpg", "cat5.jpg", "cat4.jpg", "cat2.jpg", "cat6.jpg", "cat3.jpg", "cat6.jpg", "cat5.jpg"];
var cards3 = ["cat6.jpg", "cat4.jpg", "cat2.jpg", "cat5.jpg", "cat2.jpg", "cat3.jpg", "cat3.jpg", "cat1.jpg", "cat4.jpg", "cat5.jpg", "cat6.jpg", "cat1.jpg"];
var oneVisible = false;
var turnCounter = 0;
var visible_nr;
var lock = false;
var pairsLeft = 6;
function hide2Cards(nr, visible_nr) {
document.querySelector('#c'+nr).setAttribute('style', 'opacity: 0');
document.querySelector('#c'+visible_nr).setAttribute('style', 'opacity: 0');
pairsLeft--;
if(pairsLeft == 0)
{
document.querySelector('.board').innerHTML = '<h1>You win!<br>Done in '+turnCounter+' turns</h1>';
}
lock = false;
}
function restore2Cards(nr1, nr2) {
document.querySelector('#c'+nr1).setAttribute('style', 'background-image:""');
document.querySelector('#c'+nr2).setAttribute('style', 'background-image:""');
document.querySelector('#c'+nr1).classList.add('card');
document.querySelector('#c'+nr1).classList.remove('cardA');
document.querySelector('#c'+nr2).classList.add('card');
document.querySelector('#c'+nr2).classList.remove('cardA');
lock = false;
}
function revealCard(e) {
var opacityValue = e.target.classList.add('opacity');
var nr = e.target.id.substr(1);
if(opacityValue != 0 && lock == false) {
lock = true;
var obraz = "url(img/"+ cards[nr] +")";
document.querySelector('#c'+nr).setAttribute('style', 'background-image:'+obraz);
document.querySelector('#c'+nr).classList.add('cardA');
document.querySelector('#c'+nr).classList.remove('card');
if(oneVisible == false)
{
//first card
oneVisible = true;
visible_nr = nr;
lock = false;
}
else
{
//second card
if(cards[visible_nr] == cards[nr])
{
// console.log("para");
setTimeout(function() { hide2Cards(nr, visible_nr) }, 1000);
}
else
{
// console.log("pudło");
setTimeout(function() { restore2Cards(nr, visible_nr) }, 1000);
}
turnCounter++;
document.querySelector('.score').innerText = 'Counter: ' + turnCounter;
oneVisible = false;
}
}
}
var c = document.querySelectorAll('.card');
var random = Math.floor(Math.random() * 3);
var cards = null;
switch(random) {
case 0:
cards = cards1;break
case 1:
cards = cards2;break;
case 2:
cards = cards3;break;
default:
cards = cards1;
}
for(var i=0; i<12; i++) {
c[i].addEventListener("click", revealCard);
}