In: Computer Science
PYTHON
A Class for a Deck of Cards
We will create a class called Card whose objects we will imagine to be representations of playing cards. Each object of the class will be a particular card such as the '3' of clubs or 'A' of spades. For the private data of the class we will need a card value ('A', '2', '3', ... 'Q', 'K') and a suit (spades, hearts, diamond, clubs).
Before we design this class, let's see a very simple main() (also called the client, since it uses, or employs, our class) that instantiates a few Card objects and displays their values on the screen. We want to do a lot of fun things with our Card, but not yet. We start very carefully and slowly.
from enum import Enum def main(): card1 = Card() card2 = Card('5') card3 = Card('9', Suit.HEARTS) card4 = Card('j', Suit.CLUBS) card5 = Card('1', Suit.DIAMONDS) if not card2.set(2, Suit.CLUBS): print("GOOD: incorrect value (2 (int), CLUBS) passed to Card's set()\n") if not card2.set('2', Suit.CLUBS): print("BAD: incorrect value ('2', CLUBS) passed to Card's set()\n") print(str(card1) + "\n" + str(card2) + "\n" + str(card3) + "\n" + str(card4) + "\n" + str(card5) + "\n") card1 = card4 # test assignment operator for objects print("after assigning card4 to card1:") print(str(card1) + "\n" + str(card4) + "\n")
We can't run this main() without embedding it into a full program that has the Card class definition, but let's look at a copy of the run anyhow, so we can see what the correct output for this program should be:
GOOD: incorrect value (2 (int), CLUBS) passed to Card's set() (A of Spades) (2 of Clubs) (9 of Hearts) (J of Clubs) (A of Spades) after assigning card4 to card1: (J of Clubs) (J of Clubs)
We can learn a lot about what class Card must be and do by looking at main() and the run. Let's make a list of things that we can surmise just by looking at the client.
card1 = Card() card2 = Card('5') card3 = Card('9', Suit.HEARTS) card4 = Card('j', Suit.CLUBS) card5 = Card('1', Suit.DIAMONDS)
We see that card1 is instantiated with no arguments. This tells us that the class has a constructor (always __init__()) that does not require any arguments. Do you remember how that's done? Right! Either a default constructor (no formal parameters) or a constructor with all default parameters. So it must be one of those types.
However, we also see that card2 is providing the constructor with one argument and card3, card4 and card5 with two arguments. So, we must also have at least a 2-parameter constructor, and whatever number of parameters it has, they must all be default parameters (not counting the self parameter).
card3 = Card('9', Suit.HEARTS)The first argument, the value, is passed to the constructor as what data type? It is surrounded by quotes '5', '9', 'j', so it must be a string. (See Note On String Delimiters, below.) But the suit is a little odd. It is a construct like Suit.CLUBS or Suit.DIAMONDS with no quotes around it. Words without quotes in Python usually refer to variable names, but in this case, that's not what they are. Here we are using enumerated or enum types. You may not have had enums in your first course, so you will learn about them this week.
CODE IN PYTHON:
#importing enum
import enum
#creating enum using class
class Suit:
SPADES = "spades"
HEARTS = "hearts"
CLUBS = "clubs"
DIAMONDS = "diamonds"
#creating Card class
class Card:
#constructor with default values A of spades
def __init__(self,value="A",suit="spades"):
if(value=="1"):
self.__value = "A"
else:
self.__value = value
self.__suit = suit
#set method
def set(self,value,suit):
value = str(value)
flag = False
if(value == "A"):
falg = True
elif(value == "2"):
flag = True
elif(value == "3"):
flag = True
elif(value == "4"):
flag = True
elif(value == "5"):
flag = True
elif(value == "6"):
flag = True
elif(value == "7"):
flag = True
elif(value == "8"):
flag = True
elif(value == "9"):
flag = True
elif(value == "10"):
flag = True
elif(value == "J"):
flag = True
elif(value == "Q"):
flag = True
elif(value == "K"):
flag = True
if(flag):
self.value = value;
flag = False
if(suit == "spades"):
flag = True
elif(suit == "hearts"):
flag = True
elif(suit == "diamonds"):
flag = True
elif(suit == "clubs"):
flag = True
if(flag):
self.suit = suit
return True
else:
print("invalid suit input....")
return False
else:
print("invalid value input....")
return False
def getValue(self):
return self.value
def getSuit(self):
return self.suit
def __str__(self):
return self.__value + " of " + self.__suit
card1 = Card()
card2 = Card('5')
card3 = Card('9', Suit.HEARTS)
card4 = Card('j', Suit.CLUBS)
card5 = Card('1', Suit.DIAMONDS)
if not card2.set(2, Suit.CLUBS):
print("GOOD: incorrect value (2 (int), CLUBS) passed to Card's
set()\n")
if not card2.set('2', Suit.CLUBS):
print("BAD: incorrect value ('2', CLUBS) passed to Card's
set()\n")
print(str(card1) + "\n" + str(card2) + "\n" +str(card3) + "\n" + str(card4) + "\n"+ str(card5) + "\n")
card1 = card4
# test assignment operator for objects
print("after assigning card4 to card1:")
print(str(card1) + "\n" + str(card4)+ "\n")
INDENTATION:
OUTPUT: