Skip to content

Commit 4f8b7f3

Browse files
author
KitN
committed
Learning OOP with poker hands
1 parent 84ceac6 commit 4f8b7f3

3 files changed

Lines changed: 108 additions & 1 deletion

File tree

code/Card.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,18 @@ def move_cards(self, hand, num):
114114
for i in range(num):
115115
hand.add_card(self.pop_card())
116116

117+
def deal_hands(self, hands, handsize):
118+
""" Deals a given number of hands of a given size.
119+
120+
hands: number of hands to be dealt
121+
handsize: how many Cards should be in each Hand
122+
"""
123+
table = []
124+
for i in range(hands):
125+
curhand = Hand()
126+
self.move_cards(curhand, handsize)
127+
table.append(curhand)
128+
return table
117129

118130
class Hand(Deck):
119131
"""Represents a hand of playing cards."""
@@ -147,3 +159,9 @@ def find_defining_class(obj, method_name):
147159
deck.move_cards(hand, 5)
148160
hand.sort()
149161
print(hand)
162+
163+
print('***')
164+
headsup = deck.deal_hands(2,5)
165+
for hand in headsup:
166+
print(hand)
167+
print('---')

code/PokerHand.py

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

41123
if __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

code/Time2_soln.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ def __str__(self):
3333
hour, minute = divmod(minutes, 60)
3434
return '%.2d:%.2d:%.2d' % (hour, minute, second)
3535

36+
def __lt__(self, other):
37+
return self.seconds < other.seconds
38+
3639
def print_time(self):
3740
"""Prints a string representation of the time."""
3841
print(str(self))
@@ -109,6 +112,10 @@ def main():
109112
total = sum([t1, t2, t3])
110113
print(total)
111114

115+
print( t1 < t2)
116+
print( t2 < t3)
117+
print( t3 < t1)
118+
112119

113120
if __name__ == '__main__':
114121
main()

0 commit comments

Comments
 (0)