-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPokerHand.py
142 lines (121 loc) · 3.66 KB
/
PokerHand.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
from Card import *
from exercise_11 import invert_dict
class PokerHand(Hand):
def suit_hist(self):
self.suits = {}
for card in self.cards:
self.suits[card.suit] = self.suits.get(card.suit, 0) + 1
def has_flush(self):
self.suit_hist()
for val in self.suits.values():
if val >= 5:
return True
return False
def rank_hist(self):
self.ranks = {}
for card in self.cards:
self.ranks[card.rank] = self.ranks.get(card.rank, 0) + 1
def has_pair(self):
self.rank_hist()
for val in self.ranks.values():
if val >= 2:
return True
return False
def has_two_pair(self):
self.rank_hist()
count = 0
for val in self.ranks.values():
if val >= 2:
count += 1
if count == 2:
return True
return False
def has_three_of_a_kind(self):
self.rank_hist()
for val in self.ranks.values():
if val >= 3:
return True
return False
def has_four_of_a_kind(self):
self.rank_hist()
for val in self.ranks.values():
if val == 4:
return True
return False
def has_full_house(self):
if self.has_two_pair() and self.has_three_of_a_kind():
return True
return False
def has_straight(self):
self.rank_hist()
t = sorted(list(self.ranks.keys()))
r = get_combinations_of_hand_ranks(t)
straights = get_straights()
for x in r:
if x in straights:
return True
return False
def has_straight_flush(self):
if self.has_flush() and self.has_straight():
return True
return False
def classify(self):
self.label = 'nothing'
if self.has_straight_flush():
self.label = 'straight-flush'
return self.label
elif self.has_four_of_a_kind():
self.label = 'four-of-a-kind'
return self.label
elif self.has_full_house():
self.label = 'full-house'
return self.label
elif self.has_flush():
self.label = 'flush'
return self.label
elif self.has_straight():
self.label = 'straight'
return self.label
elif self.has_three_of_a_kind():
self.label = 'three-of-a-kind'
return self.label
elif self.has_two_pair():
self.label = 'two-pair'
return self.label
elif self.has_pair():
self.label = 'one-pair'
return self.label
return self.label
def get_straights():
straights = []
i = 1
while i < 10:
straights.append([i, i + 1, i + 2, i + 3, i + 4])
i += 1
straights.append([1, 13, 12, 11, 10])
return straights
def get_combinations_of_hand_ranks(t):
comb = []
i = 1
while i < (len(t) - 4):
comb.append([t[i], t[i + 1], t[i + 2], t[i + 3], t[i + 4]])
i += 1
return comb
if __name__ == '__main__':
some_list = {}
# deal the cards and classify the hands
for j in range(10000):
# make a deck
deck = Deck()
deck.shuffle()
for i in range(5):
hand = PokerHand()
deck.move_cards(hand, 7)
hand.sort()
some_list[hand.classify()] = some_list.get(hand.classify(), 0) + 1
for key, value in some_list.items():
some_list[key] = round(value/50000, 4)
new_list = invert_dict(some_list)
rankings = sorted(list(new_list.items()))
for x in rankings:
print(x)