-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
182 lines (156 loc) Β· 3.53 KB
/
main.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
const grid = document.querySelector('.js-grid');
// array of html elements representing cards
let cards = null;
let isProcessingMatch = false;
/**
* Card component
*
* @return {string} Return the string representaton of the HTML Card component
*/
let Card = ({ value }) => {
return `
<div class="card" data-value="${value}">
<section class="card_front"></section>
<section class="card_back">${value}</section>
</div>
`;
};
let state = {
// will hold maximum two cards [card1, card2]
selectedCards: [],
clearSelectedCards() {
this.selectedCards = [];
},
// state for matched count
matchedCount: 0,
maximumMatchedCount: 18,
incrementMatchCount() {
this.matchedCount += 1;
},
allMatched() {
return this.matchedCount === this.maximumMatchedCount;
},
restart() {
this.matchedCount = 0;
}
};
let uniqueMatches = [
'π',
'π»',
'π',
'π',
'πΌ',
'π¨',
'πΌ',
'π',
'πΊ',
'π',
'π',
'π€',
'π',
'π',
'π©',
'π',
'π¨',
'π©'
];
function startGame() {
let html = generateCardsHtml();
grid.innerHTML = html;
cards = grid.querySelectorAll('.card');
cards.forEach(card => {
card.addEventListener('click', handleCardClick);
});
}
/**
* Return string HTML represantation of cards.
*
* @return {string} the string html representation
*/
function generateCardsHtml() {
let mathces = [...uniqueMatches, ...uniqueMatches];
function shuffle(a) {
for (let i = a.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[a[i], a[j]] = [a[j], a[i]];
}
return a;
}
shuffle(mathces);
return mathces.map(value => Card({ value })).join('');
}
startGame();
function handleCardClick(e) {
// return if it is processing the match
if (isProcessingMatch) return;
const cardEl = e.currentTarget;
// return if you clicked this card already
if (cardEl.classList.contains('open')) return;
// open the card
if (state.selectedCards.length < 2) {
cardEl.classList.add('open');
state.selectedCards.push(cardEl);
}
// match the card
if (state.selectedCards.length === 2) {
matchCards();
return;
}
}
function matchCards() {
isProcessingMatch = true;
let [cardA, cardB] = state.selectedCards;
let valueA = cardA.dataset.value;
let valueB = cardB.dataset.value;
if (valueA === valueB) {
// handle a match
setTimeout(() => {
state.incrementMatchCount();
state.selectedCards.forEach(card => {
card.classList.add('matched');
card.removeEventListener('click', handleCardClick);
});
state.clearSelectedCards();
isProcessingMatch = false;
didItEnd();
}, 1000);
} else {
// handle don' match
setTimeout(() => {
state.selectedCards.forEach(card => {
card.classList.remove('open');
});
state.clearSelectedCards();
isProcessingMatch = false;
}, 1000);
}
}
const modalEl = document.querySelector('.js-modal');
const timePlaceholder = modalEl.querySelector('.js-modal-time');
function didItEnd() {
if (state.allMatched()) {
// it did end
// show a modal
modalEl.classList.add('open');
// cound down to 0
// and restart game
let countDown = 6;
let interval = setInterval(() => {
countDown--;
timePlaceholder.textContent = `${countDown}`;
if (countDown === 0) {
// restart the game
modalEl.classList.remove('open');
timePlaceholder.textContent = `...`;
startGame();
clearInterval(interval);
}
}, 1000);
state.restart();
}
}
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('./sw.js').then(() => {
console.log('Service worker is registered');
});
}