Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import Card
- from Deck import *
- class Hand:
- """A hand is simply a list of 5 cards, dealt from the deck."""
- # Note that we have to pass in a Deck because we need
- # a place from which to draw the cards.
- def __init__(self, deck):
- """A hand is simply the first five cards in the deck, if there are five cards available. If not, return None."""
- self.mysuits = [0]*4
- self.myranks = [0]*14
- self.cards = []
- for i in range(5):
- card = deck.deal()
- self.cards.append(card)
- suit = Card.SUITDICTIONARY[card.getSuit()]
- self.mysuits[suit]+= 1
- self.myranks[card.getRank()]+= 1
- def __str__(self):
- """The string representation of the Hand, which is just the five cards."""
- result = ""
- for card in self.cards:
- result += str(card) + "\n"
- return result
- def hasRoyalFlush(self):
- 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
- def hasStraightFlush(self):
- return self.hasFlush() and self.hasStraight()
- def hasFourOfAKind(self):
- return self.myranks.count(4) == 1
- def hasFullHouse(self):
- return self.hasThreeOfAKind() and self.hasPair()
- def hasFlush(self):
- return self.mysuits.count(5) == 1
- def hasStraight(self):
- return False
- def hasThreeOfAKind(self):
- return self.myranks.count(3) == 1
- def hasTwoPair(self):
- return self.myranks.count(2) == 2
- def hasPair(self):
- """Return a boolean indicating whether the current hand contains a pair."""
- return self.myranks.count(2) == 1
- def evaluateHand(self):
- if self.hasRoyalFlush():
- return "Royal Flush"
- elif self.hasStraightFlush():
- return "Straight Flush"
- elif self.hasFourOfAKind():
- return "Four of a kind"
- elif self.hasFullHouse():
- return "Full House"
- elif self.hasFlush():
- return "Flush"
- elif self.hasStraight():
- return "Straight"
- elif self.hasThreeOfAKind():
- return "Three of a kind"
- elif self.hasTwoPair():
- return "Two pair"
- elif self.hasPair():
- return "Pair"
- else:
- return "Nothing"
Add Comment
Please, Sign In to add comment