-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrange_parser.rb
executable file
·116 lines (106 loc) · 2.96 KB
/
range_parser.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
module RangeTools
module RangeParser
def parseRange(rangeString)
tagBuckets = {
suited: [],
offsuited: [],
both: [],
single: {}
}
expandRangeTags(rangeString, tagBuckets)
end
def expandRangeTags(rangeString, tagBuckets)
rangeTags = rangeString.split(',')
rangeTags.each do |rangeTag|
rangeTag.strip!
tagType = getTagType(rangeTag)
if tagType.to_s.index('spanner').nil?
tags = [rangeTag]
else
tags = expandRangeTag(rangeTag, tagType)
end
tagBuckets = addToTagBucket(tags, tagType, tagBuckets)
end
tagBuckets
end
def addToTagBucket(tags, tagType, tagBuckets)
bucket = bucketType(tagType)
if bucket == :single
tag = tags[0]
tagBuckets = addSingle(tag, tagBuckets)
else
tags.each do |tag|
tagBuckets[bucket] << tag.slice(0,2).to_sym
end
end
tagBuckets
end
def addSingle(tag, tagBuckets)
hand = tag.slice(0,2).to_sym
suit = tag.slice(2,4).to_sym
tagBuckets[:single][hand] ||= []
tagBuckets[:single][hand] << suit
tagBuckets
end
def bucketType(tagType)
types = {
suited: [:suitspanner, :suit],
offsuited: [:offsuitspanner, :offsuit],
both: [:spanner, :pairspanner, :both, :pair],
single: [:single]
}
bucketType = nil
types.each_pair do |bucket,tagTypes|
bucketType = bucket if tagTypes.include?(tagType)
end
bucketType
end
def expandRangeTag(rangeTag, tagType)
span = expandSpanner(rangeTag)
expandedTags = nil
if tagType == :pairspanner
expandedTags = span.collect {|card| card + card}
else
suitTag = suitedType(tagType)
expandedTags = span.collect do |rightCard|
rangeTag[0] + rightCard + suitTag
end
end
expandedTags
end
def suitedType(tagType)
case tagType
when :suitspanner then 's'
when :offsuitspanner then 'o'
when :spanner then ''
end
end
def expandSpanner(rangeTag)
ranks = 'AKQJT98765432'
startCard = ranks.index(rangeTag[1])
endCard = ranks.index(rangeTag[3])
span = ranks.slice(startCard, endCard - startCard + 1)
span.split('')
end
def getTagType(rangeTag)
#order matters here! todo fix it
tagTypes = {
spanner: /^[A-Z\d]+-[A-Z\d]+$/,
suitspanner: /^[A-Z\d]+-[A-Z\d]s+$/,
offsuitspanner: /^[A-Z\d]+-[A-Z\d]o+$/,
pairspanner: /^([A-Z\d])\1-([A-Z\d])\2$/,
offsuit: /^[A-Z\d][A-Z\d]o$/,
suit: /^[A-Z\d][A-Z\d]s$/,
both: /^[A-Z\d][A-Z\d]$/,
single: /^[A-Z\d][A-Z\d][cdhs][cdhs]$/,
pair: /^([A-Z\d])\1$/
}
found = nil
tagTypes.each_pair do |type, pattern|
found = type if rangeTag =~ pattern
end
raise 'bad tag in rangestring' if found.nil?
found
end
end
end