Created
May 18, 2016 04:13
-
-
Save Aslan11/dfd179a7e20216c94a22eb74890ef4cf to your computer and use it in GitHub Desktop.
Playing Card Class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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