Guest User

Untitled

a guest
Dec 14th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.33 KB | None | 0 0
  1. import Card
  2. from Deck import *
  3.  
  4. class Hand:
  5.     """A hand is simply a list of 5 cards, dealt from the deck."""
  6.  
  7. # Note that we have to pass in a Deck because we need
  8. # a place from which to draw the cards.
  9.     def __init__(self, deck):
  10.         """A hand is simply the first five cards in the deck, if there are five cards available. If not, return None."""
  11.         self.mysuits = [0]*4
  12.         self.myranks = [0]*14
  13.         self.cards = []
  14.         for i in range(5):
  15.             card = deck.deal()
  16.             self.cards.append(card)
  17.             suit = Card.SUITDICTIONARY[card.getSuit()]
  18.             self.mysuits[suit]+= 1
  19.             self.myranks[card.getRank()]+= 1
  20.  
  21.     def __str__(self):
  22.         """The string representation of the Hand, which is just the five cards."""
  23.         result = ""
  24.         for card in self.cards:
  25.             result += str(card) + "\n"      
  26.         return result
  27.    
  28.     def hasRoyalFlush(self):
  29.         return self.hasFlush() and self.myranks[1] == 1 and self.myranks[10] == 1 and self.myranks[11] == 1 and self.myranks[12] == 1 and self.myranks[13] == 1
  30.  
  31.     def hasStraightFlush(self):
  32.         return self.hasFlush() and self.hasStraight()
  33.  
  34.     def hasFourOfAKind(self):
  35.         return self.myranks.count(4) == 1
  36.  
  37.     def hasFullHouse(self):
  38.         return self.hasThreeOfAKind() and self.hasPair()
  39.  
  40.     def hasFlush(self):
  41.         return self.mysuits.count(5) == 1
  42.  
  43.     def hasStraight(self):
  44.         return False
  45.  
  46.     def hasThreeOfAKind(self):
  47.         return self.myranks.count(3) == 1
  48.  
  49.     def hasTwoPair(self):
  50.         return self.myranks.count(2) == 2
  51.  
  52.     def hasPair(self):
  53.         """Return a boolean indicating whether the current hand contains a pair."""
  54.         return self.myranks.count(2) == 1
  55.  
  56.  
  57.     def evaluateHand(self):
  58.         if self.hasRoyalFlush():
  59.             return "Royal Flush"
  60.         elif self.hasStraightFlush():
  61.             return "Straight Flush"
  62.         elif self.hasFourOfAKind():
  63.         return "Four of a kind"
  64.         elif self.hasFullHouse():
  65.         return "Full House"
  66.     elif self.hasFlush():
  67.         return "Flush"
  68.     elif self.hasStraight():
  69.         return "Straight"
  70.     elif self.hasThreeOfAKind():
  71.         return "Three of a kind"
  72.     elif self.hasTwoPair():
  73.         return "Two pair"
  74.     elif self.hasPair():
  75.         return "Pair"
  76.         else:
  77.         return "Nothing"
Add Comment
Please, Sign In to add comment