Ejemplo n.º 1
0
    def test_melding(self):
        hand_text = [
            '9H', 'AC', 'TH', '3C', '3D', '7C', 'QH', '3H', '8C', '8D', '4D',
            '7H', '8S', '5H', '4H', 'AS', 'TD', '3S', '2S', 'AH'
        ]
        hand = [utils.card_from_text(x) for x in hand_text]

        # check
        all_set_melds_text = [['3C', '3D', '3H', '3S'], ['8C', '8D', '8S'],
                              ['AC', 'AS', 'AH'], ['3D', '3H', '3S'],
                              ['3C', '3H', '3S'], ['3C', '3D', '3S'],
                              ['3C', '3D', '3H']]
        all_set_melds_text = set([frozenset(x) for x in all_set_melds_text])
        all_set_melds = get_all_set_melds(hand)
        self.assertEqual(
            all_set_melds_text,
            set([
                frozenset([str(card) for card in meld_pile])
                for meld_pile in all_set_melds
            ]))

        # check
        all_run_melds_text = [['AS', '2S', '3S'], ['3H', '4H', '5H']]
        all_run_melds = get_all_run_melds(hand)
        self.assertEqual(all_run_melds_text,
                         [[str(card) for card in meld_pile]
                          for meld_pile in all_run_melds])

        # check
        meld_clusters = get_meld_clusters(hand=hand)
        self.assertEqual(len(meld_clusters), 36)
Ejemplo n.º 2
0
def can_gin(game: GinRummyGame) -> bool:
    result = False
    last_action = game.get_last_action()
    last_action_type = type(last_action)
    if last_action_type is DrawCardAction or last_action_type is PickUpDiscardAction:
        current_player = game.get_current_player()
        hand = current_player.hand
        going_out_deadwood_count = game.settings.going_out_deadwood_count
        meld_clusters = melding.get_meld_clusters(hand=hand,
                                                  going_out_deadwood_count=going_out_deadwood_count,
                                                  is_going_out=True)
        if meld_clusters:
            deadwood_counts = [utils.get_deadwood_count(hand, meld_cluster) for meld_cluster in meld_clusters]
            result = min(deadwood_counts) == 0
    return result
Ejemplo n.º 3
0
def get_knock_cards(game: GinRummyGame) -> List[Card]:
    """
    :param game: GinRummyGame
    :return: list[Card] of cards that player can knock with
    """
    knock_cards = set()
    last_action = game.get_last_action()
    last_action_type = type(last_action)
    if last_action_type is DrawCardAction or last_action_type is PickUpDiscardAction:
        current_player = game.get_current_player()
        hand = current_player.hand
        going_out_deadwood_count = game.settings.going_out_deadwood_count
        meld_clusters = melding.get_meld_clusters(hand=hand,
                                                  going_out_deadwood_count=going_out_deadwood_count,
                                                  is_going_out=True)
        deadwood_cluster = [utils.get_deadwood(hand, meld_cluster) for meld_cluster in meld_clusters]
        for deadwood in deadwood_cluster:
            for card in deadwood:
                knock_cards.add(card)
    return list(knock_cards)
Ejemplo n.º 4
0
 def _get_best_discards(discard_action_events, state) -> List[Card]:
     best_discards = []  # type: List[Card]
     final_deadwood_count = 999
     env_hand = state['obs'][0]
     hand = utils.decode_cards(env_cards=env_hand)
     for discard_action_event in discard_action_events:
         discard_card = discard_action_event.card
         next_hand = [card for card in hand if card != discard_card]
         meld_clusters = melding.get_meld_clusters(hand=next_hand)
         deadwood_counts = []
         for meld_cluster in meld_clusters:
             deadwood_count = utils.get_deadwood_count(hand=next_hand, meld_cluster=meld_cluster)
             deadwood_counts.append(deadwood_count)
         best_deadwood_count = min(deadwood_counts,
                                   default=utils.get_deadwood_count(hand=next_hand, meld_cluster=[]))
         if best_deadwood_count < final_deadwood_count:
             final_deadwood_count = best_deadwood_count
             best_discards = [discard_card]
         elif best_deadwood_count == final_deadwood_count:
             best_discards.append(discard_card)
     return best_discards