@@ -26,6 +26,80 @@ def suit_hist(self):
2626 for card in self .cards :
2727 self .suits [card .suit ] = self .suits .get (card .suit , 0 ) + 1
2828
29+ def rank_hist (self ):
30+ """Builds a histogram of the ranks (number value) that appear.
31+
32+ Stores the result in attribute ranks.
33+ """
34+ self .ranks = {}
35+ for card in self .cards :
36+ self .ranks [card .rank ] = self .ranks .get (card .rank , 0 ) + 1
37+
38+ def has_flush (self ):
39+ """Returns True if the hand has a flush, False otherwise.
40+
41+ Note that this works correctly for hands with more than 5 cards.
42+ """
43+ self .suit_hist ()
44+ for val in self .suits .values ():
45+ if val >= 5 :
46+ return True
47+ return False
48+
49+ def has_pair (self ):
50+ """Returns True if the hand has two of a kind at least once, False
51+ otherwise
52+
53+ Note that a hand with three fives will also 'have a pair'
54+ """
55+ self .rank_hist ()
56+ for val in self .ranks .values ():
57+ if val >= 2 :
58+ return True
59+ return False
60+
61+ def has_two_pair (self ):
62+ """Returns True if the hand has two of a kind at least TWICE, False
63+ otherwise
64+
65+ Note that a hand with three fives will also 'have a pair'
66+ """
67+ self .rank_hist ()
68+ count = 0
69+ for val in self .ranks .values ():
70+ if val >= 2 :
71+ count += 1
72+ if count >= 2 :
73+ return True
74+ return False
75+
76+ def has_three_o_kind (self ):
77+ """Returns True if the hand has a three of a kind, False otherwise
78+
79+ Overlaps with better hands (e.g. four o' a kind)
80+ """
81+ self .rank_hist ()
82+ for val in self .ranks .values ():
83+ if val >= 3 :
84+ return True
85+ return False
86+
87+ def has_straight (self ):
88+ """ Aces high or lo"""
89+ self .rank_hist ()
90+ prevkey = 0
91+ count = 0
92+ for key in self .ranks .keys ():
93+ if key == prevkey + 1 :
94+ count += 1
95+ if count == 5 :
96+ return True
97+ else :
98+ count = 0
99+ prevkey = key
100+
101+ return False
102+
29103 def has_flush (self ):
30104 """Returns True if the hand has a flush, False otherwise.
31105
@@ -37,6 +111,14 @@ def has_flush(self):
37111 return True
38112 return False
39113
114+ def has_full_house (self ):
115+ pass
116+
117+ def has_four_o_kind (self ):
118+ pass
119+
120+ def has_straight_flush (self ):
121+ pass
40122
41123if __name__ == '__main__' :
42124 # make a deck
@@ -49,6 +131,6 @@ def has_flush(self):
49131 deck .move_cards (hand , 7 )
50132 hand .sort ()
51133 print (hand )
52- print (hand .has_flush ())
134+ print (hand .has_straight ())
53135 print ('' )
54136
0 commit comments