-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdeck.js
35 lines (32 loc) · 811 Bytes
/
deck.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
class Deck {
constructor(cardObjects) {
this.cards = cardObjects;
this.matchedCards = [];
this.selectedCards = [];
}
shuffle() {
var i = this.cards.length,
j, temp;
while (--i > 0) {
j = Math.floor(Math.random() * (i + 1));
temp = this.cards[j];
this.cards[j] = this.cards[i];
this.cards[i] = temp;
}
return this.cards;
}
checkSelectedCards(clickedCard) {
this.selectedCards.push(clickedCard);
this.moveToMatched();
}
moveToMatched() {
if (this.selectedCards.length >= 2 &&
this.selectedCards[0].dataset.image ===
this.selectedCards[1].dataset.image) {
this.matchedCards.push(this.selectedCards[0],
this.selectedCards[1]);
this.selectedCards.pop();
this.selectedCards.pop();
}
}
}