Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- This program contains a class that represents a Card object(With a rank and suit like Ace of Spades) with helpful methods:
- getRank(), getSuit(), bjValue()
- This program also includes multiple test cases.
- Author: Vikram Peddinti
- '''
- class Card:
- # Dictionary to convert rank to words
- rankDict = {1: 'Ace', 2: 'Two', 3: 'Three', 4: 'Four', 5: 'Five', 6: 'Six', 7: 'Seven', 8: 'Eight', 9: 'Nine',
- 10: 'Ten', 11: 'Jack', 12: 'Queen', 13: 'King'}
- # Dictionary to convert suit to words
- suitDict = {'d': 'Diamonds', 'c': 'Clubs', 'h': 'Hearts', 's': 'Spades'}
- #Inistialization method which takes in a rank & suit
- def __init__(self, rank, suit):
- self.rank = rank #Store the rank
- self.suit = suit #Store the suit
- #Return the Rank value of the card
- def getRank(self):
- return self.rank
- #Return the Suit value of the card
- def getSuit(self):
- return self.suit
- #Return the BlackJack Value of the card
- def bjValue(self):
- if(self.rank == 1): #Checking if the card is an Ace -> Return 1
- return 1
- elif(self.rank > 10): #If a card is a face card -> Return 10
- return 10
- else:
- return self.rank #If the card is a number card return its value.
- #Special Method to convert to string
- def __str__(self):
- return Card.rankDict[self.rank] + " of " + Card.suitDict[self.suit] #Return in the format of RANK of SUIT
- #Run only if this program is the executor
- if (__name__ == "__main__"):
- #Loop through every single possiblity instead of hardcoding tests
- for rKey, rValue in Card.rankDict.items(): #The Key of the rank dictionary
- for sKey, sValue in Card.suitDict.items(): #The Key of the Suit dictionary
- #Run the basic test case for all possiblities
- tempCard = Card(rKey, sKey)
- print(tempCard)
- print(tempCard.getRank())
- print(tempCard.getSuit())
- print(tempCard.bjValue())
Add Comment
Please, Sign In to add comment