fork(1) download
  1.  
  2. from Cards import Hand, Deck
  3.  
  4.  
  5. class PokerHand(Hand):
  6. """Represents a poker hand."""
  7.  
  8. def suit_hist(self):
  9. """Builds a histogram of the suits that appear in the hand.
  10.  
  11. Stores the result in attribute suits.
  12. """
  13. self.suits = {}
  14. for card in self.cards:
  15. self.suits[card.suit] = self.suits.get(card.suit, 0) + 1
  16.  
  17. def rank_hist(self):
  18. self.ranks = {}
  19. for card in self.cards:
  20. self.ranks[card.rank] = self.ranks.get(card.rank, 0) + 1
  21.  
  22. def has_flush(self):
  23. """Returns True if the hand has a flush, False otherwise.
  24.  
  25. Note that this works correctly for hands with more than 5 cards.
  26. """
  27. self.suit_hist()
  28. for val in self.suits.values():
  29. if val >= 5:
  30. return True
  31. return False
  32.  
  33. def has_pair(self):
  34. self.rank_hist()
  35. for val in self.ranks.values():
  36. if val == 2: return True
  37. return False
  38.  
  39. def has_twopair(self):
  40. m = 0
  41. self.rank_hist()
  42. for val in self.ranks.values():
  43. if val == 2: m += 1
  44. if m == 2:
  45. return True
  46. else:
  47. return False
  48.  
  49. def has_three(self):
  50. self.rank_hist()
  51. for val in self.ranks.values():
  52. if val == 3: return True
  53. return False
  54.  
  55. def has_straight(self):
  56. #straight is a seuence of 5 cards or 4 cards + Ace
  57. #how to chek if it is a sequence?
  58. #ebaniy straight!
  59. chek = 0
  60. hand_ranks = []
  61. for card in self.cards:
  62. hand_ranks.append(card.rank)
  63.  
  64. h_ranks = sorted(set(hand_ranks))
  65.  
  66. for i in range(len(h_ranks)-1):
  67. if h_ranks[i]+1 == h_ranks[i+1]:
  68. chek += 1
  69. if chek == 5 and 1 in hand_ranks:
  70. return True
  71. if chek == 5:
  72. return True
  73. else:
  74. chek = 0
  75. return False
  76.  
  77. def has_four(self):
  78. self.suit_hist()
  79. for suit in self.suits.values():
  80. if suit == 4: return True
  81. return False
  82.  
  83. def has_straight_flush(self):
  84. return self.has_straight() and self.has_flush()
  85.  
  86. def has_fullhouse(self):
  87. return self.has_pair() and self.has_three()
  88.  
  89. def classify(self):
  90. d = {"straight flush": self.has_straight_flush(), "four": self.has_four(), "fullhouse": self.has_fullhouse(),
  91. "flush": self.has_flush(), "straight": self.has_straight(), "three": self.has_three(), "two pairs": self.has_twopair(),
  92. "pair": self.has_pair()}
  93. for key in d.keys():
  94. if d[key] == True:
  95. self.label = str(key)
  96. break
  97.  
  98. if __name__ == '__main__':
  99. # make a deck
  100. deck = Deck()
  101. deck.shuffle()
  102.  
  103. # deal the cards and classify the hands
  104. for i in range(7):
  105. hand = PokerHand()
  106. deck.move_cards(hand, 7)
  107. hand.sort()
  108.  
  109. print(hand)
  110. print("Has a flush:", hand.has_flush())
  111. print("Has a pair:", hand.has_pair())
  112. print("Has a pair of pairs:", hand.has_twopair())
  113. print("Has three of a kind:", hand.has_three())
  114. print("Has a full house:", hand.has_fullhouse())
  115. print("Has straight:", hand.has_straight())
  116. hand.classify()
  117. print(hand.label)
  118. print('')
  119.  
  120. # your code goes here
Runtime error #stdin #stdout #stderr 0.01s 27664KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Traceback (most recent call last):
  File "./prog.py", line 2, in <module>
ImportError: No module named 'Cards'