Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
| Download

📚 The CoCalc Library - books, templates and other resources

Views: 96168
License: OTHER
1
"""This module contains a code example related to
2
3
Think Python, 2nd Edition
4
by Allen Downey
5
http://thinkpython2.com
6
7
Copyright 2015 Allen Downey
8
9
License: http://creativecommons.org/licenses/by/4.0/
10
"""
11
12
from __future__ import print_function, division
13
14
from Card import Hand, Deck
15
16
17
class PokerHand(Hand):
18
"""Represents a poker hand."""
19
20
def suit_hist(self):
21
"""Builds a histogram of the suits that appear in the hand.
22
23
Stores the result in attribute suits.
24
"""
25
self.suits = {}
26
for card in self.cards:
27
self.suits[card.suit] = self.suits.get(card.suit, 0) + 1
28
29
def has_flush(self):
30
"""Returns True if the hand has a flush, False otherwise.
31
32
Note that this works correctly for hands with more than 5 cards.
33
"""
34
self.suit_hist()
35
for val in self.suits.values():
36
if val >= 5:
37
return True
38
return False
39
40
41
if __name__ == '__main__':
42
# make a deck
43
deck = Deck()
44
deck.shuffle()
45
46
# deal the cards and classify the hands
47
for i in range(7):
48
hand = PokerHand()
49
deck.move_cards(hand, 7)
50
hand.sort()
51
print(hand)
52
print(hand.has_flush())
53
print('')
54
55
56