-
Notifications
You must be signed in to change notification settings - Fork 0
/
poker_engine.py
195 lines (142 loc) · 5.26 KB
/
poker_engine.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
card_weight = {}
for i in range(2, 11):
card_weight[str(i)] = i
card_weight['J'] = 11
card_weight['Q'] = 12
card_weight['K'] = 13
card_weight['A'] = 14
def check_for_repeating_ranks_combinations(cards):
pairs = []
threes = []
fourth = []
full_house = []
ranks = {}
for card in cards:
rank = card[1]
if rank not in ranks:
ranks[rank] = [card]
else:
ranks[rank].append(card)
for rank, cards in ranks.items():
if len(cards) > 1:
if len(cards) % 2 == 0:
for card_idx in range(0, len(cards), 2):
pairs.append(cards[card_idx:card_idx + 2])
else:
pairs.append(cards[:2])
if len(cards) > 2:
if len(cards) > 3:
threes.append(cards[:3])
else:
threes.append(cards)
if len(cards) > 3:
fourth.append(cards)
combinations = {
'Pair': pairs,
'Three of a kind': threes,
'Four of a kind': fourth
}
if len(combinations['Pair']) > 0 and len(combinations['Three of a kind']) > 0 and len(
combinations['Four of a kind']) == 0:
for pair in combinations['Pair']:
for card in pair:
if card in combinations['Three of a kind'][0]:
break
else:
full_house.append(card)
full_house.append(combinations['Three of a kind'][0])
combinations['Full House'] = full_house
else:
combinations['Full House'] = None
return combinations
def check_for_flush(cards):
suits = {}
flush = None
for card in cards:
suit = card[0]
if suit not in suits:
suits[suit] = [card]
else:
suits[suit].append(card)
for suit, cards in suits.items():
if len(cards) >= 5:
flush = cards[:5]
return flush
def check_for_sequential_ranks_combinations(cards):
sorted_cards = sorted(cards, key=lambda x: card_weight[x[1:]])
all_cards_sorted = ' '.join(sorted(card_weight.keys(), key=lambda x: card_weight[x]))
sorted_cards_ranks = [card[1:] for card in sorted_cards]
ranks_to_cards_mapping = {}
for rank, card in zip(sorted_cards_ranks, sorted_cards):
ranks_to_cards_mapping[rank] = card
straights_with_ranks = []
for idx in range(len(sorted_cards_ranks)):
substring = sorted_cards_ranks[idx: idx + 5]
num_of_ranks = len(substring)
if num_of_ranks < 5:
continue
if " ".join(substring) in all_cards_sorted:
straights_with_ranks.append(substring)
if straights_with_ranks:
straight = [ranks_to_cards_mapping[rank] for rank in straights_with_ranks[0]]
return straight
return None
def check_hand(cards):
combinations = {}
repeating_ranks = check_for_repeating_ranks_combinations(cards)
flush = check_for_flush(cards)
sequential_ranks = check_for_sequential_ranks_combinations(cards)
if len(repeating_ranks['Pair']) > 0 and len(repeating_ranks['Pair']) < 2:
combinations['Pair'] = repeating_ranks['Pair'][0]
if len(repeating_ranks['Pair']) > 1:
two_pairs = []
for cards in repeating_ranks['Pair']:
two_pairs.extend(cards)
combinations['Two Pairs'] = two_pairs
if len(repeating_ranks['Three of a kind']) > 0:
combinations['Three of a kind'] = repeating_ranks['Three of a kind']
if sequential_ranks and len(sequential_ranks) > 0:
combinations['Straight'] = sequential_ranks
if flush and len(flush) > 0:
combinations['Flush'] = flush
if repeating_ranks['Full House'] and len(repeating_ranks['Full House']) > 0:
full_house = []
for card in repeating_ranks['Full House']:
if type(card) != list:
full_house.append(card)
else:
for c in card:
full_house.append(c)
combinations['Full House'] = full_house
if len(repeating_ranks['Four of a kind']) > 0:
combinations['Four of a kind'] = repeating_ranks['Four of a kind'][0]
if flush and check_for_sequential_ranks_combinations(flush):
combinations['Straight Flush'] = check_for_sequential_ranks_combinations(flush)
sorted_cards = sorted(cards, key=lambda x: card_weight[x[1:]])
sorted_cards_ranks = ''.join([card[1:] for card in sorted_cards])
if '10JQKA' in sorted_cards_ranks:
if check_for_sequential_ranks_combinations(cards):
combinations['Royal Flush'] = check_for_sequential_ranks_combinations(cards)
return combinations
def get_higher_card(cards):
return sorted(cards, key=lambda x: card_weight[x[1]], reverse=True)[0]
def get_best_hand(combinations):
rankings = {
'Pair': 0,
'Two Pairs': 1,
'Three of a kind': 2,
'Straight': 3,
'Flush': 4,
'Full House': 5,
'Four of a kind': 6,
'Straight Flush': 7,
'Royal Flush': 8
}
best_score = -1
best_combination = None
for combination in combinations:
score = rankings[combination]
if score > best_score:
best_score = score
best_combination = combination
return best_score, best_combination