-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathblackjackhelpers.ts
84 lines (73 loc) · 1.96 KB
/
blackjackhelpers.ts
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
type Card = { suit: string; name: string; value: number };
export var shuffle = function(a: Array<Card>): Array<Card> {
var j, x, i;
for (i = a.length - 1; i > 0; i--) {
j = Math.floor(Math.random() * (i + 1));
x = a[i];
a[i] = a[j];
a[j] = x;
}
return a;
};
var getNameFromValue = function(num: number): string {
//input 1 , output "Ace" , input 2, output "two", etc ...
if (num >= 2 && num <= 10) {
return String(num);
} else if (num === 1) {
return "Ace";
} else if (num === 11) {
return "Jack";
} else if (num === 12) {
return "Queen";
} else {
return "King";
}
};
var getSuit = function(num: number): string {
var suitDescriptor = ["spades", "clubs", "hearts", "diamonds"];
return suitDescriptor[num];
};
export var getDeck = function(): Array<Card> {
var deck = [];
for (var y = 0; y < 4; y++) {
for (var x = 0; x < 13; x++) {
var cardOfSpades = {
suit: getSuit(y),
name: getNameFromValue(x + 1),
value: x + 1
};
deck.push(cardOfSpades);
}
}
return deck;
};
export var drawCard = function(currentDeck: Array<Card>): Card {
var currentCard = currentDeck.pop();
if (currentCard === undefined) {
throw new Error("Deck is Empty.");
}
return currentCard;
};
export var getSum = function(cards: Array<Card>): number {
var sum = 0;
var numAcesSeen = 0;
for (var i = 0; i < cards.length; i++) {
if (cards[i].name === "Jack" || cards[i].name === "Queen" || cards[i].name === "King"){
cards[i].value = 10;
} else if (cards[i].name === "Ace"){
numAcesSeen += 1;
continue;
}
sum += cards[i].value;
}
for (var i = 0; i < cards.length; i++) {
if (cards[i].name === "Ace" && sum + (numAcesSeen - 1) < 11){
cards[i].value = 11;
sum += cards[i].value;
} else if (cards[i].name === "Ace"){
cards[i].value = 1;
sum += cards[i].value;
}
}
return sum;
};