-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpoker-utils.js
105 lines (88 loc) · 2.23 KB
/
poker-utils.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
"use strict";
const shuffle = require('./shuffle');
const cards = [
'As', '2s', '3s', '4s', '5s', '6s', '7s', '8s', '9s', 'Ts', 'Js', 'Qs', 'Ks',
'Ad', '2d', '3d', '4d', '5d', '6d', '7d', '8d', '9d', 'Td', 'Jd', 'Qd', 'Kd',
'Ac', '2c', '3c', '4c', '5c', '6c', '7c', '8c', '9c', 'Tc', 'Jc', 'Qc', 'Kc',
'Ah', '2h', '3h', '4h', '5h', '6h', '7h', '8h', '9h', 'Th', 'Jh', 'Qh', 'Kh',
];
function newDeck() {
return shuffle(cards.slice());
}
function drawCards(numCards, deck) {
const draw = [];
for (let i = 0; i < numCards; i++) {
draw.push(deck.shift());
}
return {
deck: deck,
draw: draw,
};
}
function existInHand(card, hand) {
if (hand.indexOf(card) > -1) {
return true;
}
return false;
}
function convertCardToEmoji(card) {
let rank = card.slice(0, 1);
let suit = card.slice(1, 2);
if (rank === 'T') {
rank = '10';
}
if (suit === 's') {
suit = '♠️';
} else if (suit === 'd') {
suit = '♦️';
} else if (suit === 'c') {
suit = '♣️';
} else if (suit === 'h') {
suit = '♥️';
}
return `${rank}${suit}`;
}
function convertHandToEmoji(hand) {
return hand.map(convertCardToEmoji).join('');
}
function convertCardToSpeech(card) {
let rank = card.slice(0, 1);
let suit = card.slice(1, 2);
if (rank === 'A') {
rank = 'Ace';
}
if (rank === 'K') {
rank = 'King';
}
if (rank === 'Q') {
rank = 'Queen';
}
if (rank === 'J') {
rank = 'Jack';
}
if (rank === 'T') {
rank = '10';
}
if (suit === 's') {
suit = 'spades'
} else if (suit === 'd') {
suit = 'diamonds'
} else if (suit === 'c') {
suit = 'clubs'
} else if (suit === 'h') {
suit = 'hearts'
}
return `${rank} of ${suit}`;
}
function convertHandToSpeech(hand) {
return hand.map(convertCardtoSpeech).join(', ');
}
module.exports = {
newDeck: newDeck,
drawCards: drawCards,
existInHand: existInHand,
convertCardToEmoji: convertCardToEmoji,
convertHandToEmoji: convertHandToEmoji,
convertCardToSpeech: convertCardToSpeech,
convertHandToSpeech: convertHandToSpeech,
};