-
Notifications
You must be signed in to change notification settings - Fork 0
/
hand_evaluator.rb
executable file
·90 lines (80 loc) · 3.09 KB
/
hand_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
require('./pair_evaluator.rb')
require('./flush_evaluator.rb')
require('./straight_evaluator.rb')
module RangeTools
module HandEvaluator
def evalHand(board, twoCardHand, madeHands)
madePairHands, flushStrength, straightStrength = madeHandInfo(twoCardHand, board)
madeHand = bestMadeHand(flushStrength, straightStrength, madePairHands)
if madeHand.nil?
madeHands = markDrawHands(madeHands, madePairHands, straightStrength, flushStrength, twoCardHand)
elsif madeHand == :quads_or_full_house
madeHands = markHands(madePairHands, madeHands, twoCardHand)
else
madeHands = markMadeHand(madeHands, madeHand, twoCardHand)
end
madeHands
end
def madeHandInfo(twoCardHand, board)
[
PairEvaluator.evalPairHands(twoCardHand, board),
FlushEvaluator.evalFlush(twoCardHand, board),
StraightEvaluator.evalStraight(twoCardHand, board)
]
end
def markDrawHands(madeHands, madePairHands, straightStrength, flushStrength, twoCardHand)
hasPair = true if madePairHands[:pair]
drawHands = madeDrawHands(straightStrength, flushStrength, hasPair)
madeHands = markHands(drawHands, madeHands, twoCardHand)
markHands(madePairHands, madeHands, twoCardHand)#ace high, overcards
end
def bestMadeHand(flushStrength, straightStrength, madePairHands)
if (flushStrength[:fullHand] == :flush && straightStrength[:fullHand] == :straight)
:straight_flush
elsif (madePairHands[:quads] || madePairHands[:full_house])
:quads_or_full_house
elsif flushStrength[:fullHand] == :flush
:flush
elsif straightStrength[:fullHand] == :straight
:straight
end
end
def madeDrawHands(straightStrength, flushStrength, hasPair)
straight, straightBoard = straightStrength[:fullHand], straightStrength[:board]
flush, flushBoard = flushStrength[:fullHand], flushStrength[:board]
hands = {
combo_draw: straight && flush,
pair_plus_flush_draw: hasPair && (flush == :flush_draw),
flush_draw_on_board: flushBoard && flushBoard == :flush_draw
}
pairPlusX = ('pair_plus_' + straight.to_s).to_sym if straightBoard
hands[pairPlusX] = hasPair && straight if pairPlusX
hands[flush] = true if flush
hands[straight] = true if straight
hands[(straightBoard.to_s + '_on_board').to_sym] = true if straightBoard
hands
end
def markHands(hands, madeHands, twoCardHand)
hands.each_pair do |hand, isSet|
if isSet
madeHands = markMadeHand(madeHands, hand, twoCardHand)
end
end
madeHands
end
def markMadeHand(madeHands, handType, twoCardHand)
handTag = buildHandTag(twoCardHand)
madeHands[handType] << handTag
madeHands
end
def buildHandTag(twoCardHand)
card1, card2 = twoCardHand[0], twoCardHand[1]
if twoCardHand[0][:rank] < twoCardHand[1][:rank]
card1, card2 = card2, card1
end
r1, r2 = card1[:tag].to_s, card2[:tag].to_s
s1, s2 = card1[:suit].to_s, card2[:suit].to_s
r1+r2+s1+s2
end
end
end