-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoker.go
330 lines (273 loc) · 5.95 KB
/
poker.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
package main
import (
// "bytes"
"fmt"
"utf8"
"sort"
"rand"
)
import (
"os"
"log"
"runtime/pprof"
)
type Card string
func NewCard(rank, suit int) Card {
return Card(string([]byte{byte(rank), byte(suit)}))
}
const (
CardNames = "XX23456789TJQKA"
)
var Suits = utf8.NewString("♥♦♠♣")
func (c Card) String() string {
return fmt.Sprintf("%c%c", CardNames[c.Rank()], Suits.At(c.Suit()))
}
type CardSort []Card
func (c CardSort) Len() int { return len(c) }
func (c CardSort) Less(i, j int) bool { return c[i] < c[j] }
func (c CardSort) Swap(i, j int) { c[i], c[j] = c[j], c[i] }
type CardPair string
func NewPair(a, b Card) CardPair {
if a.Rank() < b.Rank() {
a, b = b, a
}
var s byte
if a.Suit() == b.Suit() {s = 'S'} else {s = 'U'}
return CardPair([]byte{a[0], b[0], s})
}
func (c CardPair) String() string {
return fmt.Sprintf("%c%c %c", CardNames[c[0]], CardNames[c[1]], c[2])
}
func (c Card) Rank() int {
return int(c[0])
}
func (c Card) Suit() int {
return int(c[1])
}
type Hand struct {
Type int
Kickers []int
}
const (
HIGH_CARD = iota
PAIR
TWO_PAIR
THREE_OF_A_KIND
STRAIGHT
FLUSH
FULL_HOUSE
FOUR_OF_A_KIND
STRAIGHT_FLUSH
)
const (
JACK = 11
QUEEN = 12
KING = 13
ACE = 14
)
func Deck() (out []Card) {
out = make([]Card, 52)
for r := 2; r < 15; r++ {
for s := 0; s < 4; s++ {
out[s*13 + r - 2] = NewCard(r, s)
}
}
return
}
var SEVEN_COMBINATIONS = [][]int{
[]int{0, 1, 2, 3, 4},
[]int{0, 1, 2, 3, 5},
[]int{0, 1, 2, 3, 6},
[]int{0, 1, 2, 4, 5},
[]int{0, 1, 2, 4, 6},
[]int{0, 1, 2, 5, 6},
[]int{0, 1, 3, 4, 5},
[]int{0, 1, 3, 4, 6},
[]int{0, 1, 3, 5, 6},
[]int{0, 1, 4, 5, 6},
[]int{0, 2, 3, 4, 5},
[]int{0, 2, 3, 4, 6},
[]int{0, 2, 3, 5, 6},
[]int{0, 2, 4, 5, 6},
[]int{0, 3, 4, 5, 6},
[]int{1, 2, 3, 4, 5},
[]int{1, 2, 3, 4, 6},
[]int{1, 2, 3, 5, 6},
[]int{1, 2, 4, 5, 6},
[]int{1, 3, 4, 5, 6},
[]int{2, 3, 4, 5, 6},
}
func BestOfSeven(cards []Card) (best Hand) {
if len(cards) != 7 {
panic("ahhhhh")
}
best = Hand{-1, []int{}}
for _, perm := range SEVEN_COMBINATIONS {
hand := make([]Card, 5)
for i, p := range perm {
hand[i] = cards[p]
}
rating := RateHand(hand)
if CmpHand(rating, best) > 0 {
best = rating
}
}
return
}
func CmpHand(a, b Hand) int {
cmp := a.Type - b.Type
if cmp != 0 { return cmp }
for i := 0; i < len(a.Kickers); i++ {
cmp = a.Kickers[i] - b.Kickers[i]
if cmp != 0 { return cmp }
}
return 0
}
func RateHand(cards []Card) (hand Hand) {
if len(cards) != 5 {
panic("non-5 card hand")
}
ranks := make([]int, 5)
rank_map := make([]int, 15)
suit := cards[0].Suit()
flush := true
for i, c := range cards {
ranks[i] = c.Rank()
rank_map[c.Rank()]++
flush = flush && c.Suit() == suit
}
sort.Sort(sort.IntSlice(ranks))
straight_rank := 0
if ranks[0] == 2 &&
ranks[1] == 3 &&
ranks[2] == 4 &&
ranks[3] == 5 &&
ranks[4] == ACE {
straight_rank = 5
} else if ranks[0] + 1 == ranks[1] &&
ranks[1] + 1 == ranks[2] &&
ranks[2] + 1 == ranks[3] &&
ranks[3] + 1 == ranks[4] {
straight_rank = ranks[4]
}
var pair1, pair2, triple, quadruple int
for rank, count := range rank_map {
switch count {
case 2:
if pair1 == 0 { pair1 = rank
} else { pair2 = rank }
case 3: triple = rank
case 4: quadruple = rank
}
}
if pair1 < pair2 {
pair1, pair2 = pair2, pair1
}
kickers := func(kicker1, kicker2 int) []int {
out := make([]int, 0, 5)
if kicker1 != 0 { out = append(out, kicker1) }
if kicker2 != 0 { out = append(out, kicker2) }
for i := len(ranks) - 1; i >= 0; i-- {
if ranks[i] != kicker1 && ranks[i] != kicker2 {
out = append(out, ranks[i])
}
}
return out
}
switch {
case straight_rank != 0 && flush:
return Hand{ STRAIGHT_FLUSH, []int{straight_rank} }
case quadruple != 0:
return Hand{ FOUR_OF_A_KIND, kickers(quadruple, 0) }
case triple != 0 && pair1 != 0:
return Hand{ FULL_HOUSE, kickers(triple, pair1) }
case flush:
return Hand{ FLUSH, kickers(0, 0) }
case straight_rank != 0:
return Hand{ STRAIGHT, []int{straight_rank} }
case triple != 0:
return Hand{ THREE_OF_A_KIND, kickers(triple, 0) }
case pair2 != 0:
return Hand{ TWO_PAIR, kickers(pair1, pair2) }
case pair1 != 0:
return Hand{ PAIR, kickers(pair1, 0) }
default:
return Hand{ HIGH_CARD, kickers(0, 0) }
}
panic("bad hand")
}
type Stats struct {
win, loss int
}
const PLAYERS = 8
func RunHand(deck []Card, result map[int]map[CardPair]Stats) {
community, deck := deck[:5], deck[5:]
hands := make([][]Card, PLAYERS)
for i, _ := range hands {
hands[i], deck = deck[0:2], deck[2:]
}
ratings := make([]Hand, PLAYERS)
best_idx := 0
for i, hand := range hands {
ratings[i] = BestOfSeven(append(community, hand[0], hand[1]))
if CmpHand(ratings[i], ratings[best_idx]) > 0 {
best_idx = i
}
}
for i, hand := range hands {
pair := NewPair(hand[0], hand[1])
stats := result[pair]
if CmpHand(ratings[i], ratings[best_idx]) >= 0 {
stats.win++
} else {
stats.loss++
}
result[pair] = stats
}
}
const HANDS = 100000
func RunHands() map[CardPair]Stats {
d := Deck()
result := make(map[CardPair]Stats)
for i := 0; i < HANDS; i++ {
d2 := make([]Card, 52)
for i, p := range rand.Perm(52) {
d2[i] = d[p]
}
RunHand(d2, result)
}
return result
}
func CompareHands() {
d := Deck()
d2 := make([]Card, 52)
for i, p := range rand.Perm(52) {
d2[i] = d[p]
}
hand1, hand2 := d2[:7], d2[7:14]
sort.Sort(CardSort(hand1))
sort.Sort(CardSort(hand2))
cmp := CmpHand(BestOfSeven(hand1), BestOfSeven(hand2))
var symbol byte
switch {
case cmp == 0: symbol = '='
case cmp < 0: symbol = '<'
case cmp > 0: symbol = '>'
}
fmt.Printf("%s %c %s\n\n", hand1, symbol, hand2)
}
func main () {
f, err := os.Create("prof")
if err != nil {
log.Fatal(err)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
for i := 0; i < 1000000; i++ {
CompareHands()
}
// result := RunHands()
// for k,v := range result {
// fmt.Printf("%s: %.2f\n", k.String(), 100.0 * float64(v.win) / float64(v.win+v.loss))
// }
}