import random ######################################### # Card Class ######################################### class Card: def __init__(self, face, suit, value=1): '''Create a new card face: single chr for face value ['A','1',...'J','Q','K'] suit: single chr ['H','D','S','C'] value: point value for card ''' self.face = face.upper()[0] self.suit = suit.upper()[0] self.value = value def is_black(self): '''Return True if the card is black''' return self.suit == 'S' or self.suit == 'C' def is_facecard(self): '''Return True if the card is ['A','J','Q','K'] ''' return not self.face.isdigit() def __eq__(self, other): ''' Magic method called when evaluating: card1 == card2''' return self.face == other.face and self.suit == other.suit def __ne__(self, other): ''' Magic method called when evaluating: card1 != card2''' return not self == other # equivalent to not self.__eq__(other) def __lt__(self, other): ''' Magic method called when evaluating: card1 < card2''' return self.value < other.value def __le__(self, other): ''' Magic method called when evaluating: card1 <= card2''' return self < other or self.face == other.face def __gt__(self, other): ''' Magic method called when evaluating: card1 > card2''' return self.value > other.value def __ge__(self, other): ''' Magic method called when evaluating: card1 >= card2''' return self > other or self.face == other.face def __str__(self): ''' Magic method called when evaluating: str(card)''' return self.suit + self.face ######################################### # Hand Class ######################################### class Hand: def __init__(self, cards): ''' cards is a list of Card objects ''' self.cards = cards def value(self): ''' Return the total point value for all cards in the hand''' #return sum([c.value for c in self.cards]) total = 0 for card in self.cards: total += card.value return total def has_pair(self): '''Returns True if hand has a pair''' ''' for i, c in enumerate(self.cards): for c2 in self.cards[i + 1:]: if c.face == c2.face: return True return False ''' num_cards = len(self.cards) # or len(self) for i in range(num_cards): for j in range(i + 1, num_cards): if self.cards[i].face != self.cards[j].face: return False return True def has_flush(self): '''Returns True if hand is all of the same suit''' first_suit = self.cards[0].suit for c in self.cards[1:]: if c.suit != first_suit: return False return True def __str__(self): ''' Magic method called when evaluating: str(hand)''' #return str([str(c) for c in self.cards]) result = "[ " for card in self.cards: result = result + str(card) + " " result += "]" return result def __len__(self): ''' Magic method called when evaluating: len(hand)''' return len(self.cards) ######################################### # Deck Class ######################################### class Deck: def __init__(self, cards): '''cards is a list of Card objects''' self.cards = cards def shuffle(self): '''Randomize the order of internal cards list''' random.shuffle(self.cards) def peek(self, n=5): '''Peek at the top n cards''' return Hand(self.cards[:n]) def deal(self, n=1): hand_cards = self.cards[0:n] del self.cards[0:n] return Hand(hand_cards) def __str__(self): ''' Magic method called when evaluating: str(deck)''' return str([str(c) for c in self.cards]) def __len__(self): '''Magic method to be used when evaluation: len(deck)''' return len(self.cards)