-
Notifications
You must be signed in to change notification settings - Fork 0
/
CardDeck.js
83 lines (72 loc) · 1.55 KB
/
CardDeck.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
class CardDeck{
constructor(){
this.cards = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A'];
this.families = {
spade: {
icon: '♠',
name: 'spade',
color: 'black'
},
heart: {
icon: '♥',
name: 'heart',
color: 'red'
},
club: {
icon: '♣',
name: 'club',
color: 'black'
},
diamond: {
icon: '♦',
name: 'diamond',
color: 'red'
}
};
this.deck = [];
this.generate();
}
generate(){
if(this.deck.length != 0){
this.deck = [];
}
for(let f in this.families){
let fData = this.families[f];
for(let c in this.cards){
let card = new Card(fData.name, this.cards[c], fData.color, fData.icon);
this.deck.push( card );
}
}
}
shuffle(){
for(let i = this.deck.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[this.deck[i], this.deck[j]] = [this.deck[j], this.deck[i]];
}
console.log(this.deck);
}
pickACard(randomCard = false){
if(!randomCard){
return this.deck.shift();
}
else{
return this.deck.splice(Math.floor(Math.random()*this.deck.length), 1);
}
}
pickCards(count = 1, randomCard = false){
let cards = [];
for(let i = 0; i < count; i++){
cards.push(this.pickACard(randomCard));
}
return cards;
}
}
class Card{
constructor(family, value, color, icon){
this.family = family;
this.value = value;
this.color = color;
this.icon = icon;
this.uid = this.value + this.family;
}
}