forked from C-Bandstra/beyonce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deck.js
79 lines (73 loc) · 1.99 KB
/
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
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
class Deck {
constructor() {
this.cards = [];
this.matchedCards = [];
this.selectedCards = [];
this.shuffledCards = [];
}
shuffle() {
var min = 0;
var max = 10;
for (let i = 0; i < 10; i++) {
var randomNum = Math.floor(Math.random() * (max - min) + min);
var card = this.cards.splice(randomNum, 1);
this.shuffledCards.push.apply(this.shuffledCards, card);
max--;
}
}
moveToMatched() {
var selectedArr = this.selectedCards;
if (selectedArr[0].matchInfo === selectedArr[1].matchInfo) {
selectedArr.forEach(card => {
card.match();
showCurrentMatchesImg(card);
});
this.matchedCards.push(selectedArr);
removeMatchedDom();
} else {
setTimeout(flipTimeout, 1500)
}
}
populateDeck() {
var firstCardsId = 0;
var lastCardsId = 5;
for (var i = 1; i < 6; i++) {
firstCardsId++;
lastCardsId++;
var card1 = new Card(`card-${i}`, firstCardsId, i);
var card2 = new Card(`card-${i}`, lastCardsId, i);
this.cards.push(card1);
this.cards.push(card2);
}
this.shuffle();
}
checkSelected(event) {
var selectedArr = this.selectedCards;
this.shuffledCards.forEach((card, i) => {
var cardIndex = this.shuffledCards[i];
if (cardIndex.id == event.target.id && selectedArr.length <= 2) {
cardIndex.selected = false;
this.populateSelected(selectedArr, cardIndex);
}
})
}
populateSelected(selectedArr, cardIndex) {
if (!cardIndex.selected && selectedArr.length < 2) {
selectedArr.push(cardIndex);
cardIndex.selected = true;
displayImg(event);
} else {
this.removeSelected(selectedArr);
}
}
removeSelected(selectedArr) {
selectedArr.forEach(card => {
if (card.selected && (card.id == event.target.id)) {
var index = selectedArr.indexOf(card);
card.selected = false;
selectedArr.splice(index, 1);
displayImg(event);
}
})
}
}