-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathhand.go
More file actions
222 lines (197 loc) · 4.35 KB
/
hand.go
File metadata and controls
222 lines (197 loc) · 4.35 KB
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
package main
import "strings"
// "fmt"
type Hand []string
var RANKS string = "--23456789TJQKA"
// findWinners returns the slice of winning players.
func (g *Game) findWinners(players []*Player) []*Player {
if len(players) == 0 {
return nil
} else if len(players) == 1 {
return players
}
hands := make([]Hand, 0)
for _, p := range players {
if p.state == folded {
continue
}
hands = append(hands, p.bestHand)
}
winningHands := findWinningHands(hands)
winners := make([]*Player, 0)
for _, p := range players {
for _, h := range winningHands {
if areHandsEq(p.bestHand, h) {
winners = append(winners, p)
break
}
}
}
if len(hands) == 1 {
g.controller.recordWinners([]*Player{})
} else {
g.controller.recordWinners(winners)
}
return winners
}
//Returns nil if hands is empty
func findWinningHands(hands []Hand) (winners []Hand) {
if len(hands) == 0 {
return nil
} else if len(hands) == 1 {
return hands
}
winners = append(make([]Hand, 0), hands[0])
max_hand, max_count := winners[0].handRank()
for _, h := range hands[1:] {
test_hand, test_count := h.handRank()
if test_hand == max_hand && isEq(test_count, max_count) {
//The hands are equal
winners = append(winners, h)
} else if (test_hand > max_hand) ||
((test_hand == max_hand) && gt(test_count, max_count)) {
//test_hand > max_hand
winners = append(make([]Hand, 0), h)
max_hand, max_count = h.handRank()
} else if (test_hand < max_hand) ||
((test_hand == max_hand) && less(test_count, max_count)) {
//test_hand < max_hand
continue
}
}
return winners
}
// less returns true if a is less than b
func less(a, b []int) bool {
if len(a) != len(b) {
panic("len(a) != len(b) in less comparison")
}
for i := range a {
if a[i] < b[i] {
return true
} else if a[i] > b[i] {
return false
}
}
return false
}
// gt returns true if a is greater than b
func gt(a, b []int) bool {
if len(a) != len(b) {
panic("len(a) != len(b) in gt comparison")
}
for i := range a {
if a[i] > b[i] {
return true
} else if a[i] < b[i] {
return false
}
}
return false
}
func isEq(a, b []int) bool {
if len(a) != len(b) {
return false
}
for i := range a {
if a[i] != b[i] {
return false
}
}
return true
}
func (h Hand) rawRanksAndSuits() (ranks [5]int, suits [5]rune) {
for i, card := range h {
numeral := string(card[0])
ranks[i] = strings.Index(RANKS, numeral)
if ranks[i] == -1 {
panic("can't find card")
}
suits[i] = rune(card[1])
}
return ranks, suits
}
func countAndCombine(rawRanks [5]int) (counts []int, countRanks []int) {
var hash [15]int
for _, n := range rawRanks {
if n < 2 || n > 14 {
panic("ranking out of expected range")
}
hash[n]++
}
for q := 4; q >= 1; q-- {
for i := len(hash) - 1; i >= 2; i-- {
if hash[i] == q {
counts = append(counts, q)
countRanks = append(countRanks, i)
}
}
}
return counts, countRanks
}
func isStraight(countRanks []int) bool {
if len(countRanks) != 5 {
return false
}
if (countRanks[0] - countRanks[4]) != 4 {
return false
}
return true
}
func isFlush(suits [5]rune) bool {
suit := suits[0]
for _, v := range suits[1:] {
if suit != v {
return false
}
}
return true
}
//Returns the ranking for the hand and the card rankings
//'7 T 7 9 7' => handRank = 3 and cardRanks = [7, 10, 9]
// cardRanks is ordered by count first, then by card rank
func (h Hand) handRank() (handRank uint, countRanks []int) {
rawRanks, suits := h.rawRanksAndSuits()
counts, countRanks := countAndCombine(rawRanks)
if isEq(countRanks, []int{14, 5, 4, 3, 2}) {
//Ace low straight
countRanks = []int{5, 4, 3, 2, 1}
}
straight := isStraight(countRanks)
flush := isFlush(suits)
switch {
case straight && flush:
handRank = 8
case isEq(counts, []int{4, 1}):
handRank = 7
case isEq(counts, []int{3, 2}):
handRank = 6
case flush:
handRank = 5
case straight:
handRank = 4
case isEq(counts, []int{3, 1, 1}):
handRank = 3
case isEq(counts, []int{2, 2, 1}):
handRank = 2
case isEq(counts, []int{2, 1, 1, 1}):
handRank = 1
default:
if !isEq(counts, []int{1, 1, 1, 1, 1}) {
panic("Either have 5 of a kind, or missed a straight and/or a flush")
}
handRank = 0
}
return handRank, countRanks
}
func areHandsEq(a, b Hand) bool {
if len(a) != len(b) {
return false
}
for i := range a {
if a[i] != b[i] {
return false
}
}
return true
}