Example #1
0
    def cards_with_ladder(self, cards):
        kind_groups = cards.group_by_kind()
        cards_with_ladder = []
        for kind in kind_groups.keys():
            consecutive_cards = []
            last_card = Card()
            kind_groups[kind].sort()
            for index, card in enumerate(kind_groups[kind]):
                if not last_card.number:
                    last_card = card
                    consecutive_cards.append(last_card)
                elif card.number - last_card.number == 1:
                    last_card = card
                    consecutive_cards.append(card)
                    if index == len(kind_groups[kind]) - 1 and len(
                            consecutive_cards
                    ) >= self.MIN_EQUAL_CARD_NUMBER_FOR_GAME:
                        cards_with_ladder.append(
                            Cards.from_list_of_cards(consecutive_cards))
                else:
                    if len(consecutive_cards
                           ) >= self.MIN_EQUAL_CARD_NUMBER_FOR_GAME:
                        cards_with_ladder.append(
                            Cards.from_list_of_cards(consecutive_cards))
                    last_card = card
                    consecutive_cards = [last_card]

        return cards_with_ladder
Example #2
0
 def test_equality_detection(self):
     list_of_cards = [Card(10, 'O'), Card(3, 'C')]
     reversed_list_of_cards = list_of_cards.copy()
     reversed_list_of_cards.reverse()
     cards_a = Cards.from_list_of_cards(list_of_cards)
     cards_b = Cards.from_list_of_cards(reversed_list_of_cards)
     assert cards_a == cards_b
Example #3
0
 def test_dropping_cards_that_exists_in_cards(self):
     list_of_cards_to_drop = [Card(1, "E"), Card(4, "O")]
     cards_to_drop = Cards.from_list_of_cards(list_of_cards_to_drop)
     list_of_cards = [Card(3, "E"), Card(8, "O")] + list_of_cards_to_drop
     cards = Cards.from_list_of_cards(list_of_cards)
     number_of_cards_before_dropping_card = len(cards)
     dropped_cards = cards.drop_cards(cards_to_drop)
     number_of_cards_after_dropping_card = len(cards)
     assert dropped_cards == cards_to_drop and number_of_cards_before_dropping_card - number_of_cards_after_dropping_card == len(
         cards_to_drop)
Example #4
0
 def cards_with_same_number(self, cards):
     same_number_cards = []
     available_cards = cards
     while available_cards:
         next_card = available_cards[0]
         equal_cards = Cards.from_list_of_cards([
             card for card in available_cards
             if card.number == next_card.number
         ])
         if len(equal_cards) >= self.MIN_EQUAL_CARD_NUMBER_FOR_GAME:
             same_number_cards.append(equal_cards)
         # Delete processed cards from available cards
         available_cards = Cards.from_list_of_cards(
             [card for card in available_cards if card not in equal_cards])
     return same_number_cards
Example #5
0
 def test_dropping_cards_that_do_not_exist_in_cards(self):
     list_of_cards_to_drop = [Card(3, "E"), Card(5, "O")]
     cards_to_drop = Cards.from_list_of_cards(list_of_cards_to_drop)
     list_of_cards = [Card(3, "E"), Card(9, "O")]
     cards = Cards.from_list_of_cards(list_of_cards)
     number_of_cards_before_dropping_card = len(cards)
     try:
         cards.drop_cards(cards_to_drop)
         has_raised_card_not_found_exception = False
     except game_exceptions.CardNotFound:
         has_raised_card_not_found_exception = True
     except Exception:
         has_raised_card_not_found_exception = False
     number_of_cards_after_dropping_card = len(cards)
     assert has_raised_card_not_found_exception and number_of_cards_before_dropping_card == number_of_cards_after_dropping_card
Example #6
0
 def test_group_by_kind(self):
     ten_of_basto = Card(10, 'B')
     nine_of_gold = Card(9, 'O')
     six_of_cup = Card(6, 'C')
     four_of_basto = Card(4, 'B')
     twelve_of_gold = Card(12, 'O')
     eleven_of_gold = Card(11, 'O')
     cards = Cards.from_list_of_cards([
         ten_of_basto, nine_of_gold, six_of_cup, four_of_basto,
         twelve_of_gold, eleven_of_gold
     ])
     kind_groups = cards.group_by_kind()
     for kind in ['B', 'O', 'C']:
         if kind == "B":
             cards_were_correctly_grouped = ten_of_basto in kind_groups[kind] and four_of_basto in kind_groups[kind] \
                                            and len(kind_groups[kind]) == 2
         elif kind == "O":
             cards_were_correctly_grouped = twelve_of_gold in kind_groups[kind] and eleven_of_gold in kind_groups[kind] \
                                            and nine_of_gold in kind_groups[kind] and len(kind_groups[kind]) == 3
         elif kind == "C":
             cards_were_correctly_grouped = six_of_cup in kind_groups[
                 kind] and len(kind_groups[kind]) == 1
         else:
             cards_were_correctly_grouped = False
     assert cards_were_correctly_grouped
