-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDeck.js
51 lines (43 loc) · 1.48 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
class Deck {
constructor() {
this.cards = [
'🂡', '🂱', '🃑', '🃁',
'🂢', '🂲', '🃒', '🃂',
'🂣', '🂳', '🃓', '🃃',
'🂤', '🂴', '🃔', '🃄',
'🂥', '🂵', '🃕', '🃅',
'🂦', '🂶', '🃖', '🃆',
'🂧', '🂷', '🃗', '🃇',
'🂨', '🂸', '🃘', '🃈',
'🂩', '🂹', '🃙', '🃉',
'🂪', '🂺', '🃚', '🃊',
'🂫', '🂻', '🃛', '🃋',
'🂭', '🂽', '🃝', '🃍',
'🂮', '🂾', '🃞', '🃎',
];
}
shuffle() {
let temp;
// Fisher–Yates shuffle https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle
for (var i = this.cards.length - 1; i > 0; i--) {
const card2Index = Math.floor(Math.random() * (i+1));
temp = this.cards[card2Index];
this.cards[card2Index] = this.cards[i];
this.cards[i] = temp;
}
}
drawOne() {
const card = this.cards.pop();
if (card) {
// Card unicode values are in format U+1F0A1 with the last two hex values representing the suit and the rank
const hex = card.codePointAt(0).toString(16).split('');
// Values 1 to 14 representing ace through king
// Value 12 is for "knight" which is not used in our deck
const rank = parseInt(hex.pop(), 16);
// 'a', 'b', 'c', 'd' representing spades, hearts, diamonds and clubs respectively
const suit = hex.pop();
return { card, suit, rank };
}
}
}
export default Deck;