Skip to content

Instantly share code, notes, and snippets.

@Aslan11
Created May 18, 2016 04:13
Show Gist options
  • Select an option

  • Save Aslan11/dfd179a7e20216c94a22eb74890ef4cf to your computer and use it in GitHub Desktop.

Select an option

Save Aslan11/dfd179a7e20216c94a22eb74890ef4cf to your computer and use it in GitHub Desktop.
Playing Card Class
class PlayingCard:
'Common base class for all playing cards'
deckCount = 0
cardNameMap = {
1: 'Ace',
11: 'Jack',
12: 'Queen',
13: 'King'
}
# type is an integer between 1 and 13
# suit is a string (later we could implement a suitNameMap if necessary)
def __init__(self, type, suit):
self.type = type
self.suit = suit
PlayingCard.deckCount += 1
def name(self):
if (self.type > 1 and self.type <= 10):
return self.type
else:
return PlayingCard.cardNameMap[self.type]
def properName(self):
return "{0} of {1}".format(self.name(), self.suit)
def displayCount(self):
return "Total Playing Cards: {0}".format(PlayingCard.deckCount)
# This would create first object of PlayingCard class
cardOne = PlayingCard(13, "Hearts")
cardTwo = PlayingCard(1, "Spades")
cardThree = PlayingCard(7, "Clubs")
# cards have an attribute of type
print cardOne.type
# they have a function of name
print cardOne.name()
# they have another function properName
print cardOne.properName()
print cardTwo.properName()
print cardThree.properName()
# they also have a displayCount
print cardThree.displayCount()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment