def add_cards(self, new_cards, use_deck): """ add a set of cards to a hand, taking the cards from the given deck, each card in the set must be a card object, card name, or canonical index """ to_add = new_cards #if we get a string, make it into a set of cards if (isinstance(new_cards, str)): to_add = [] card_str = new_cards while len(card_str) > 1: to_add.append(card_str[:2]) card_str = card_str[2:] for x in to_add: if (isinstance(x, Card) and (x.is_valid())): new_card = x elif isinstance(x, int): if ((x > -1) and (x < 52)): new_card = Card(x) else: return -1 elif isinstance(x, str): new_card = Card(x) if new_card == -1: return -1 else: return -1 # check to see if this new card is already in the hand new_index = new_card.get_index() for test_card in self.cards: if (test_card.get_index() == new_index): return -1 # if this card is in the deck, add it to the hand if (use_deck == None) or (use_deck.take_card(new_card) != -1): self.cards.append(new_card) self.suits[new_card.suit] += 1 self.values[new_card.value] += 1 self.vals_in_suit[new_card.suit] |= 1 << new_card.value self.all_values |= 1 << new_card.value
def test_card(self): x = Card('Ah') #case 1: make sure that when we set a card, the value is consistent self.assertTrue(x.short_name() == 'Ah') self.assertTrue(x.single_name() == 'Y') #case 2: make sure that when we set a card, the suit and value match y = Card('Ad') self.assertTrue(x.value == y.value) #case 3: make sure that Ah and Ad have different suits self.assertFalse(x.suit == y.suit) #case 4: test setting the card by long name y.set_value_by_name('Deuce') y.set_suit_by_name('Hearts') self.assertTrue(y.short_name() == '2h')
def get_lowest_playable_card(self): '''Returns card instance for the lowest playable card''' lowest_playable_card = None lowest_rank = self.get_lowest_card_rank() if lowest_rank: lowest_playable_rank = lowest_rank - 1 if lowest_playable_rank in RANKS: lowest_playable_card = Card(lowest_playable_rank, self.suit) return lowest_playable_card
def calculate_valid_cards(self): '''Calculates which cards can be played on layout.''' # If layout is not active then only card of rank 7 is valid if len(self.cards) == 0: valid_cards = [Card(7, self.suit)] else: valid_cards = [ self.get_highest_playable_card(), self.get_lowest_playable_card() ] return [c for c in valid_cards if c is not None]
def get_highest_playable_card(self): '''Returns card instance for the highest playable card''' highest_playable_card = None highest_rank = self.get_highest_card_rank() if highest_rank: highest_playable_rank = highest_rank + 1 if highest_playable_rank in RANKS: highest_playable_card = Card(highest_playable_rank, self.suit) return highest_playable_card
def __init__(self, command_str, curr_layouts): '''Provides validation of user commands''' self.pass_cmd = False if command_str == PASS_CMD: self.pass_cmd = True self.card = None self.layout = None else: # Separate command string and pass to relevant constructor self.card = Card.create_from_user_cmd(command_str.split('L')[0]) self.layout = curr_layouts.get_layout_by_id( int(command_str.split('L')[1]) )