Beispiel #1
0
    def step(self, action):
        ''' Perform one draw of the game

        Args:
            action (str): specific action of doudizhu. Eg: '33344'

        Returns:
            dict: next player's state
            int: next player's id
        '''

        # record game history
        player = self.players[self.current_player]
        self._record_history()
        self.trace.append((self.current_player, action))

        # update cards played
        if action != 'pass':
            self._add_played_cards(action)
            self.state['played_cards'] = self.played_cards
        # perform action
        greater_player = self.rounder.proceed_round(player, action)
        next_player_id = get_downstream_player_id(player, self.players)

        # update next_state
        self.state['self'] = next_player_id
        next_player = self.players[next_player_id]
        self.state['hand'] = cards2str(next_player.hand)
        self.state['current_hand'] = cards2str(next_player.current_hand)
        actions = next_player.available_actions(greater_player, self.judger)
        self.state['actions'] = actions
        self.state['others_hand'] = self._get_others_current_hand(next_player)
        self.current_player = next_player_id
        return copy.deepcopy(self.state), next_player_id
Beispiel #2
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 = '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.hand.extend(self.deck[-3:])
        self.landlord.hand.sort(key=functools.cmp_to_key(doudizhu_sort_card))
        self.landlord.current_hand = copy.deepcopy(self.landlord.hand)
        return self.landlord.player_id
Beispiel #3
0
    def __init__(self):
        self.trace = []
        self.played_cards = np.zeros((len(CARD_RANK_STR), ), dtype=np.int)

        self.greater_player = None
        self.dealer = Dealer()
        self.deck_str = cards2str(self.dealer.deck)
Beispiel #4
0
    def __init__(self):
        self.trace = []
        self.played_cards = []

        self.greater_player = None
        self.dealer = Dealer()
        self.deck_str = cards2str(self.dealer.deck)
Beispiel #5
0
 def _get_others_current_hand(self, player):
     player_up = self.players[get_upstream_player_id(player, self.players)]
     player_down = self.players[get_downstream_player_id(
         player, self.players)]
     others_hand = (player_up.current_hand + player_down.current_hand)
     others_hand.sort(key=functools.cmp_to_key(doudizhu_sort_card))
     return cards2str(others_hand)
Beispiel #6
0
    def init_game(self):
        ''' Initialize players and state.

        Returns:
            dict: first state in one game
            int: current player's id
        '''

        # initialize public variables
        self.current_game += 1
        self.game_result = {0: 0, 1: 0, 2: 0}
        self.histories = []
        self.trace = []
        self.played_cards = []
        self.state = {'deck': None, 'seen_cards': None, 'landlord': None,
                      'self': None, 'initial_hand': None, 'trace': self.trace,
                      'played_cards': None, 'others_hand': None,
                      'current_hand': None, 'actions': []}

        # initialize players
        self.players = [Player(num)
                        for num in range(DoudizhuGame.players_num)]

        # initialize round to deal cards and determine landlord
        self.rounder = Round()
        self.rounder.initiate(self.players)
        self.current_player = self.rounder.landlord_num

        # initialize Judger
        self.judger = Judger(self.players)

        # initialize state of landlord to be ready for proceeding round
        player = self.players[self.current_player]
        self.rounder.round_last = get_upstream_player_id(player, self.players)
        deck = init_54_deck()
        deck.sort(key=functools.cmp_to_key(doudizhu_sort_card))
        self.state['deck'] = cards2str(deck)
        self.state['landlord'] = self.rounder.landlord_num
        self.state['self'] = self.current_player
        self.state['hand'] = cards2str(player.hand)
        self.state['seen_cards'] = self.rounder.seen_cards
        self.state['current_hand'] = cards2str(player.current_hand)
        self.state['others_hand'] = self._get_others_current_hand(player)
        self.state['actions'] = list(
            self.judger.playable_cards[self.current_player])
        return copy.deepcopy(self.state), self.current_player
Beispiel #7
0
    def get_state(self, player_id):
        ''' Return player's state

        Args:
            player_id (int): the player_id of a player

        Returns:
            dict: corresponding player's state
        '''

        player = self.players[player_id]
        if self.current_player is not None:  # when get first state
            return copy.deepcopy(self.state)
        else:  # when get final states of all players
            self.state['self'] = player_id
            self.state['hand'] = cards2str(player.hand)
            self.state['current_hand'] = cards2str(player.current_hand)
            self.state['actions'] = None
            self.state['others_hand'] = self._get_others_current_hand(player)
            return copy.deepcopy(self.state)
Beispiel #8
0
    def deal_cards(self, players):
        ''' Deal cards to players

        Args:
            players (list): list of DoudizhuPlayer objects
        '''
        hand_num = (len(self.deck) - 1) // 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)
Beispiel #9
0
    def initiate(self, players):
        ''' Call dealer to deal cards and bid landlord.

        Args:
            players (list): list of DoudizhuPlayer objects
        '''

        landlord_num = 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_num = landlord_num
Beispiel #10
0
    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': []}