-
Notifications
You must be signed in to change notification settings - Fork 0
/
054_poker_hands.py
269 lines (223 loc) · 6.33 KB
/
054_poker_hands.py
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
"""
Task:
Rank values:
1 High Card: Highest value card.
2 One Pair: Two cards of the same value.
3 Two Pairs: Two different pairs.
4 Three of a Kind: Three cards of the same value.
5 Straight: All cards are consecutive values.
6 Flush: All cards of the same suit.
7 Full House: Three of a kind and a pair.
8 Four of a Kind: Four cards of the same value.
9 Straight Flush: All cards are consecutive values of same suit.
10 Royal Flush: Ten, Jack, Queen, King, Ace, in same suit.
"""
import time
def main():
start_time = time.time()
data = read_file("054_poker.txt")
wins = -3
for example in data:
hand1 = example[:5]
hand2 = example[5:]
rank1 = get_rank(hand1)
rank2 = get_rank(hand2)
if rank1 > rank2:
wins += 1
elif rank1 == rank2:
if rank1 == 1:
res = compare_highest_card_in_hands(hand1, hand2)
if res == 1:
wins += 1
elif res == 0:
print('same highest card', rank_to_string(rank1), ' - ', rank_to_string(rank2))
elif rank1 == 2:
res = compare_one_pairs(hand1, hand2)
if res == 1:
wins += 1
elif res == 0:
res = compare_highest_card_in_hands(hand1, hand2)
if res == 1:
wins += 1
elif res == 0:
print('same highest card', rank_to_string(rank1), ' - ', rank_to_string(rank2))
else:
print('same diff rank')
print('wins', wins)
print("time:", time.time() - start_time)
def read_file(filename):
data_file = open(filename)
data_set = []
for line in data_file.readlines():
data_set.append(line.strip().split(' '))
return data_set
def get_rank(hand):
"""
get rank with according card from player 1 and 2
if tie remove cards involved in that card, get rank again and compare once mor
repeat until winner is definite
:returns (rank, with which card)
"""
if is_royal_flush(hand):
return 10
elif is_straight_flush(hand):
return 9
elif is_four_of_a_kind(hand):
return 8
elif is_full_house(hand):
return 7
elif is_flush(hand):
return 6
elif is_straight(hand):
return 5
elif is_three_of_a_kind(hand):
return 4
elif is_two_pairs(hand):
return 3
elif is_one_pair(hand):
return 2
else:
return 1
def is_royal_flush(hand):
if not is_flush(hand):
return False
# check if Ass, King, Queen, ... are in the hand
necessary_cards = {'A', 'K', 'Q', 'J', 'T'}
for card in hand:
if card[0] not in necessary_cards:
return False
else:
necessary_cards.remove(card[0])
return True
def is_straight_flush(hand):
return is_flush(hand) and is_straight(hand)
def is_four_of_a_kind(hand):
card_counter = dict()
for card in hand:
if card[0] in card_counter:
card_counter[card[0]] += 1
else:
card_counter[card[0]] = 1
max_count = max(card_counter.values())
return max_count == 4
def is_full_house(hand):
card_counter = dict()
for card in hand:
if card[0] in card_counter:
card_counter[card[0]] += 1
else:
card_counter[card[0]] = 1
count = card_counter.values()
if len(count) == 2:
return True
def is_flush(hand):
# check if all cards are of the same color
hand_color = hand[0][1]
for card in hand:
if card[1] != hand_color:
return False
return True
def is_straight(hand):
hand = [card_to_int(card[0]) for card in hand]
hand.sort()
last = hand[0] - 1
for card in hand:
if card == last + 1:
last += 1
else:
return False
def is_three_of_a_kind(hand):
card_counter = dict()
for card in hand:
if card[0] in card_counter:
card_counter[card[0]] += 1
else:
card_counter[card[0]] = 1
max_count = max(card_counter.values())
return max_count == 3
def is_two_pairs(hand):
card_counter = dict()
for card in hand:
if card[0] in card_counter:
card_counter[card[0]] += 1
else:
card_counter[card[0]] = 1
return len(card_counter) == 3
def is_one_pair(hand):
card_counter = dict()
for card in hand:
if card[0] in card_counter:
card_counter[card[0]] += 1
else:
card_counter[card[0]] = 1
return len(card_counter) == 4
def compare_highest_card_in_hands(hand1, hand2):
hand1_ints = [card_to_int(card[0]) for card in hand1]
hand2_ints = [card_to_int(card[0]) for card in hand2]
if max(hand1_ints) == max(hand2_ints):
return 0
elif max(hand1_ints) > max(hand2_ints):
return 1
else:
return -1
def compare_one_pairs(hand1, hand2):
hand1_ints = [card_to_int(card[0]) for card in hand1]
hand2_ints = [card_to_int(card[0]) for card in hand2]
pair1 = 0
count1 = set()
for card in hand1_ints:
if card in count1:
pair1 = card
else:
count1.add(card)
pair2 = 0
count2 = set()
for card in hand2_ints:
if card in count2:
pair2 = card
else:
count2.add(card)
if pair1 == pair2:
return 0
elif pair1 > pair2:
return 1
else:
return -1
def card_to_int(card):
if not card.isalpha():
return int(card)
elif card == 'T':
return 10
elif card == 'J':
return 11
elif card == 'Q':
return 12
elif card == 'K':
return 13
elif card == "A":
return 14
else:
print("unknown card!", card)
def rank_to_string(rank):
if rank == 10:
return 'royal flash'
elif rank == 9:
return 'straight flush'
elif rank == 8:
return 'four of a kind'
elif rank == 7:
return 'full house'
elif rank == 6:
return 'flush'
elif rank == 5:
return 'straight'
elif rank == 4:
return 'three of a kind'
elif rank == 3:
return 'two pairs'
elif rank == 2:
return 'one pair'
else:
return 'highest card'
if __name__ == '__main__':
main()