main.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. class CardValueException(Exception):
  2. """
  3. Create a new exception class
  4. """
  5. def __init__(self, value):
  6. """
  7. Constructor : creates a new card value exception
  8. @param string Value to display
  9. """
  10. self.value = str(value)
  11. def __str__(self):
  12. """
  13. Display the error message
  14. """
  15. return repr(self.value)
  16. class HandException(Exception):
  17. """
  18. Create a new exception class
  19. """
  20. def __init__(self, value):
  21. """
  22. Constructor : creates a new hand exception
  23. @param string Value to display
  24. """
  25. self.value = str(value)
  26. def __str__(self):
  27. """
  28. Display the error message
  29. """
  30. return repr(self.value)
  31. class Card(object):
  32. """
  33. Definition of a card object
  34. """
  35. ### static properties
  36. # card type
  37. headType = "Head"
  38. asType = "As"
  39. defaultType = "default"
  40. # associaative table
  41. assocStringType = {
  42. "As" : [ "A" ],
  43. "Head" : [ "K", "Q", "J" ],
  44. "default" : [ str(i) for i in reversed(range(10)) ]
  45. }
  46. #available values
  47. availableStringValues = ["A", "K", "Q", "J"] + assocStringType[defaultType]
  48. def __init__(self, valueString):
  49. """
  50. Constructor : creates a new card following its string value
  51. @param string valueString
  52. @raise CardValueException if unexecpected string value is given
  53. """
  54. self.__valueString = valueString
  55. self.__type = self.__defineCardType()
  56. self.__value = self.__defineCardValue()
  57. def __defineCardType(self):
  58. """
  59. Using an associative table define if the card is either an head, an as or a normal card
  60. """
  61. if not self.__valueString in Card.availableStringValues:
  62. raise CardValueException("This value doesn't exist, please check your code")
  63. for cardType in Card.assocStringType.keys():
  64. if self.__valueString in Card.assocStringType[cardType]:
  65. return cardType
  66. def __defineCardValue(self):
  67. """
  68. Following is string value define is numeric value
  69. A card could have more than one value, ie : As => 1 or 11
  70. In this case the value will be a list, ie As [1, 11]
  71. Otherwise an integer
  72. """
  73. if self.__type == Card.asType:
  74. return [1, 11]
  75. if self.__type == Card.headType:
  76. return 10
  77. return int(self.__valueString)
  78. def getType(self):
  79. """
  80. Returns the card type
  81. """
  82. return self.__type
  83. def getValue(self):
  84. """
  85. Returns the card type
  86. """
  87. return self.__value
  88. def getValueString(self):
  89. """
  90. Returns the card type
  91. """
  92. return self.__valueString
  93. class Hand(object):
  94. """
  95. Definition of a hand object
  96. """
  97. def __init__(self):
  98. """
  99. Constructror : creates a new empty hand
  100. """
  101. self.__cards = []
  102. def addCard(self, card):
  103. """
  104. Appends a card to hand
  105. @param Card A Card instance
  106. """
  107. if type(card) != Card:
  108. raise HandException("You try to append an unexecpected object, the object a Card instance")
  109. self.__cards.append(card)
  110. def addCards(self, *args):
  111. """
  112. Append a bunch of cards to hand
  113. """
  114. for card in args:
  115. self.addCard(card)
  116. def addUp(self):
  117. """
  118. Returns the sum of all card value
  119. if there are some as cards in the hand
  120. returns a list of total
  121. """
  122. #retrieve as cards contents by the hand
  123. indexAsCards = [ self.__cards.index(card) for card in self.__cards if card.getType() == Card.asType ]
  124. if len(indexAsCards):
  125. indexAsCards = indexAsCards[0]
  126. for asValue in self.__cards[indexAsCards].getValue():
  127. yield asValue + sum( [card.getValue() for index,card in enumerate(self.__cards) if index != indexAsCards ] )
  128. return
  129. yield sum( [card.getValue() for card in self.__cards ])
  130. def detectCouples(self):
  131. """
  132. Retrieve the list of coupled cards
  133. """
  134. cards = [ card.getValueString() for card in self.__cards ]
  135. return dict((x,cards.count(x)) for x in set(cards))
  136. hand = Hand()
  137. hand.addCards(Card('3'), Card('2'), Card('4'), Card('3'))
  138. for val in hand.addUp():
  139. print val
  140. print hand.detectCouples()