예제 #1
0
파일: dealer.py 프로젝트: sweedp/rlcard
    def determine_role(self, players):
        ''' Determine landlord and peasants according to players' hand

        Args:
            players (list): list of DoudizhuPlayer objects

        Returns:
            int: landlord's player_id
        '''
        # deal cards
        self.shuffle()
        self.deal_cards(players)
        players[0].role = 'landlord'
        self.landlord = players[0]
        players[1].role = 'peasant'
        players[2].role = 'peasant'
        #players[0].role = 'peasant'
        #self.landlord = players[0]
        self.landlord_agent = 0
        ## determine 'landlord'
        if(self.landlord_score==True):

            max_score = self.get_landlord_score(
                cards2str(self.landlord.current_hand))

            counter = 1
            for player in players[1:]:
                player.role = 'peasant'
                score = self.get_landlord_score(
                    cards2str(player.current_hand))
                if score > max_score:
                    max_score = score
                    self.landlord = player
                    if(counter==1):

                        self.landlord_agent = 1
                    if(counter==2):
                        self.landlord_agent = 2
                counter += 1

            self.landlord.role = 'landlord'

        # give the 'landlord' the  three cards
        self.landlord.current_hand.extend(self.deck[-3:])
        self.landlord.current_hand.sort(key=functools.cmp_to_key(doudizhu_sort_card))
        self.landlord.initial_hand = cards2str(self.landlord.current_hand)

        return self.landlord.player_id
예제 #2
0
    def shuffle_other_player_cards(self, env: Env):
        cur_player_id = env.get_player_id()
        other_player_id = []
        for i in range(3):
            if i != cur_player_id:
                other_player_id.append(i)

        other_hands = [[] for i in range(3)]
        # 合并二者
        merged_hands = []
        for id in other_player_id:
            other_hands[id] = copy.deepcopy(env.game.players[id]._current_hand)
            merged_hands.extend(other_hands[id])

        merged_hands = np.asarray(merged_hands)

        other_shuffuled_hands = [[] for i in range(3)]
        other_shuffuled_idx = random.sample(range(len(merged_hands)),
                                            len(merged_hands))
        idx_start = 0
        other_shuffled_singels = [[] for i in range(3)]

        for id in other_player_id:
            # 选取
            hands = list(
                merged_hands[other_shuffuled_idx[idx_start:idx_start +
                                                 len(other_hands[id])]])
            # 排序
            hands.sort(key=functools.cmp_to_key(doudizhu_sort_card))
            other_shuffuled_hands[id] = hands
            idx_start += len(other_hands[id])

            st = cards2str(other_shuffuled_hands[id])
            cardstr = "".join(
                OrderedDict.fromkeys(cards2str(other_shuffuled_hands[id])))
            other_shuffled_singels[id] = cardstr

        # 设置数据到env
        for id in other_player_id:
            # 手牌更新
            env.game.players[id]._current_hand = list(
                other_shuffuled_hands[id])
            # 去重更新
            env.game.players[id].singles = other_shuffled_singels[id]
            # 重新计算可出的牌
            cardstr = cards2str(env.game.players[id]._current_hand)
            env.game.judger.playable_cards[
                id] = env.game.judger.playable_cards_from_hand(cardstr)
예제 #3
0
파일: game.py 프로젝트: hsywhu/rlcard
 def _get_others_current_hand(self, player):
     player_up = self.players[(player.player_id + 1) % len(self.players)]
     player_down = self.players[(player.player_id - 1) % len(self.players)]
     others_hand = merge(player_up.current_hand,
                         player_down.current_hand,
                         key=functools.cmp_to_key(doudizhu_sort_card))
     return cards2str(others_hand)
