-
Notifications
You must be signed in to change notification settings - Fork 0
/
range_manager.rb
executable file
·146 lines (125 loc) · 3.07 KB
/
range_manager.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
require './range_parser.rb'
require './range_formatter.rb'
module RangeTools
class RangeManager
include RangeFormatter
include RangeParser
attr_accessor :range
def initialize
@range = buildRange
end
def buildRange
ranks = %w(2 3 4 5 6 7 8 9 T J Q K A)
len = ranks.length
range = {}
ranks.each do |right|
cur = ranks.index(right)
lefts = ranks.slice(cur..len)
lefts.each do |left|
pair = true if left == right
range[(left + right).to_sym] = _combos(pair)
end
end
range
end
def combos
@combos ||= _combos
end
def _combos(pair=false)
combos = {}
suits = %w(c d h s)
suits.each do |lCombo|
suits.each do |rCombo|
next if lCombo == rCombo && pair
combos[(lCombo + rCombo).to_sym] = false
end
end
combos
end
def setAll(tag)
pair = true if tag[0] == tag[1]
_combos(pair).each_key do |combo|
range[tag][combo] = true
end
end
def setSuited(tag)
suits = [:cc, :dd, :hh, :ss]
suits.each do |suit|
range[tag][suit] = true
end
end
def setOffSuited(tag)
offSuits = combos.keys - [:cc, :dd, :hh, :ss]
offSuits.each do |offSuit|
range[tag][offSuit] = true
end
end
def setSingleHand(tag, suits)
suits.each do |combo|
range[tag][combo] = true
end
end
def resetAll
@range = buildRange
end
def populateRange(rangeString)
tagBuckets = parseRange(rangeString)
resetAll
processTagBuckets(tagBuckets)
end
def processTagBuckets(tagBuckets)
rangeSetters = {
setSingleHand: tagBuckets[:single],
setOffSuited: tagBuckets[:offsuited],
setSuited: tagBuckets[:suited],
setAll: tagBuckets[:both]
}
rangeSetters.each_pair do |setter, bucket|
setAllInBucket(setter, bucket)
end
end
def setAllInBucket(setter, bucket)
if bucket.is_a?(Hash) then
bucket.each_pair {|tag, suits| send(setter,tag, suits)}
else
bucket.each { |tag| send(setter, tag) }
end
end
def allSet?(hand, type)
hands = handCombosByType(hand,type)
hands.all? do |combo, set|
set
end
end
def anySet?(hand, type)
hands = handCombosByType(hand,type)
hands.any? do |combo, set|
set
end
end
def getSetCombos(hand, type)
hands = handCombosByType(hand,type)
hands.select do |combo, set|
set
end.keys
end
def handCombosByType(hand,type)
type = :pair if isPair(hand)
hands = @range[hand]
if type == :offsuits
hands = hands.select do |combo,v|
(combos.keys - [:cc, :dd, :hh, :ss]).include?(combo)
end
elsif type == :suits
hands = hands.select do |combo,v|
[:cc, :dd, :hh, :ss].include?(combo)
end
end
hands
end
def isPair(hand)
hand = hand.to_s
hand[0] == hand[1]
end
end
end