forked from KD-Games/Project1-Memory-Game
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
139 lines (100 loc) · 2.9 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
const cards = document.querySelectorAll('.memory-card');
let lockBoard = false;
let hasFlippedCard = false;
let firstCard, secondCard;
let score = 10;
let scoreField = document.getElementById('score');
let scoreFieldPopup = document.getElementById('score-pop');
scoreField.innerText = score;
scoreFieldPopup.innerText = score;
let modal = document.getElementById("popup1");
let gameOverPop = document.getElementById("popup2");
let closeicon = document.querySelector(".close");
let matchedCard = document.getElementsByClassName("match");
// //Initial flip so the player can view all the cards and memorize
function initialFlip(){
setTimeout(loadFlip,800);
}
function loadFlip(){
cards.forEach(card => {
card.classList.add('flip');
card.classList.add('avoid-clicks');
});
}
// //Player has 8 seconds to view the cards,
// //below we're making all the cards face back again
function flipBack(){
setTimeout(loadAnotherFlip,8000);
}
function loadAnotherFlip(){
cards.forEach(card => {
card.classList.remove('flip');
card.classList.remove('avoid-clicks');
});
}
////////////////////////////////////////////////////////////
function flipCard(){
if(lockBoard) return;
if (this === firstCard) return;
this.classList.add('flip');
if (!hasFlippedCard){
hasFlippedCard = true;
firstCard = this;
return;
}
hasFlippedCard = false;
secondCard = this;
checkForMatch();
}
function checkForMatch(){
let isMatch = firstCard.dataset.framework === secondCard.dataset.framework;
isMatch ? disableCards() : unflipCards();
}
function disableCards(){
firstCard.removeEventListener('click', flipCard);
secondCard.removeEventListener('click', flipCard);
firstCard.classList.add('x');
secondCard.classList.add('x');
firstCard.classList.add('match');
secondCard.classList.add('match');
resetBoard();
}
function unflipCards(){
lockBoard = true;
setTimeout(() => {
firstCard.classList.remove('flip');
secondCard.classList.remove('flip');
resetBoard();
}, 1500);
score -= 5;
scoreField.innerText = score;
scoreFieldPopup.innerText = score;
}
function resetBoard(){
[hasFlippedCard, lockBoard] = [false, false];
[firstCard, secondCard] = [null, null];
}
(function shuffle(){
cards.forEach(card =>{
let randomPos = Math.floor(Math.random() * 6);
card.style.order = randomPos;
});
})();
function congratulations(){
if (matchedCard.length == cards.length){
//show congratulations modal
modal.classList.add("show");
};
}
initialFlip();
flipBack();
function gameOver(){
if(score<=0){
gameOverPop.classList.add("show");
};
}
cards.forEach(card => {
card.addEventListener('click', flipCard);
card.addEventListener("click", congratulations);
card.addEventListener("click",gameOver);
});