예제 #4
0
파일: judger.py 프로젝트: zsf1975/rlcard
    def get_playable_cards(self, player):
        ''' Provide all legal cards the player can play according to his
        current hand.

        Args:
            player (DoudizhuPlayer object): object of DoudizhuPlayer
            init_flag (boolean): For the first time, set it True to accelerate
              the preocess.

        Returns:
            list: list of string of playable cards
        '''
        player_id = player.player_id
        current_hand = cards2str(player.current_hand)
        missed = None
        for single in player.singles:
            if single not in current_hand:
                missed = single
                break

        playable_cards = self.playable_cards[player_id].copy()

        if missed is not None:
            position = player.singles.find(missed)
            player.singles = player.singles[position + 1:]
            for cards in playable_cards:
                if missed in cards or (not contains_cards(current_hand,
                                                          cards)):
                    self.playable_cards[player_id].remove(cards)
        else:
            for cards in playable_cards:
                if not contains_cards(current_hand, cards):
                    #del self.playable_cards[player_id][cards]
                    self.playable_cards[player_id].remove(cards)
        return self.playable_cards[player_id]
예제 #5
0
    def get_playable_cards(self, player):
        ''' Provide all legal cards the player can play according to his
        current hand.

        Args:
            player (DoudizhuPlayer object): object of DoudizhuPlayer

        Returns:
            list: list of string of playable cards
        '''

        player_id = player.player_id
        current_hand = cards2str(player.current_hand)
        missed = None
        for single in player.singles:
            if single not in current_hand:
                missed = single
                break
        playable_cards = list(self.playable_cards[player_id])
        if missed is not None:
            position = player.singles.find(missed)
            player.singles = player.singles[position + 1:]
            for cards in playable_cards:
                if missed in cards or (not contains_cards(current_hand,
                                                          cards)):
                    del self.playable_cards[player_id][cards]
        else:
            for cards in playable_cards:
                if not contains_cards(current_hand, cards):
                    del self.playable_cards[player_id][cards]
        return list(self.playable_cards[player_id])
예제 #6
0
파일: round.py 프로젝트: kaanozdogru/rlcard
    def __init__(self, np_random, played_cards):
        self.np_random = np_random
        self.played_cards = played_cards
        self.trace = []

        self.greater_player = None
        self.dealer = Dealer(self.np_random)
        self.deck_str = cards2str(self.dealer.deck)
예제 #7
0
파일: round.py 프로젝트: yw10/rlcard
    def __init__(self, np_random):
        self.np_random = np_random
        self.trace = []
        self.played_cards = np.zeros((len(CARD_RANK_STR), ), dtype=np.int)

        self.greater_player = None
        self.dealer = Dealer(self.np_random)
        self.deck_str = cards2str(self.dealer.deck)
예제 #8
0
 def __init__(self, players, np_random):
     ''' Initilize the Judger class for Dou Dizhu
     '''
     self.playable_cards = [set() for _ in range(3)]
     self._recorded_removed_playable_cards = [[] for _ in range(3)]
     for player in players:
         player_id = player.player_id
         current_hand = cards2str(player.current_hand)
         self.playable_cards[player_id] = self.playable_cards_from_hand(
             current_hand)
예제 #9
0
파일: judger.py 프로젝트: zsf1975/rlcard
 def __init__(self, players):
     ''' Initilize the Judger class for Dou Dizhu
     '''
     all_cards_list = CARD_TYPE[1]
     self.playable_cards = [set() for _ in range(3)]
     for player in players:
         player_id = player.player_id
         current_hand = cards2str(player.current_hand)
         for cards in all_cards_list:
             if contains_cards(current_hand, cards):
                 self.playable_cards[player_id].add(cards)
예제 #10
0
    def deal_cards(self, players):
        ''' Deal cards to players

        Args:
            players (list): list of DoudizhuPlayer objects
        '''
        hand_num = (len(self.deck) - 3) // len(players)
        for index, player in enumerate(players):
            current_hand = self.deck[index * hand_num:(index + 1) * hand_num]
            current_hand.sort(key=functools.cmp_to_key(doudizhu_sort_card))
            player.set_current_hand(current_hand)
            player.initial_hand = cards2str(player.current_hand)
