Exemplo n.º 1
0
 def test_encode_cards(self):
     plane = np.zeros((5, 15), dtype=int)
     plane[0] = np.ones(15, dtype=int)
     cards = '333BR'
     encode_cards(plane, cards)
     self.assertEqual(plane[3][0], 1)
     self.assertEqual(plane[1][13], 1)
     self.assertEqual(plane[1][14], 1)
Exemplo n.º 2
0
    def extract_state(self, state):
        ''' Encode state

        Args:
            state (dict): dict of original state

        Returns:
            numpy array: 6*5*15 array
                         6 : current hand
                             the union of the other two players' hand
                             the recent three actions
                             the union of all played cards
        '''

        encoded_state = np.zeros((6, 5, 15), dtype=int)
        for index in range(6):
            encoded_state[index][0] = np.ones(15, dtype=int)
        encode_cards(encoded_state[0], state['current_hand'])
        encode_cards(encoded_state[1], state['others_hand'])
        for i, action in enumerate(state['trace'][-3:]):
            if action[1] != 'pass':
                encode_cards(encoded_state[4 - i], action[1])
        if state['played_cards'] is not None:
            encode_cards(encoded_state[5], state['played_cards'])
        return encoded_state
Exemplo n.º 3
0
    def _extract_state(self, state):
        ''' Encode state

        Args:
            state (dict): dict of original state

        Returns:
            numpy array: 6*5*15 array
                         6 : current hand
                             the union of the other two players' hand
                             the recent three actions
                             the union of all played cards
        '''
        obs = np.zeros((6, 5, 15), dtype=int)
        for index in range(6):
            obs[index][0] = np.ones(15, dtype=int)
        encode_cards(obs[0], state['current_hand'])
        encode_cards(obs[1], state['others_hand'])
        for i, action in enumerate(state['trace'][-3:]):
            if action[1] != 'pass':
                encode_cards(obs[4 - i], action[1])
        if state['played_cards'] is not None:
            encode_cards(obs[5], state['played_cards'])

        extracted_state = {
            'obs': obs,
            'legal_actions': self._get_legal_actions()
        }
        if self.allow_raw_data:
            extracted_state['raw_obs'] = state
            # TODO: state['actions'] can be None, may have bugs
            if state['actions'] == None:
                extracted_state['raw_legal_actions'] = []
            else:
                extracted_state['raw_legal_actions'] = [
                    a for a in state['actions']
                ]
        if self.record_action:
            extracted_state['action_record'] = self.action_recorder
        return extracted_state