-
Notifications
You must be signed in to change notification settings - Fork 24
/
card.go
103 lines (81 loc) · 1.81 KB
/
card.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
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
package poker
type Card int32
var (
intRanks [13]int32
strRanks = "23456789TJQKA"
primes = []int32{2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41}
)
var (
charRankToIntRank = map[uint8]int32{}
charSuitToIntSuit = map[uint8]int32{
's': 1, // spades
'h': 2, // hearts
'd': 4, // diamonds
'c': 8, // clubs
}
intSuitToCharSuit = "xshxdxxxc"
)
var (
prettySuits = map[int]string{
1: "\u2660", // spades
2: "\u2764", // hearts
4: "\u2666", // diamonds
8: "\u2663", // clubs
}
prettyReds = [...]int{2, 4}
)
func init() {
for i := 0; i < 13; i++ {
intRanks[i] = int32(i)
}
for i := range strRanks {
charRankToIntRank[strRanks[i]] = intRanks[i]
}
}
func NewCard(s string) Card {
rankInt := charRankToIntRank[s[0]]
suitInt := charSuitToIntSuit[s[1]]
rankPrime := primes[rankInt]
bitRank := int32(1) << uint32(rankInt) << 16
suit := suitInt << 12
rank := rankInt << 8
return Card(bitRank | suit | rank | rankPrime)
}
func (c *Card) MarshalJSON() ([]byte, error) {
return []byte("\"" + c.String() + "\""), nil
}
func (c *Card) UnmarshalJSON(b []byte) error {
*c = NewCard(string(b[1:3]))
return nil
}
func (c Card) String() string {
return string(strRanks[c.Rank()]) + string(intSuitToCharSuit[c.Suit()])
}
func (c Card) Rank() int32 {
return (int32(c) >> 8) & 0xF
}
func (c Card) Suit() int32 {
return (int32(c) >> 12) & 0xF
}
func (c Card) BitRank() int32 {
return (int32(c) >> 16) & 0x1FFF
}
func (c Card) Prime() int32 {
return int32(c) & 0x3F
}
func primeProductFromHand(cards []Card) int32 {
product := int32(1)
for _, card := range cards {
product *= (int32(card) & 0xFF)
}
return product
}
func primeProductFromRankBits(rankBits int32) int32 {
product := int32(1)
for _, i := range intRanks {
if rankBits&(1<<uint(i)) != 0 {
product *= primes[i]
}
}
return product
}