# # hand.py # # CS 111, Boston University # from card import * class Hand: """ a class for objects that represent a single hand of cards """ def __init__(self): """ constructor for Hand objects """ self.cards = [] def __repr__(self): """ returns a string representation of the called Hand object (self) """ return str(self.cards) def add_card(self, card): """ add the specified Card object (card) to the list of cards for the called Hand object (self) """ self.cards += [card] def num_cards(self): """ returns the number of cards in the called Hand (self) """ return len(self.cards) def get_value(self): """ returns the total value of the cards belonging to the called Hand object (self) """ value = 0 for card in self.cards: value += card.get_value() return value def has_any(self, rank): """ returns True if the called Hand (self) has at least one instance of the specified rank, and False otherwise """ for card in self.cards: if card.rank == rank: return True # if we get here, the hand does not have any instances of the rank return False