-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcard.py
More file actions
90 lines (67 loc) · 2.59 KB
/
card.py
File metadata and controls
90 lines (67 loc) · 2.59 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
"""Defines the Card class"""
class Card:
"""Represents a card in a card game
Attributes (data/information):
DIAMOND, CLUB, HEART, SPADE: class variables representing the card suits
_value: the value of the card, from 1 to 14
_suit: the suit of the card, from 1 to 4
Methods (functionality):
accessing the value and suit of the card
getting the name of the card (e.g. Queen for 12)
getting the name of the suit (e.g. Spades for 4)
string representation of the card (e.g. Ace of Spades, 3 of Diamonds)
"""
#define the suite values as class variables which are constant
DIAMONDS = 1
CLUBS = 2
HEARTS = 3
SPADES = 4
def __init__(self, value, suit):
"""Creates and initializes the attributes of the card object"""
self._value = value
self._suit = suit
def getValue(self):
"""Access the value of the card"""
return self._value
def setValue(self, newValue):
"""Change the value of the card"""
self._value = newValue
def getSuit(self):
"""Access the suit of the card"""
return self._suit
def setSuit(self, newSuit):
"""Change the suit of the card"""
self._suit = newSuit
def getCardName(self):
"""Determines the card name based on the card value.
The name of the card is an example of a calculated property that is
never stored as a separte field variable. Such property is also called
a derived attribute
"""
if (self._value == 1):
cardName = 'Ace'
elif (self._value == 13):
cardName = 'King'
elif (self._value == 12):
cardName = 'Queen'
elif (self._value == 11):
cardName = 'Jack'
else:
cardName = str(self._value)
#return the calculated card name to the caller
return cardName
def getSuitName(self):
"""Determine the name of the suit of the card based on the suit value"""
if self._suit == Card.DIAMONDS:
return 'Diamonds'
elif self._suit == Card.CLUBS:
return 'Clubs'
elif self._suit == Card.HEARTS:
return 'Hearts'
elif self._suit == Card.SPADES:
return 'Spades'
else:
assert False, 'Unknown suit value. Cannot return the suit name'
def __str__(self):
"""Default built-in method that is called automatically when a card object is printed"""
return f'{self.getCardName()} of {self.getSuitName()}'