/
rank.go
164 lines (145 loc) · 3.07 KB
/
rank.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
package main
import "sort"
// ranks
const (
onePair = iota + 1
twoPairs
threeOfAKind
straight
flush
fullHouse
fourOfAKind
straightFlush
royalFlush
)
// weights
const (
rank = 3200000
highValue1 = 160000
highValue2 = 8000
highValue3 = 400
highValue4 = 20
highValue5 = 1
)
func rankPlayerHand(hand []int, valueChannel chan int) {
sort.Ints(hand)
values := make([]int, 5)
suits := make([]int, 5)
for i := 0; i < 5; i++ {
values[i] = hand[i] / 10
suits[i] = hand[i] % 10
}
sameSuit := true
suitZero := suits[0]
for i := 1; i < 5; i++ {
sameSuit = sameSuit && suits[i] == suitZero
if !sameSuit {
break
}
}
consecutiveValues := true
for i := 1; i < 5; i++ {
consecutiveValues = consecutiveValues && values[i] == values[i-1]+1
if !consecutiveValues {
break
}
}
handValue := 0
rankFound := false
if consecutiveValues {
if sameSuit {
if values[4] == ace {
handValue = rank * royalFlush
} else {
handValue = rank*straightFlush + highValue1*values[4]
}
} else {
handValue = rank*straight + highValue1*values[4]
}
rankFound = true
} else {
if sameSuit {
handValue = rank * flush
handValue += highValue1 * values[4]
handValue += highValue2 * values[3]
handValue += highValue3 * values[2]
handValue += highValue4 * values[1]
handValue += highValue5 * values[0]
rankFound = true
}
}
if !rankFound {
// must be one of those strange cases :)
valueChannel <- duplicateBasedValue(values)
return
}
valueChannel <- handValue
}
func duplicateBasedValue(values []int) (handValue int) {
occ := make(map[int]int)
for _, x := range values {
occ[x]++
}
looseValues := make([]int, 0)
pairValues := make([]int, 0)
switch len(occ) {
case 2:
fourRank := false
for v, o := range occ {
switch o {
case 1:
handValue += v * highValue2
case 2:
handValue += v * highValue2
case 3:
handValue += v * highValue1
case 4:
fourRank = true
handValue += v * highValue1
}
}
if fourRank {
handValue += rank * fourOfAKind
} else {
handValue += rank * fullHouse
}
case 3:
threeRank := false
for v, o := range occ {
switch o {
case 1:
looseValues = append(looseValues, v)
case 2:
pairValues = append(pairValues, v)
case 3:
threeRank = true
handValue += v * highValue1
}
}
sort.Ints(looseValues)
sort.Ints(pairValues)
if threeRank {
handValue += rank * threeOfAKind
handValue += looseValues[1]*highValue2 + looseValues[0]*highValue3
} else {
handValue += rank * twoPairs
handValue += pairValues[1]*highValue1 + pairValues[0]*highValue2 + looseValues[0]*highValue3
}
case 4:
handValue += rank * onePair
for v, o := range occ {
switch o {
case 1:
looseValues = append(looseValues, v)
case 2:
handValue += v * highValue1
}
}
sort.Ints(looseValues)
handValue += looseValues[2]*highValue2 + looseValues[1]*highValue3 + looseValues[0]*highValue4
case 5:
handValue += values[4]*highValue1 + values[3]*highValue2 + values[2]*highValue3 +
values[1]*highValue4 + values[0]*highValue5
}
return
}