-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
114 lines (94 loc) · 3.01 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
'use strict';
(function() {
var time_ticks;
var seconds;
var point_calculator;
var opened_cards;
var points = document.getElementsByClassName('points')[0];
var timer = document.getElementsByClassName('timer')[0];
var stop = document.getElementsByClassName('finish')[0];
function play_cards() {
var card = document.getElementsByClassName('card');
var all_cards = ["url('images/5.png')", "url('images/6.png')", "url('images/7.png')", "url('images/8.png')", "url('images/9.png')", "url('images/10.png')", "url('images/5.png')", "url('images/6.png')", "url('images/7.png')", "url('images/8.png')", "url('images/9.png')", "url('images/10.png')"];
seconds = 29;
point_calculator = 0;
opened_cards = [];
stop.style.display = 'none';
shuffle(all_cards);
for (var i = 0; i < card.length; i++) {
if(card[i].classList.contains('flipped')) {
card[i].classList.toggle('flipped');
}
card[i].querySelector('.close').style.backgroundImage = all_cards[i];
card[i].addEventListener('click', flip);
}
points.innerText = '00';
startTimer();
}
function flip() {
if (!this.classList.contains('flipped') && opened_cards.length < 2) {
this.classList.toggle('flipped');
opened_cards.push(this);
if (opened_cards.length === 2) {
checkMatch();
}
}
}
function checkMatch() {
if (opened_cards[0].querySelector('.close').style.backgroundImage === opened_cards[1].querySelector('.close').style.backgroundImage) {
opened_cards = [];
points.innerText = '0' + ++point_calculator;
}
else {
setTimeout(flipBack, 1500);
}
}
function flipBack() {
opened_cards[0].classList.toggle('flipped');
opened_cards[1].classList.toggle('flipped');
opened_cards = [];
}
function startTimer() {
timer.innerText = '0:30';
time_ticks = setInterval(decrementTime, 1000);
}
function decrementTime() {
if (seconds === 0) {
timer.innerText = '0:0' + seconds;
clearInterval(time_ticks);
complete();
}
if (seconds < 10) {
timer.innerText = '0:0' + seconds;
}
if (seconds >= 10) {
timer.innerText = '0:' + seconds;
}
if (point_calculator === 8){
clearInterval(time_ticks);
complete();
}
seconds--;
}
function complete() {
var restart = document.getElementsByTagName('button')[0];
restart.addEventListener('click', play_cards);
stop.style.display = 'flex';
if (point_calculator === 6) {
stop.querySelector('h1').innerText = 'you win';
}
else {
stop.querySelector('h1').innerText = 'Good luck next time';
}
stop.querySelector('.final-score').innerText = 'score: ' + point_calculator;
stop.querySelector('.time').innerText = 'time left: ' + seconds + ' sec.';
}
function shuffle(pack) {
for (var i = pack.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
pack[j] = [pack[i], pack[i] = pack[j]][0];
}
return pack;
}
play_cards();
})();