-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcard.go
187 lines (153 loc) · 3.54 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package main
import (
"fmt"
"github.com/fatih/color"
)
/*Static class that handles cards. We represent cards as 32-bit integers, so
there is no object instantiation - they are just ints. Most of the bits are
used, and have a specific meaning. See below:
Card:
bitrank suit rank prime
+--------+--------+--------+--------+
|xxxbbbbb|bbbbbbbb|cdhsrrrr|xxpppppp|
+--------+--------+--------+--------+
1) p = prime number of rank (deuce=2,trey=3,four=5,...,ace=41)
2) r = rank of card (deuce=0,trey=1,four=2,five=3,...,ace=12)
3) cdhs = suit of card (bit turned on based on suit of card)
4) b = bit turned on depending on rank of card
5) x = unused
This representation will allow us to do very important things like:
- Make a unique prime prodcut for each hand
- Detect flushes
- Detect straights
and is also quite performant.*/
type csuit rune
type crank rune
type Card int32
type Suit uint16
type Rank uint16
const (
Club Suit = 0x8000
Diamond = 0x4000
Heart = 0x2000
Spade = 0x1000
)
const (
Deuce Rank = iota
Trey
Four
Five
Six
Seven
Eight
Nine
Ten
Jack
Queen
King
Ace
)
const (
strRanks = "23456789TJQKA"
)
var IntRanks = makeRanks(1, 13)
var Primes = [13]int{2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41}
var CharSuitToIntSuit = map[csuit]Suit{
's' : Spade, // spades
'h' : Heart, // hearts
'd' : Diamond, // diamonds
'c' : Club, // clubs
}
var CharRankToIntRank = map[crank]Rank{
'2': Deuce,
'3': Trey,
'4': Four,
'5': Five,
'6': Six,
'7': Seven,
'8': Eight,
'9': Nine,
'T': Ten,
'J': Jack,
'Q': Queen,
'K': King,
'A': Ace,
}
var strSuits = map[Suit]string {
Spade : "s", // spades
Heart : "h", // hearts
Diamond : "d", // diamonds
Club : "c", // clubs
}
//for pretty printing
var PrettySuits = map[Suit]string {
Spade : "\u2660", // spades
Heart : "\u2764", // hearts
Diamond : "\u2666", // diamonds
Club : "\u2663", // clubs
}
//hearts and diamonds
var PrettyReds = [2]Suit{Heart, Diamond}
/*Converts Card string to binary integer representation of card, inspired by:
http://www.suffecool.net/poker/evaluator.html*/
func NewCard(value string) Card {
runes := []rune(value)
rankChar := crank(runes[0])
suitChar := csuit(runes[1])
rank := CharRankToIntRank[rankChar]
suit := CharSuitToIntSuit[suitChar]
bitrank := 1 << uint(16 + rank)
n := Primes[rank] | (int(rank) << 8) | int(suit) | bitrank
return Card(n)
}
func (c Card) String() string {
return string(strRanks[c.Rank()]) + strSuits[c.Suit()]
}
func (c Card) Rank() Rank {
return Rank((c >> 8) & 0x0f)
}
func (c Card) Suit() Suit {
return Suit(c & 0xf000)
}
func (c Card) BitRank() int {
return int(c) >> 16
}
func (c Card) Prime() int {
return int(c) & 0xff
}
//Expects a list of cards in integer form.
/*func primeProductFromHand(cards []Card) (product int) {
product = 1
for _ , c := range(cards) {
product *= (c & 0xFF)
}
return
}
*/
//Prints a single card
func (c Card) PrettyStr() string {
colorEnabled := true
//suit and rank
rank := c.Rank()
suit := c.Suit()
//if we need to color red
s := PrettySuits[suit]
red := color.New(color.FgRed).SprintFunc()
if (colorEnabled && isRedSuit(suit, PrettyReds)) {
s = red(s)
}
r := strRanks[rank]
return " [ " + string(r) + " " + string(s) + " ] "
}
// Expects a list of cards in integer form.
func PrintPrettyCards(cards []Card) string {
output := " "
for index, c := range(cards) {
if index != len(cards) - 1 {
output += c.PrettyStr() + ","
} else {
output += c.PrettyStr() + " "
}
}
return fmt.Sprintf("%s", output)
}