예제 #1
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)
예제 #2
0
 def test_step(self):
     game = Game()
     state, _ = game.init_game()
     action = state['actions'][0]
     state, next_player_id = game.step(action)
     next_player = game.players[next_player_id]
     player_id = get_upstream_player_id(next_player, game.players)
     self.assertEqual(state['trace'][0][0], player_id)
     self.assertEqual(state['trace'][0][1], action)
예제 #3
0
파일: game.py 프로젝트: yli96/rlcard
    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
예제 #4
0
파일: game.py 프로젝트: yli96/rlcard
    def is_over(self):
        ''' Judge whether a game is over

        Returns:
            Bool: True(over) / False(not over)
        '''

        if self.current_player is None:
            return True
        last_player = get_upstream_player_id(
            self.players[self.current_player], self.players)
        if len(self.players[last_player].current_hand) == 0:
            if self.players[last_player].role == 'peasant':
                for _, player in enumerate(self.players):
                    if player.role == 'peasant':
                        self.game_result[_] = 1
            else:
                self.game_result[last_player] = 1
            self.current_player = None
            return True
        return False
예제 #5
0
 def test_get_upstream_player_id(self):
     players = init_players(5)
     self.assertEqual(get_upstream_player_id(players[0], players), 4)