Exemple #1
0
    def expand(self):
        if self.state.curr_cards.size() == 0 or self.state.next_cards.size(
        ) == 0:
            return False

        valid_hands = get_complete_hand(str(
            self.state.curr_cards)) if self.state.prev_hand.size(
            ) == 0 else get_beat_hand(str(self.state.prev_hand),
                                      str(self.state.curr_cards))
        valid_hands = [
            CardSet.parse_from("")
            if valid_hand == "x" else CardSet.parse_from(valid_hand)
            for valid_hand in valid_hands.split(" ")
        ]

        next_states = [
            MCTSState(1 - self.state.player, self.state.next_cards,
                      CardSet.diff(self.state.curr_cards, valid_hand),
                      valid_hand, False if valid_hand.nonempty() else True)
            for valid_hand in valid_hands
        ]

        self.children = [
            MCTSNode(self, next_state) for next_state in next_states
        ]

        pn_scores = self._get_pn_scores()
        vn_scores = self._get_vn_scores()
        for child, pn_score, vn_score in zip(self.children, pn_scores,
                                             vn_scores):
            child.P = pn_score
            child.V = vn_score

        return True
Exemple #2
0
 def valid_actions(self):
     actions = get_complete_hand(str(
         self.curr_cards)) if self.prev_hand.size() == 0 else get_beat_hand(
             str(self.prev_hand), str(self.curr_cards))
     actions = actions.split(" ")
     actions = [
         CardSet.parse_from("")
         if action == "x" else CardSet.parse_from(action)
         for action in actions
     ]
     return actions
Exemple #3
0
def select_action_by_rule(state):
    valid_hands = get_complete_hand(str(
        state.curr_cards)) if state.prev_hand.size() == 0 else get_beat_hand(
            str(state.prev_hand), str(state.curr_cards))
    valid_hands = valid_hands.split(" ")
    valid_hands = [
        CardSet.parse_from("")
        if valid_hand == "x" else CardSet.parse_from(valid_hand)
        for valid_hand in valid_hands
    ]

    # sorted by split card length
    left_cards = [
        CardSet.diff(state.curr_cards, valid_hand)
        for valid_hand in valid_hands
    ]
    split_lens = [
        0 if left_card.size() == 0 else len(
            get_simple_hand(str(left_card)).split(" "))
        for left_card in left_cards
    ]

    hands_with_lens = list(zip(valid_hands, split_lens))
    min_split_len = min(hands_with_lens, key=lambda tpl: tpl[1])
    min_split_len_hands = [
        hand_with_len[0] for hand_with_len in hands_with_lens
        if hand_with_len[1] == min_split_len[1]
    ]

    if len(min_split_len_hands) == 1:
        return min_split_len_hands[0]

    # sorted by left card length
    left_len = [(hand, 0 if CardSet.diff(state.curr_cards, hand).size() == 0
                 else len(str(CardSet.diff(state.curr_cards, hand))))
                for hand in min_split_len_hands]
    min_left_len = min(left_len, key=lambda tpl: tpl[1])
    min_left_len_hands = [
        hand for hand, length in left_len if length == min_left_len[1]
    ]

    if len(min_left_len_hands) == 1:
        return min_left_len_hands[0]

    # sorted by hand rank
    card_hands = [
        CardHand.parse_from('' if str(hand) == 'x' else str(hand))
        for hand in min_left_len_hands
    ]
    sorted_by_rank = sorted(card_hands, key=cmp_to_key(card_hand_compare))
    return sorted_by_rank[0].card_set