예제 #11
0
    def restore_other_player_cards(self, env: Env):
        cur_player_id = env.get_player_id()
        other_player_id = []
        for i in range(3):
            if i != cur_player_id:
                other_player_id.append(i)

        for id in other_player_id:
            env.game.players[id]._current_hand = self.other_hands[id]
            env.game.players[id].singles = self.other_singles[id]
            cardstr = cards2str(env.game.players[id]._current_hand)
            env.game.judger.playable_cards[
                id] = env.game.judger.playable_cards_from_hand(cardstr)
예제 #12
0
    def get_state(self, public, others_hands, num_cards_left, actions):
        state = {}
        state['seen_cards'] = public['seen_cards']
        state['landlord'] = public['landlord']
        state['trace'] = public['trace'].copy()
        state['played_cards'] = public['played_cards']
        state['self'] = self.player_id
        state['current_hand'] = cards2str(self._current_hand)
        state['others_hand'] = others_hands
        state['num_cards_left'] = num_cards_left
        state['actions'] = actions

        return state
예제 #13
0
    def get_state(self, public, others_hands, actions):
        state = {}
        state['deck'] = public['deck']
        state['seen_cards'] = public['seen_cards']
        state['landlord'] = public['landlord']
        state['trace'] = public['trace'].copy()
        state['played_cards'] = public['played_cards'].copy()
        state['self'] = self.player_id
        state['initial_hand'] = self.initial_hand
        state['current_hand'] = cards2str(self.current_hand)
        state['others_hand'] = others_hands
        state['actions'] = actions

        return state
예제 #14
0
파일: round.py 프로젝트: yw10/rlcard
    def initiate(self, players):
        ''' Call dealer to deal cards and bid landlord.

        Args:
            players (list): list of DoudizhuPlayer objects
        '''
        landlord_id = self.dealer.determine_role(players)
        seen_cards = self.dealer.deck[-3:]
        seen_cards.sort(key=functools.cmp_to_key(doudizhu_sort_card))
        self.seen_cards = cards2str(seen_cards)
        self.landlord_id = landlord_id
        self.current_player = landlord_id
        self.public = {'deck': self.deck_str, 'seen_cards': self.seen_cards,
                       'landlord': self.landlord_id, 'trace': self.trace,
                       'played_cards': []}
예제 #15
0
    def get_perfect_information(self):
        ''' Get the perfect information of the current state

        Returns:
            (dict): A dictionary of all the perfect information of the current state
        '''
        state = {}
        state['hand_cards'] = [
            cards2str(player.current_hand) for player in self.game.players
        ]
        state['landlord'] = self.game.state['landlord']
        state['trace'] = self.game.state['trace']
        state['current_player'] = self.game.round.current_player
        state['legal_actions'] = self.game.state['actions']
        return state
예제 #16
0
    def determine_role(self, players):
        ''' Determine landlord and peasants according to players' hand

        Args:
            players (list): list of DoudizhuPlayer objects

        Returns:
            int: landlord's player_id
        '''
        # deal cards
        self.shuffle()
        self.deal_cards(players)
        players[0].role = 'landlord'
        self.landlord = players[0]
        players[1].role = 'peasant'
        players[2].role = 'peasant'
        #players[0].role = 'peasant'
        #self.landlord = players[0]

        ## determine 'landlord'
        #max_score = get_landlord_score(
        #    cards2str(self.landlord.current_hand))
        #for player in players[1:]:
        #    player.role = 'peasant'
        #    score = get_landlord_score(
        #        cards2str(player.current_hand))
        #    if score > max_score:
        #        max_score = score
        #        self.landlord = player
        #self.landlord.role = 'landlord'

        # give the 'landlord' the  three cards
        self.landlord.current_hand.extend(self.deck[-3:])
        self.landlord.current_hand.sort(
            key=functools.cmp_to_key(doudizhu_sort_card))
        self.landlord.initial_hand = cards2str(self.landlord.current_hand)
        return self.landlord.player_id