Guest User

Untitled

a guest
May 21st, 2018
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. '''
  2. This program contains a class that represents a Card object(With a rank and suit like Ace of Spades) with helpful methods:
  3. getRank(), getSuit(), bjValue()
  4.  
  5. This program also includes multiple test cases.
  6. Author: Vikram Peddinti
  7. '''
  8.  
  9. class Card:
  10. # Dictionary to convert rank to words
  11. rankDict = {1: 'Ace', 2: 'Two', 3: 'Three', 4: 'Four', 5: 'Five', 6: 'Six', 7: 'Seven', 8: 'Eight', 9: 'Nine',
  12. 10: 'Ten', 11: 'Jack', 12: 'Queen', 13: 'King'}
  13. # Dictionary to convert suit to words
  14. suitDict = {'d': 'Diamonds', 'c': 'Clubs', 'h': 'Hearts', 's': 'Spades'}
  15.  
  16. #Inistialization method which takes in a rank & suit
  17. def __init__(self, rank, suit):
  18. self.rank = rank #Store the rank
  19. self.suit = suit #Store the suit
  20.  
  21. #Return the Rank value of the card
  22. def getRank(self):
  23. return self.rank
  24.  
  25. #Return the Suit value of the card
  26. def getSuit(self):
  27. return self.suit
  28.  
  29. #Return the BlackJack Value of the card
  30. def bjValue(self):
  31. if(self.rank == 1): #Checking if the card is an Ace -> Return 1
  32. return 1
  33. elif(self.rank > 10): #If a card is a face card -> Return 10
  34. return 10
  35. else:
  36. return self.rank #If the card is a number card return its value.
  37.  
  38. #Special Method to convert to string
  39. def __str__(self):
  40. return Card.rankDict[self.rank] + " of " + Card.suitDict[self.suit] #Return in the format of RANK of SUIT
  41.  
  42.  
  43. #Run only if this program is the executor
  44. if (__name__ == "__main__"):
  45.  
  46. #Loop through every single possiblity instead of hardcoding tests
  47. for rKey, rValue in Card.rankDict.items(): #The Key of the rank dictionary
  48. for sKey, sValue in Card.suitDict.items(): #The Key of the Suit dictionary
  49. #Run the basic test case for all possiblities
  50. tempCard = Card(rKey, sKey)
  51. print(tempCard)
  52. print(tempCard.getRank())
  53. print(tempCard.getSuit())
  54. print(tempCard.bjValue())
Add Comment
Please, Sign In to add comment