def test_get_same_value(hands_generator, random_card_list, card_deck): for _ in range(1000): card_deck = Card_Deck() card_deck.shuffle() one_pair = hands_generator.get_same_value(random_card_list, card_deck, type_of_pair="one") one_pair_unique = set([x.value for x in one_pair]) one_pair_suit = set([x.suit for x in one_pair]) assert len(one_pair) == 2, "One pair must contain two cards!" assert len(one_pair_unique) == 1, "One pair must contain only 1 unique value!" assert len(one_pair_suit) == 2, "One pair must contain 2 differnt suits!" two_pair = hands_generator.get_same_value(random_card_list, card_deck, type_of_pair="two") two_pair_unique = set([x.value for x in two_pair]) two_pair_suits = set([x.suit for x in two_pair]) assert len(two_pair) == 4, "Two pairs must contain 4 cards!" assert len(two_pair_unique) == 2, "Two pair must contain 2 unique values!" assert len(two_pair_suits) >= 2, "Two pair must contain at least 2 different suits!" three_of_a_kind = hands_generator.get_same_value(random_card_list, card_deck, "three") three_of_a_kind_unique = set([x.value for x in three_of_a_kind]) three_of_a_kind_suits = set([x.suit for x in three_of_a_kind]) assert len(three_of_a_kind) == 3, "Three of a kind must contain 3 cards!" print(three_of_a_kind) assert len(three_of_a_kind_unique) == 1, "Three of a kind must contain only 1 unique value!" assert len(three_of_a_kind_suits) == 3, "Three of a kind must contain 3 suits!" four_of_a_kind = hands_generator.get_same_value(random_card_list, card_deck, "four") four_of_a_kind_unique = set([x.value for x in four_of_a_kind]) four_of_a_kind_suit = set([x.suit for x in four_of_a_kind]) assert len(four_of_a_kind) == 4, "Four of a kind must contain 4 cards!" assert len(four_of_a_kind_unique) == 1, "Four of a kind must contain only 1 unique value" assert len(four_of_a_kind_suit) == 4, "Four of a kind must contain 4 suits!"
def test_get_straight(hands_generator, random_card_list): for _ in range(1000): card_deck = Card_Deck() card_deck.shuffle() straight = hands_generator.get_straight(random_card_list, card_deck) assert len(straight) == 7, f"Must only generate 7 cards. Got {straight} of length {len(straight)}." count = 1 sorted_straight = sorted(straight, key=lambda x: x.value) current_card = sorted_straight[0] for idx, card in enumerate(sorted_straight): if idx > 0 and card.value - 1 == current_card.value: current_card = card count += 1 assert count >= 5, f"Cards: {sorted_straight}. Length of cards: {len(sorted_straight)}"
def get_straight(straights: List[Card_Deck.Card], cards: Card_Deck, greater: bool=True) -> List[Card_Deck.Card]: """Get 5 consecutive cards in a deck with a given card Args: straights (List[Card_Deck.Card]): first card cards: (List[Card_Deck.Card]): deck of card greater (bool, optional): whether to get bigger or lower consecutive numbers. Defaults to True. Returns: Card_Deck: a list of consecutive numbers """ if greater: while len(straights) <= 5: current_count = straights[-1].value + 1 for idx, card in enumerate(cards.cards): if card.value == current_count: straights.append(card) break else: while len(straights) <= 5: if straights[0].value == 1: current_count = 13 else: current_count = straights[-1].value - 1 for idx, card in enumerate(cards.cards): if card.value == current_count: straights.append(card) break for _ in range(2): straights.append(cards.deal())
def get_straight(self, straights: List[Card_Deck.Card], cards: Card_Deck, greater: bool = True) -> List[Card_Deck.Card]: """Get 7 cards that consist of 5 consecutive cards in a deck with a given card Args: straights (List[Card_Deck.Card]): first card cards: (List[Card_Deck.Card]): deck of card greater (bool, optional): whether to get bigger or lower consecutive numbers. Defaults to True. Returns: Card_Deck: a list of consecutive numbers """ current_value = straights[0].value if 13 - current_value <= 4: max_val = 13 else: max_val = current_value + 5 if current_value - 4 <= 0: min_val = 0 else: min_val = current_value - 4 initial_value_to_pick = np.random.randint(min_val, max_val, 1)[0] if initial_value_to_pick > current_value: values_to_pick = list( range(initial_value_to_pick - 4, initial_value_to_pick + 1)) logger.debug(f"Lower pick from: {values_to_pick}") straights = [ cards.Card(x, np.random.choice(list(cards.SUITS.keys()))) for x in values_to_pick ] else: values_to_pick = list( range(initial_value_to_pick, initial_value_to_pick + 5)) logger.debug(f"Higher pick from: {values_to_pick}") straights = [ cards.Card(x, np.random.choice(list(cards.SUITS.keys()))) for x in values_to_pick ] while len(straights) < 7: potential_card = cards.deal() if potential_card.value not in values_to_pick: straights.append(potential_card) values_to_pick.append(potential_card.value) return straights
def get_same_suits(same_suits: List[Card_Deck.Card], cards: Card_Deck)-> List[Card_Deck.Card]: """Return a list that contains five cards of the same suit Args: same_suits (List[Card_Deck.Card]): list that contain the first card cards (Card_Deck): deck of cards Returns: List[Card_Deck.Card]: list that contain at least 5 cards of the same suit """ first_suit: str = same_suits[0].suit while len(same_suits) < 5: for idx, card in enumerate(cards.cards): if card.suit == first_suit: same_suits.append(card) for _ in range(2): same_suits.append(cards.deal()) return same_suits
def card_deck(): card_deck = Card_Deck() return card_deck
idx += 1 continue if current_card.value < first_card[ 0].value: #TODO: Compare with the minimum value of first_card straight_lower += 1 if straight_upper >= 4: idx += 1 continue if current_card.value > first_card[-1].value: pass values.add(current_card.value) suits[current_card.suit] += 1 first_card.append(current_card) idx += 1 #TODO: Finish generating just high cards return first_card #======================================= END ============================================================================= if __name__ == "__main__": args, _ = get_args() if args.loglvl is not None: logger.loglevel(LOGLVL[args.loglvl]) hand_generator = Hands_Generator() card_deck = Card_Deck() for _ in range(1000): random_ind = np.random.randint(0, 52, 1)[0] random_card = card_deck.cards[random_ind] straights = hand_generator.get_straight([random_card], card_deck) assert len(straights) == 5
def test_shuffle_seed(card_deck): test_cards1 = card_deck.cards.copy() card_deck1 = Card_Deck() assert test_cards1 != card_deck.shuffle(seed=124).cards, "Reimplement your seed in shuffle!"