-
Notifications
You must be signed in to change notification settings - Fork 0
/
blackjack.go
63 lines (63 loc) · 1.54 KB
/
blackjack.go
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
package blackjack
type set map[string]struct{}
var contains = struct{}{}
// ParseCard returns the integer value of a card following blackjack ruleset.
func ParseCard(card string) int {
ruleset := map[string]int{
"ace" :11,
"two":2,
"three":3,
"four":4,
"five":5,
"six":6,
"seven":7,
"eight":8,
"nine":9,
"ten":10,
"jack":10,
"queen":10,
"king":10,
"other":0,
}
return ruleset[card]
}
func fillStandCardSet() set {
dealerCardsList := []string{"ten","jack","queen","king","ace"}
dealerCards := make(set,len(dealerCardsList))
for _,v := range dealerCardsList {
dealerCards[v] = contains
}
return dealerCards
}
func stand(dealerCard string,standCardSet set) bool {
_,contains := standCardSet[dealerCard]
return contains
}
// FirstTurn returns the decision for the first turn, given two cards of the
// player and one card of the dealer.
func FirstTurn(card1, card2, dealerCard string) string {
standCardSet := fillStandCardSet()
sum := ParseCard(card1)+ParseCard(card2)
switch {
case card1=="ace" && card2=="ace":
return "P"
case sum==21:
if stand(dealerCard,standCardSet) {
return "S"
} else {
return "W"
}
case sum >=17 && sum <=20:
return "S"
case sum >= 12 && sum <=16:
if ParseCard(dealerCard) >= 7 {
return "H"
} else {
return "S"
}
case sum <= 11:
return "H"
default:
return "H"
}
}