-
Notifications
You must be signed in to change notification settings - Fork 0
/
pair_evaluator.rb
177 lines (153 loc) · 4.68 KB
/
pair_evaluator.rb
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
module RangeTools
module PairEvaluator
extend self
def evalPairHands(twoCardHand, board)
pairBuckets = buildPairBuckets(twoCardHand, board)
pairBuckets = preparePairBuckets(pairBuckets)
evalPairBuckets(pairBuckets, twoCardHand, board)
end
private
def evalPairBuckets(pairBuckets, twoCardHand, board)
pairHands = {}
pairValue = findPairValue(pairBuckets)
pairHands[pairValue] = true if pairValue
if pairValue == :pair
pairHands = evalPairType(pairHands, pairBuckets, twoCardHand, board)
elsif pairValue.nil?
pairHands = evalOverCards(pairHands, twoCardHand, board)
end
pairHands
end
def findPairValue(pairBuckets)
pairValue = nil
if pairValue = hasQuads(pairBuckets)
elsif pairValue = hasFullHouse(pairBuckets)
elsif pairValue = hasTrips(pairBuckets)
elsif pairValue = hasTwoPair(pairBuckets)
else pairValue = hasPair(pairBuckets)
end
pairValue
end
def hasQuads(pairBuckets)
:quads if pairBuckets[:quads].any?
end
def hasFullHouse(pairBuckets)
found = false
if pairBuckets[:trips].length > 1
found = true
elsif pairBuckets[:trips].any? and pairBuckets[:pairs].any?
found = true
end
:full_house if found
end
def hasTrips(pairBuckets)
:trips if pairBuckets[:trips].any?
end
def hasTwoPair(pairBuckets)
:two_pair if pairBuckets[:pairs].length > 1
end
def hasPair(pairBuckets)
:pair if pairBuckets[:pairs].any?
end
def evalPairType(pairHands, pairBuckets, twoCardHand, board)
lcard, rcard = twoCardRanks(twoCardHand)
ranks = boardRanks(board)
if ranks.include?(lcard) || ranks.include?(rcard) || (lcard == rcard)
pairHands = evalPairStrength(pairHands, pairBuckets)
pairHands = evalPocketPair(pairHands, lcard, rcard, board)
pairHands = evalTopPair(pairHands, lcard, rcard, board)
else
pairHands[:pair_on_board] = true
end
pairHands
end
def evalTopPair(pairHands, lcard, rcard, board)
ranks = boardRanks(board)
if (ranks.include?(lcard) && ranks.max == lcard) \
|| (ranks.include?(rcard) && ranks.max == rcard)
pairHands[:top_pair] = true
end
pairHands
end
def evalPocketPair(pairHands, lcard, rcard, board)
if lcard == rcard
pairHands[:pocket_pair] = true
pairHands[:premium_pocket] = true if lcard > 11#QQ+
pairHands[:over_pair] = true if boardRanks(board).max < lcard
end
pairHands
end
def evalPairStrength(pairHands, pairBuckets)
pair = pairBuckets[:pairs].first
if pair < 7
pairHands[:low_pair] = true
elsif pair < 10
pairHands[:mid_pair] = true
else
pairHands[:high_pair] = true
end
pairHands
end
def evalOverCards(pairHands, twoCardHand, board)
board = boardRanks(board)
lcard, rcard = twoCardRanks(twoCardHand)
if lcard > board.max && rcard > board.max
pairHands[:over_cards] = true
elsif lcard > board.max || rcard > board.max
pairHands[:one_over_card] = true
end
pairHands[:premium_overs] = true if [lcard, rcard].min > 11
pairHands[:ace_high] = true if [lcard, rcard].include?(14)
pairHands
end
def preparePairBuckets(pairBuckets)
replaceTagsWithNumbers(pairBuckets)
end
def replaceTagsWithNumbers(pairBuckets)
numberBuckets = {}
pairBuckets.each_pair do |bucket, tags|
numberBuckets[bucket] = tags.map {|tag| rankNumber(tag)}
end
numberBuckets
end
def rankNumber(rank)
nums = (2..9).collect { |x| x.to_s.to_sym }
orders = nums + [:T, :J, :Q, :K, :A]
orders.index(rank) + 2
end
#hand is array of card hashes
#{suit: :c, rank: 13, tag: :A} => Ac
def buildPairBuckets(hand, board)
tags = (hand + board).collect { |card| card[:tag] }
uniques = tags.uniq
tagCounts = uniques.collect { |tag| Hash[tag, tags.count(tag)] }
tagCounts.inject(_pairBuckets) { |bucket, tagCount|
tag, count = tagCount.shift
bucket[whichPairBucket(count)] << tag
bucket
}
end
def whichPairBucket(count)
case count
when 1 then :highs
when 2 then :pairs
when 3 then :trips
when 4 then :quads
end
end
def _pairBuckets
pairBuckets = {
highs: [],
pairs: [],
trips: [],
quads: []
}
end
def twoCardRanks(twoCardHand)
twoCardHand.collect {|card| card[:rank] }
end
def boardRanks(board)
board.collect {|card| card[:rank] }
end
end
end