-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
255 lines (233 loc) · 8.79 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
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
const resetButton = document.querySelector('.reset');
const timer = document.querySelector('.time-counter');
const cards = document.querySelectorAll('.card');
const cardsNumber = cards.length;
const cardsContainer = document.querySelector('#cards-container');
let hitNumber = 0;
const movements = document.querySelector('.move-counter');
let movementNumber = 0;
const hearts = document.querySelector('#heart-container');
let winnerNumber = 0;
let heartCounter = 3;
// function to calculate the time of game
window.addEventListener('load', function () {
let seconds = 0;
let minutes = 0;
setInterval(function () {
seconds += 1;
if (seconds < 10) {
secondsUnderTen = `0${seconds}`;
timer.textContent = `${minutes} : ${secondsUnderTen}`;
}
if (seconds >= 10) {
timer.textContent = `${minutes} : ${seconds}`;
}
if (seconds == 60) {
seconds = 0;
minutes += 1;
timer.textContent = `${minutes} : ${seconds}`;
}
}, 1000);
});
//function to make array of number of cards that reflect to number of images
function numberOfImages() {
let imagesArray = [];
let imageNumber = 0;
for (i = 0; i < cardsNumber; i++) {
imageNumber += 1;
imagesArray.push(imageNumber);
}
return (imagesArray);
}
const imagesArray = numberOfImages();
//function to shuffle the value of numberOfImages array
var shuffle = function (array) {
let currentIndex = array.length;
let temporaryValue, randomIndex;
// While there remain elements to shuffle...
while (0 !== currentIndex) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
// And swap it with the current element.
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}
const finalToSpread = shuffle(numberOfImages());
//function to add vumber for every child
function giveChildNumber() {
for (const card of imagesArray) {
cards[card - 1].setAttribute(`child`, card);
}
}
giveChildNumber();
//function to target cards and flip it side
let firstCard;
let secondCard;
let firstCardImage;
let secondCardImage;
let flipcard = function (e) {
const cardElement = e.target;
const card = cardElement.tagName;
const childNumber = cardElement.getAttribute('child');
if (hitNumber == 0) {
const flippingResult = flipping(cardElement, card, childNumber);
if (flippingResult === false) {
return;
} else {
firstCard = flippingResult[0];
firstCardImage = flippingResult[1];
}
} else if (hitNumber == 1) {
cardsContainer.removeEventListener("click", flipcard);
if (childNumber !== firstCard) {
const flippingResult = flipping(cardElement, card, childNumber);
if (flippingResult === false) {
cardsContainer.addEventListener("click", flipcard);
return;
} else {
secondCard = flippingResult[0];
secondCardImage = flippingResult[1];
}
let checkMatching = check(firstCardImage, secondCardImage);
movementNumber += 1;
movements.textContent = movementNumber;
heartsNumber(movementNumber);
setTimeout(function () {
if (checkMatching) {
match(firstCard, secondCard);
} else {
notMatch(firstCard, secondCard);
}
hitNumber = 0;
checkWinner(winnerNumber);
cardsContainer.addEventListener("click", flipcard);
}, 1000);
} else {
hitNumber = 1;
cardsContainer.addEventListener("click", flipcard);
}
}
}
//function to flip it side
let flipping = function (cardElement, card, childNumber) {
if (card == 'LI') {
hitNumber += 1;
const childNumberImage = finalToSpread[childNumber - 1];
const existCard = cardElement.classList.contains(`open`);
if (existCard) {
hitNumber -= 1;
return false;
} else {
cards[childNumber - 1].classList.remove('closed');
cards[childNumber - 1].classList.add('open', 'animated', 'flipInY');
cards[childNumber - 1].style.cssText = `background:url('images/icon-${childNumberImage}.png') no-repeat center;background-size: contain;`;
}
return [childNumber, childNumberImage];
} else {
return false;
}
}
cardsContainer.addEventListener("click", flipcard);
//function to check if two flipping cards are matching or not
function check(firstCardImage, secondCardImage) {
const numberCheck = firstCardImage / 2;
function checkNumber(numberCheck) {
if (numberCheck % 1 === 0) {
return (0);
} else if (numberCheck % 1 !== 0) {
return (1);
}
}
let checkNumberResult = checkNumber(numberCheck);
if (checkNumberResult === 0) {
const matchCase = firstCardImage - 1;
if (secondCardImage === matchCase) {
return true;
} else if (secondCardImage !== matchCase) {
return false;
}
} else if (checkNumberResult === 1) {
const matchCase = firstCardImage + 1;
if (secondCardImage === matchCase) {
return true;
} else if (secondCardImage !== matchCase) {
return false;
}
}
}
//Matching function
const match = function (firstCard, secondCard) {
cards[firstCard - 1].classList.remove('animated', 'flipInY');
cards[secondCard - 1].classList.remove('animated', 'flipInY');
cards[firstCard - 1].classList.add('animated', 'pulse');
cards[secondCard - 1].classList.add('animated', 'pulse');
winnerNumber += 1;
document.getElementById('match').play();
setTimeout(function () {
cards[firstCard - 1].classList.remove('animated', 'pulse');
cards[secondCard - 1].classList.remove('animated', 'pulse');
}, 1000);
}
//Not Matching function
const notMatch = function (firstCard, secondCard) {
cards[firstCard - 1].classList.remove('animated', 'flipInY');
cards[secondCard - 1].classList.remove('animated', 'flipInY');
cards[firstCard - 1].classList.add('animated', 'shake');
cards[secondCard - 1].classList.add('animated', 'shake');
cards[firstCard - 1].style.cssText = `background: #f5a435;`;
cards[secondCard - 1].style.cssText = `background: #f5a435;`;
cards[firstCard - 1].classList.remove('open');
cards[secondCard - 1].classList.remove('open');
cards[firstCard - 1].classList.add('closed');
cards[secondCard - 1].classList.add('closed');
document.getElementById('not-match').play();
setTimeout(function () {
cards[firstCard - 1].classList.remove('animated', 'shake');
cards[secondCard - 1].classList.remove('animated', 'shake');
}, 1000);
}
//function to count and determine how many heart will the user has
function heartsNumber(movementNumber) {
if (movementNumber == 16) {
heartCounter = 2;
hearts.removeChild(hearts.firstElementChild);
} else if (movementNumber == 24) {
heartCounter = 1;
hearts.removeChild(hearts.firstElementChild);
}
}
//check when user win and action for it
function checkWinner(winnerNumber) {
if (winnerNumber == 8) {
const gameScreen = document.querySelector('.game-screen');
const winnerScreen = document.querySelector('.winner-screen');
gameScreen.classList.add('disappear');
winnerScreen.classList.remove('disappear');
winnerScreen.classList.add('animated', 'fadeOut');
document.querySelector('.winner-first-block').classList.add('disappear');
document.querySelector('.play-again').classList.add('disappear');
document.querySelector('.winner-movements').textContent = movementNumber;
document.querySelector('.winner-hearts').textContent = heartCounter;
document.querySelector('.winner-time').textContent = timer.textContent;
document.getElementById('winner').play();
setTimeout(function () {
winnerScreen.classList.remove('animated', 'fadeOut');
document.querySelector('.winner-first-block').classList.remove('disappear');
document.querySelector('.play-again').classList.remove('disappear');
const rePlayButton = document.querySelector('.play-again button');
rePlayButton.addEventListener('click', function () {
document.location.reload();
});
}, 1000);
} else {
return;
}
}
//function to reset the game
resetButton.addEventListener('click', function () {
document.location.reload();
})