Esempio n. 1
0
 def test_decode_cards(self):
     deck = utils.get_deck()
     encoded_cards = utils.encode_cards(deck)
     decoded_cards = utils.decode_cards(encoded_cards)
     for i in range(52):
         card = deck[i]
         decoded_card = decoded_cards[i]
         self.assertEqual(card, decoded_card)
Esempio n. 2
0
    def _extract_state(self, state):  # 200213 don't use state ???
        ''' Encode state

        Args:
            state (dict): dict of original state

        Returns:
            numpy array: 5 * 52 array
                         5 : current hand (1 if card in hand else 0)
                             top_discard (1 if card is top discard else 0)
                             dead_cards (1 for discards except for top_discard else 0)
                             opponent known cards (likewise)
                             unknown cards (likewise)  # is this needed ??? 200213
        '''
        if self.game.is_over():
            obs = np.array([utils.encode_cards([]) for _ in range(5)])
            extracted_state = {'obs': obs, 'legal_actions': self._get_legal_actions()}
        else:
            discard_pile = self.game.round.dealer.discard_pile
            stock_pile = self.game.round.dealer.stock_pile
            top_discard = [] if not discard_pile else [discard_pile[-1]]
            dead_cards = discard_pile[:-1]
            current_player = self.game.get_current_player()
            opponent = self.game.round.players[(current_player.player_id + 1) % 2]
            known_cards = opponent.known_cards
            unknown_cards = stock_pile + [card for card in opponent.hand if card not in known_cards]
            hand_rep = utils.encode_cards(current_player.hand)
            top_discard_rep = utils.encode_cards(top_discard)
            dead_cards_rep = utils.encode_cards(dead_cards)
            known_cards_rep = utils.encode_cards(known_cards)
            unknown_cards_rep = utils.encode_cards(unknown_cards)
            rep = [hand_rep, top_discard_rep, dead_cards_rep, known_cards_rep, unknown_cards_rep]
            obs = np.array(rep)
            extracted_state = {'obs': obs, 'legal_actions': self._get_legal_actions()}
        return extracted_state