-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Expand file tree
/
Copy pathmemory.js
More file actions
39 lines (34 loc) · 801 Bytes
/
memory.js
File metadata and controls
39 lines (34 loc) · 801 Bytes
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
class MemoryGame {
constructor(cards) {
this.cards = cards || [];
this.pickedCards = [];
this.pairsClicked = 0;
this.pairsGuessed = 0;
}
shuffleCards() {
if (!this.cards) {
return undefined;
}
for (let i = this.cards.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[this.cards[i], this.cards[j]] = [this.cards[j], this.cards[i]];
}
}
addPickedCard(cardName) {
this.pickedCards.push(cardName);
}
clearPickedCards() {
this.pickedCards = [];
}
checkIfPair(card1Name, card2Name) {
this.pairsClicked++;
if (card1Name === card2Name) {
this.pairsGuessed++;
return true;
}
return false;
}
checkIfFinished() {
return this.pairsGuessed === this.cards.length / 2;
}
}