-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdeck2.py
57 lines (47 loc) · 2.08 KB
/
deck2.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
# deck.py
"""Deck class represents a deck of Cards."""
import random
from carddataclass import Card
class DeckOfCards:
NUMBER_OF_CARDS = 52 # constant number of Cards
def __init__(self):
"""Initialize the deck."""
self.current_card = 0
self._deck = []
for count in range(DeckOfCards.NUMBER_OF_CARDS):
self._deck.append(Card(Card.FACES[count % 13],
Card.SUITS[count // 13]))
def shuffle(self):
"""Shuffle deck."""
self.current_card = 0
random.shuffle(self._deck)
def deal_card(self):
"""Return one Card."""
try:
card = self._deck[self.current_card]
self.current_card += 1
return card
except:
return None
def __str__(self):
"""Return a string representation of the current _deck."""
s = ''
for index, card in enumerate(self._deck):
s += f'{self.deal_card():<19}'
if (index + 1) % 4 == 0:
s += '\n'
return s
##########################################################################
# (C) Copyright 2019 by Deitel & Associates, Inc. and #
# Pearson Education, Inc. All Rights Reserved. #
# #
# DISCLAIMER: The authors and publisher of this book have used their #
# best efforts in preparing the book. These efforts include the #
# development, research, and testing of the theories and programs #
# to determine their effectiveness. The authors and publisher make #
# no warranty of any kind, expressed or implied, with regard to these #
# programs or to the documentation contained in these books. The authors #
# and publisher shall not be liable in any event for incidental or #
# consequential damages in connection with, or arising out of, the #
# furnishing, performance, or use of these programs. #
##########################################################################