-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path054.rb
171 lines (150 loc) · 5.34 KB
/
054.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
def parse_hand(hand)
new_hand = {}
new_hand[:cards] = []
suits = []
hand.each do |card|
new_card = {}
value = card[0]
value = '10' if value == 'T'
value = '11' if value == 'J'
value = '12' if value == 'Q'
value = '13' if value == 'K'
value = '14' if value == 'A'
new_card[:value] = value.to_i
new_card[:suit] = card[1]
suits << card[1]
new_hand[:cards] << new_card
end
new_hand[:cards].sort! do |card1, card2|
card1[:value] <=> card2[:value]
end
new_hand[:flush] = suits.uniq.length == 1
new_hand[:straight] = ((new_hand[:cards][1][:value] == new_hand[:cards][0][:value] + 1) and (new_hand[:cards][2][:value] == new_hand[:cards][1][:value] + 1) and (new_hand[:cards][3][:value] == new_hand[:cards][2][:value] + 1) and (new_hand[:cards][4][:value] == new_hand[:cards][3][:value] + 1))
new_hand
end
def royal_flush(hand)
return hand[:cards][4][:value] if hand[:flush] and hand[:cards][0][:value] == 10 and hand[:cards][1][:value] == 11 and hand[:cards][2][:value] == 12 and hand[:cards][3][:value] == 13 and hand[:cards][4][:value] == 14
return nil
end
def straight_flush(hand)
return hand[:cards][4][:value] if hand[:flush] and hand[:straight]
return nil
end
def four_of_a_kind(hand)
if hand[:cards][1][:value] == hand[:cards][2][:value] and hand[:cards][2][:value] == hand[:cards][3][:value]
if hand[:cards][3][:value] == hand[:cards][4][:value]
return hand[:cards][4][:value]
elsif hand[:cards][0][:value] == hand[:cards][1][:value]
return hand[:cards][0][:value]
end
end
return nil
end
def full_house(hand)
if hand[:cards][0][:value] == hand[:cards][1][:value] and hand[:cards][2][:value] == hand[:cards][3][:value] and hand[:cards][3][:value] == hand[:cards][4][:value]
return hand[:cards][4][:value]
elsif hand[:cards][0][:value] == hand[:cards][1][:value] and hand[:cards][1][:value] == hand[:cards][2][:value] and hand[:cards][3][:value] == hand[:cards][4][:value]
return hand[:cards][0][:value]
end
return nil
end
def flush(hand)
return hand[:cards][4][:value] if hand[:flush]
return nil
end
def straight(hand)
return hand[:cards][4][:value] if hand[:straight]
return nil
end
def three_of_a_kind(hand)
if hand[:cards][2][:value] == hand[:cards][3][:value] and hand[:cards][3][:value] == hand[:cards][4][:value]
return hand[:cards][4][:value]
elsif hand[:cards][1][:value] == hand[:cards][2][:value] and hand[:cards][2][:value] == hand[:cards][3][:value]
return hand[:cards][3][:value]
elsif hand[:cards][0][:value] == hand[:cards][1][:value] and hand[:cards][1][:value] == hand[:cards][2][:value]
return hand[:cards][2][:value]
end
return nil
end
def two_pairs(hand)
high_pair = nil
low_pair = nil
if hand[:cards][3][:value] == hand[:cards][4][:value]
high_pair = hand[:cards][4][:value]
elsif hand[:cards][2][:value] == hand[:cards][3][:value]
high_pair = hand[:cards][3][:value]
end
if hand[:cards][1][:value] == hand[:cards][2][:value]
low_pair = hand[:cards][2][:value]
elsif hand[:cards][0][:value] == hand[:cards][1][:value]
low_pair = hand[:cards][1][:value]
end
return nil if high_pair.nil? or low_pair.nil?
return [high_pair, low_pair]
end
def one_pair(hand)
if hand[:cards][3][:value] == hand[:cards][4][:value]
return hand[:cards][4][:value]
elsif hand[:cards][2][:value] == hand[:cards][3][:value]
return hand[:cards][3][:value]
elsif hand[:cards][1][:value] == hand[:cards][2][:value]
return hand[:cards][2][:value]
elsif hand[:cards][0][:value] == hand[:cards][1][:value]
return hand[:cards][1][:value]
end
return nil
end
def two_pairs_tie_breaker(hand1, hand2)
return hand1[0] > hand2[0] unless hand1[0] == hand2[0]
return hand1[1] > hand2[1] unless hand1[1] == hand2[1]
return nil
end
def tie_breaker(hand1, hand2)
return (hand1[:cards][4][:value] > hand2[:cards][4][:value]) unless hand1[:cards][4][:value] == hand2[:cards][4][:value]
return (hand1[:cards][3][:value] > hand2[:cards][3][:value]) unless hand1[:cards][3][:value] == hand2[:cards][3][:value]
return (hand1[:cards][2][:value] > hand2[:cards][2][:value]) unless hand1[:cards][2][:value] == hand2[:cards][2][:value]
return (hand1[:cards][1][:value] > hand2[:cards][1][:value]) unless hand1[:cards][1][:value] == hand2[:cards][1][:value]
return (hand1[:cards][0][:value] > hand2[:cards][0][:value])
end
wins = 0
ranks = [:royal_flush, :straight_flush, :four_of_a_kind, :full_house, :flush, :straight, :three_of_a_kind, :two_pairs, :one_pair]
file = IO.readlines('poker.txt')
file.each do |game|
hand1 = parse_hand(game.slice!(0..13).split(' '))
hand2 = parse_hand(game.split(' '))
winner = false
ranks.each do |rank|
p1 = self.send(rank, hand1)
p2 = self.send(rank, hand2)
if p1 and p2.nil?
wins += 1
winner = true
break
elsif p2 and p1.nil?
winner = true
break
elsif p1 and p2
if rank == :two_pairs
tiebreak = two_pairs_tie_breaker(p1, p2)
unless tiebreak.nil?
wins += 1 if tiebreak
winner = true
break
end
else
unless p1 == p2
wins += 1 if p1 > p2
winner = true
break
end
end
wins += 1 if tie_breaker(hand1, hand2)
winner = true
break
end
end
unless winner
wins += 1 if tie_breaker(hand1, hand2)
end
end
puts wins