Example #7
0
 def test_dropping_last_card_when_cards_exist(self):
     list_of_cards = [Card(2, "E"), Card(1, "B"), Card(12, "O")]
     cards = Cards.from_list_of_cards(list_of_cards)
     number_of_cards_before_dropping_card = len(cards)
     dropped_card = cards.drop_cards()
     number_of_cards_after_dropping_card = len(cards)
     assert dropped_card == list_of_cards[
         -1] and number_of_cards_before_dropping_card - number_of_cards_after_dropping_card == 1
Example #8
0
 def test_building_cards_from_list(self):
     all_card_items_belong_to_cards = True
     list_of_cards = [Card(10, "B"), Card(5, "E"), Card(12, "C")]
     cards = Cards.from_list_of_cards(list_of_cards)
     for index, card in enumerate(cards):
         if card != list_of_cards[index]:
             all_card_items_belong_to_cards = False
     assert isinstance(cards, Cards) and all_card_items_belong_to_cards
 def test_value_computation_when_zero_rest(self):
     cards = Cards.from_list_of_cards([
         Card(4, 'E'),
         Card(6, 'B'),
         Card(3, 'E'),
         Card(5, 'E'),
         Card(6, 'C'),
         Card(6, 'E'),
         Card(6, 'O')
     ])
     grouped_cards = self._get_cards_group(cards)
     assert grouped_cards.value() == -10
Example #10
0
 def test_detection_of_normal_cut(self):
     cards = Cards.from_list_of_cards([
         Card(4, 'E'),
         Card(6, 'B'),
         Card(3, 'E'),
         Card(5, 'E'),
         Card(6, 'C'),
         Card(6, 'E'),
         Card(8, 'O')
     ])
     grouped_cards = self._get_cards_group(cards)
     assert grouped_cards.find_type_of_cut() == "normal_cut"
Example #11
0
 def test_detection_of_conga_cut(self):
     cards = Cards.from_list_of_cards([
         Card(4, 'E'),
         Card(2, 'E'),
         Card(3, 'E'),
         Card(5, 'E'),
         Card(7, 'E'),
         Card(6, 'E'),
         Card(8, 'E')
     ])
     grouped_cards = self._get_cards_group(cards)
     assert grouped_cards.find_type_of_cut() == 'conga_cut'
Example #12
0
 def test_sorting(self):
     correctly_sorted = True
     list_of_cards = [Card(11, "B"), Card(4, "E"), Card(7, "C")]
     cards = Cards.from_list_of_cards(list_of_cards)
     cards.sort()
     for index, card in enumerate(cards):
         if (index == 0 and card != list_of_cards[1]) or (
                 index == 1 and card != list_of_cards[2]) or (
                     index == 2 and card != list_of_cards[0]):
             correctly_sorted = False
             break
     assert correctly_sorted
Example #13
0
 def test_detection_of_one_ladder_and_one_same_number_game_with_rest(self):
     cards = Cards.from_list_of_cards([
         Card(1, 'O'),
         Card(6, 'B'),
         Card(3, 'O'),
         Card(2, 'O'),
         Card(6, 'C'),
         Card(6, 'E'),
         Card(10, 'O')
     ])
     cards_grouper = CardsGrouper()
     grouped_cards = cards_grouper.group_by_games_found(cards)
     assert len(grouped_cards.games) == 2 and grouped_cards.rest
Example #14
0
 def test_detection_of_one_same_number_and_no_other_game(self):
     cards = Cards.from_list_of_cards([
         Card(4, 'E'),
         Card(7, 'B'),
         Card(3, 'E'),
         Card(6, 'E'),
         Card(6, 'C'),
         Card(6, 'B'),
         Card(6, 'O')
     ])
     cards_grouper = CardsGrouper()
     grouped_cards = cards_grouper.group_by_games_found(cards)
     assert len(grouped_cards.games) == 1 and grouped_cards.rest
Example #15
0
 def test_inequality_detection(self):
     list_of_cards_a = [Card(10, 'O'), Card(3, 'C')]
     list_of_cards_b = [Card(10, 'O'), Card(4, 'C')]
     cards_a = Cards.from_list_of_cards(list_of_cards_a)
     cards_b = Cards.from_list_of_cards(list_of_cards_b)
     assert cards_a != cards_b