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
import random
15
16
17
class Card:
18
"""Represents a standard playing card.
19
20
Attributes:
21
suit: integer 0-3
22
rank: integer 1-13
23
"""
24
25
suit_names = ["Clubs", "Diamonds", "Hearts", "Spades"]
26
rank_names = [None, "Ace", "2", "3", "4", "5", "6", "7",
27
"8", "9", "10", "Jack", "Queen", "King"]
28
29
def __init__(self, suit=0, rank=2):
30
self.suit = suit
31
self.rank = rank
32
33
def __str__(self):
34
"""Returns a human-readable string representation."""
35
return '%s of %s' % (Card.rank_names[self.rank],
36
Card.suit_names[self.suit])
37
38
def __eq__(self, other):
39
"""Checks whether self and other have the same rank and suit.
40
41
returns: boolean
42
"""
43
return self.suit == other.suit and self.rank == other.rank
44
45
def __lt__(self, other):
46
"""Compares this card to other, first by suit, then rank.
47
48
returns: boolean
49
"""
50
t1 = self.suit, self.rank
51
t2 = other.suit, other.rank
52
return t1 < t2
53
54
55
class Deck:
56
"""Represents a deck of cards.
57
58
Attributes:
59
cards: list of Card objects.
60
"""
61
62
def __init__(self):
63
"""Initializes the Deck with 52 cards.
64
"""
65
self.cards = []
66
for suit in range(4):
67
for rank in range(1, 14):
68
card = Card(suit, rank)
69
self.cards.append(card)
70
71
def __str__(self):
72
"""Returns a string representation of the deck.
73
"""
74
res = []
75
for card in self.cards:
76
res.append(str(card))
77
return '\n'.join(res)
78
79
def add_card(self, card):
80
"""Adds a card to the deck.
81
82
card: Card
83
"""
84
self.cards.append(card)
85
86
def remove_card(self, card):
87
"""Removes a card from the deck or raises exception if it is not there.
88
89
card: Card
90
"""
91
self.cards.remove(card)
92
93
def pop_card(self, i=-1):
94
"""Removes and returns a card from the deck.
95
96
i: index of the card to pop; by default, pops the last card.
97
"""
98
return self.cards.pop(i)
99
100
def shuffle(self):
101
"""Shuffles the cards in this deck."""
102
random.shuffle(self.cards)
103
104
def sort(self):
105
"""Sorts the cards in ascending order."""
106
self.cards.sort()
107
108
def move_cards(self, hand, num):
109
"""Moves the given number of cards from the deck into the Hand.
110
111
hand: destination Hand object
112
num: integer number of cards to move
113
"""
114
for i in range(num):
115
hand.add_card(self.pop_card())
116
117
118
class Hand(Deck):
119
"""Represents a hand of playing cards."""
120
121
def __init__(self, label=''):
122
self.cards = []
123
self.label = label
124
125
126
def find_defining_class(obj, method_name):
127
"""Finds and returns the class object that will provide
128
the definition of method_name (as a string) if it is
129
invoked on obj.
130
131
obj: any python object
132
method_name: string method name
133
"""
134
for ty in type(obj).mro():
135
if method_name in ty.__dict__:
136
return ty
137
return None
138
139
140
if __name__ == '__main__':
141
deck = Deck()
142
deck.shuffle()
143
144
hand = Hand()
145
print(find_defining_class(hand, 'shuffle'))
146
147
deck.move_cards(hand, 5)
148
hand.sort()
149
print(hand)
150
151