Пример #1
0
 def __init__(self, name):
     self.cards = Cards()
     self.name = name
     self.winner = False
     self.cut = None
     self.played_dropped_card = False
     self.rest_value = None
Пример #2
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
Пример #3
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
Пример #4
0
 def test_receiving_valid_card(self):
     cards = Cards()
     number_of_cards_before_receiving_card = len(cards)
     valid_card = Card(4, "O")
     cards.receive_card(valid_card)
     number_of_cards_after_receiving_card = len(cards)
     assert cards[
         0] == valid_card and number_of_cards_after_receiving_card - number_of_cards_before_receiving_card == 1
Пример #5
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)
Пример #6
0
 def test_dropping_last_card_when_no_card_in_cards(self):
     cards = Cards()
     number_of_cards_before_dropping_card = len(cards)
     try:
         cards.drop_cards()
         has_raised_empty_list_of_cards_exception = False
     except game_exceptions.EmptyListOfCards:
         has_raised_empty_list_of_cards_exception = True
     except Exception:
         has_raised_empty_list_of_cards_exception = False
     number_of_cards_after_dropping_card = len(cards)
     assert has_raised_empty_list_of_cards_exception and number_of_cards_before_dropping_card == number_of_cards_after_dropping_card
Пример #7
0
 def test_receiving_a_non_card_element(self):
     cards = Cards()
     number_of_cards_before_receiving_card = len(cards)
     invalid_card = "string"
     try:
         cards.receive_card(invalid_card)
         has_rised_type_error = False
     except TypeError:
         has_rised_type_error = True
     except Exception:
         has_rised_type_error = False
     number_of_cards_after_receiving_card = len(cards)
     assert has_rised_type_error and number_of_cards_after_receiving_card == number_of_cards_before_receiving_card
Пример #8
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
Пример #9
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
Пример #10
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
Пример #11
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
Пример #12
0
 def _give_cards_to_players(game, deck):
     for i in range(len(game.players)):
         game.players[i].cards = Cards()
         for _ in range(game.CARDS_IN_HAND):
             game.players[i].cards.receive_card(deck.retrieve_card())
         game.players[i].rest_value = game.players[i].value_of_current_hand(
         )
     return game, deck
Пример #13
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
Пример #14
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"
Пример #15
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
Пример #16
0
 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
Пример #17
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'
Пример #18
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
Пример #19
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
Пример #20
0
class Player:
    def __init__(self, name):
        self.cards = Cards()
        self.name = name
        self.winner = False
        self.cut = None
        self.played_dropped_card = False
        self.rest_value = None

    def value_of_current_hand(self):
        cards_grouper = CardsGrouper()
        return cards_grouper.group_by_games_found(copy.deepcopy(
            self.cards)).value()

    def _analyze_card_impact_on_value(self, card):
        max_card = None
        cards_to_value = self.cards.get_copy()
        cards_to_value.receive_card(card)
        cards_grouper = CardsGrouper()
        card_group = cards_grouper.group_by_games_found(cards_to_value)
        for card_in_group in card_group.rest:
            if not max_card:
                max_card = card_in_group
            elif card_in_group.number > max_card.number:
                max_card = card_in_group
        if max_card is None:
            longest_game_index = np.argmax(
                [len(game) for game in card_group.games])
            max_card = card_group.games[longest_game_index][-1]
        card_group.drop_card_from_group(max_card)
        rest_value = card_group.value()
        potential_cut_type = card_group.find_type_of_cut()
        return ValueImpactAnalysis(
            rest_value=rest_value,
            less_valuable_card=max_card,
            potential_cut_type=potential_cut_type,
            has_value_improved=rest_value < self.rest_value)

    def play_cut(self, cut_type):
        self.cut = Cut(value=True, kind=cut_type)

    def has_cut(self):
        return self.cut.value if self.cut else False
Пример #21
0
 def _initialize_hand(self, style=None):
     if not hasattr(self, 'games_detector'):
         self.games_detector = GamesDetector()
     hand = Cards()
     if style == "one_same_number_game_one_ladder":
         hand.receive_card(Card(1, "O"))
         hand.receive_card(Card(1, "B"))
         hand.receive_card(Card(1, "C"))
         hand.receive_card(Card(3, "O"))
         hand.receive_card(Card(4, "O"))
         hand.receive_card(Card(5, "O"))
         hand.receive_card(Card(9, "O"))
     elif style == "two_same_number_game_no_ladder":
         hand.receive_card(Card(1, "O"))
         hand.receive_card(Card(1, "B"))
         hand.receive_card(Card(1, "C"))
         hand.receive_card(Card(3, "E"))
         hand.receive_card(Card(3, "O"))
         hand.receive_card(Card(3, "C"))
         hand.receive_card(Card(9, "O"))
     elif style == "no_same_number_game_one_ladder":
         hand.receive_card(Card(1, "O"))
         hand.receive_card(Card(2, "B"))
         hand.receive_card(Card(4, "C"))
         hand.receive_card(Card(5, "E"))
         hand.receive_card(Card(6, "E"))
         hand.receive_card(Card(7, "E"))
         hand.receive_card(Card(8, "E"))
     elif style == "no_game":
         hand.receive_card(Card(1, "O"))
         hand.receive_card(Card(2, "B"))
         hand.receive_card(Card(4, "C"))
         hand.receive_card(Card(5, "E"))
         hand.receive_card(Card(6, "E"))
         hand.receive_card(Card(10, "E"))
         hand.receive_card(Card(11, "C"))
     return hand
Пример #22
0
 def test_retrieving_card_when_no_card_left(self):
     deck = Deck()
     deck.cards_in_deck = Cards()
     deck.retrieve_card()
     assert len(deck.cards_in_deck) == 47
Пример #23
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