-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrankings.py
269 lines (253 loc) · 8.5 KB
/
rankings.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
from resources.deck import Deck
from collections import Counter
class Rankings(Deck):
straight = []
straight_flush = []
def clear_straights(self):
self.straight = []
self.straight_flush = []
def get_high_card(self):
if self.get_card_weight(self.main_player_cards[0]) < self.get_card_weight(self.main_player_cards[1]):
return [str(self.main_player_cards[1][1])]
else:
return [str(self.main_player_cards[0][1])]
def get_pair(self):
icons = []
for card in self.get_main_player_visible_cards():
icons.append(card[1])
counter = Counter(icons)
for c in counter.items():
if c[1] == 2:
return True, [str(c[0])]
return False, []
def get_two_pairs(self):
icons = []
for card in self.get_main_player_visible_cards():
icons.append(card[1])
counter = Counter(icons)
i = 0
pairs = []
for c in counter.items():
if c[1] == 2:
i += 1
pairs.append(str(c[0]))
if i >= 2:
return True, pairs
else:
return False, []
def get_three_of_a_kind(self):
icons = []
for card in self.get_main_player_visible_cards():
icons.append(card[1])
counter = Counter(icons)
for c in counter.items():
if c[1] == 3:
return True, [str(c[0])]
return False, []
def get_straight(self):
cards = [(i[0], self.get_card_weight(i)) for i in self.get_main_player_visible_cards()]
cards = sorted(cards, key=lambda tup: tup[1])
cards = [(i[0], self.get_card_out_of_weight(i[1])) for i in cards]
straight = [cards[0]]
i = 1
while i < len(cards):
if (self.get_card_weight(cards[i]) - self.get_card_weight(straight[-1])) <= 1:
straight.append((cards[i][0], cards[i][1]))
i += 1
else:
if len(straight) < 5:
straight.clear()
straight.append((cards[i][0], cards[i][1]))
i += 1
if set(['2', '3', '4', '5', 'A']).issubset([i[1] for i in cards]):
straight = []
for card in cards:
if card[1] is ('2' or '3' or '4' or '5' or 'A'):
straight.append(card)
elif card[1] is '6':
straight.append(card)
elif card[1] is '7' and ('6' in [i[1] for i in cards]):
straight.append(card)
self.straight = straight
return True, straight
straight_icons = [self.get_card_weight(i) for i in straight]
if len(set(straight_icons)) >= 5:
self.straight = straight
return True, straight
else:
return False, []
def get_flush(self):
suits = []
for card in self.get_main_player_visible_cards():
suits.append(card[0])
counter = Counter(suits)
for c in counter.items():
if c[1] >= 5:
return True, [str(c[0])]
return False, []
def get_full_house(self):
icons = []
for card in self.get_main_player_visible_cards():
icons.append(card[1])
counter = Counter(icons)
pairs = 0
three_of_a_kinds = 0
threes = []
twos = []
for c in counter.items():
if c[1] == 3:
threes.append(str(c[0]))
three_of_a_kinds += 1
if c[1] == 2:
twos.append(str(c[0]))
pairs += 1
if (pairs >= 1 and three_of_a_kinds == 1) or three_of_a_kinds == 2:
return True, [threes, twos]
return False, []
def get_four_of_a_kind(self):
icons = []
for card in self.get_main_player_visible_cards():
icons.append(card[1])
counter = Counter(icons)
for c in counter.items():
if c[1] >= 4:
return True, [str(c[0])]
return False, []
def get_straight_flush(self):
suits = []
for card in self.get_main_player_visible_cards():
suits.append(card[0])
counter = Counter(suits)
most_common_suit = 'placeholder'
for c in counter.items():
if c[1] >= 5:
most_common_suit = str(c[0])
if most_common_suit == 'placeholder':
return False, []
cards = []
for card in self.get_main_player_visible_cards():
if card[0] == most_common_suit:
cards.append(card)
# second straight check for suits
cards = [(i[0], self.get_card_weight(i)) for i in cards]
cards = sorted(cards, key=lambda tup: tup[1])
cards = [(i[0], self.get_card_out_of_weight(i[1])) for i in cards]
straight = [cards[0]]
i = 1
while i < len(cards):
if (self.get_card_weight(cards[i]) - self.get_card_weight(straight[-1])) <= 1:
straight.append((cards[i][0], cards[i][1]))
i += 1
else:
if len(straight) < 5:
straight.clear()
straight.append((cards[i][0], cards[i][1]))
i += 1
if set(['2', '3', '4', '5', 'A']).issubset([i[1] for i in cards]):
straight = []
for card in cards:
if card[1] is ('2' or '3' or '4' or '5' or 'A'):
straight.append(card)
elif card[1] is '6':
straight.append(card)
elif card[1] is '7' and ('6' in [i[1] for i in cards]):
straight.append(card)
self.straight_flush = straight
return True, straight
straight_icons = [self.get_card_weight(i) for i in straight]
if len(set(straight_icons)) >= 5:
self.straight_flush = straight
return True, straight
else:
return False, []
def get_royal_flush(self):
self.get_straight_flush()
if self.get_straight_flush()[0] and set(['A', 'K', 'Q', 'J', '10']).issubset([s[1] for s in self.straight_flush]):
return True, self.straight_flush
else:
return False, []
@staticmethod
def get_card_weight(card):
if str(card[1]) is '2':
weight = 2
elif str(card[1]) is '3':
weight = 3
elif str(card[1]) is '4':
weight = 4
elif str(card[1]) is '5':
weight = 5
elif str(card[1]) is '6':
weight = 6
elif str(card[1]) is '7':
weight = 7
elif str(card[1]) is '8':
weight = 8
elif str(card[1]) is '9':
weight = 9
elif str(card[1]) is '10':
weight = 10
elif str(card[1]) is 'J':
weight = 11
elif str(card[1]) is 'Q':
weight = 12
elif str(card[1]) is 'K':
weight = 13
elif str(card[1]) is 'A':
weight = 14
else:
weight = 999
print('No possible icon.')
return weight
@staticmethod
def get_card_out_of_weight(icon):
if icon is 2:
weight = '2'
elif icon is 3:
weight = '3'
elif icon is 4:
weight = '4'
elif icon is 5:
weight = '5'
elif icon is 6:
weight = '6'
elif icon is 7:
weight = '7'
elif icon is 8:
weight = '8'
elif icon is 9:
weight = '9'
elif icon is 10:
weight = '10'
elif icon is 11:
weight = 'J'
elif icon is 12:
weight = 'Q'
elif icon is 13:
weight = 'K'
elif icon is 14:
weight = 'A'
else:
weight = '1'
print('No possible number of card.')
return weight
def recognize_hand_ranking(self):
if self.get_royal_flush()[0]:
return 0
elif self.get_straight_flush()[0]:
return 1
elif self.get_four_of_a_kind()[0]:
return 2
elif self.get_full_house()[0]:
return 3
elif self.get_flush()[0]:
return 4
elif self.get_straight()[0]:
return 5
elif self.get_three_of_a_kind()[0]:
return 6
elif self.get_two_pairs()[0]:
return 7
elif self.get_pair()[0]:
return 8
else:
return 9