Example #1
0
 def test_step(self):
     game = Game()
     game.init_game()
     action = np.random.choice(game.get_legal_actions())
     state, next_player_id = game.step(action)
     current = game.round.current_player
     self.assertLessEqual(len(state['played_cards']), 2)
     self.assertEqual(next_player_id, current)
Example #2
0
class UnoEnv(Env):
    def __init__(self, config):
        self.game = Game()
        super().__init__(config)
        self.state_shape = [7, 4, 15]

    def _load_model(self):
        ''' Load pretrained/rule model

        Returns:
            model (Model): A Model object
        '''
        return models.load('uno-rule-v1')

    def _extract_state(self, state):
        obs = np.zeros((7, 4, 15), dtype=int)
        encode_hand(obs[:3], state['hand'])
        encode_target(obs[3], state['target'])
        encode_hand(obs[4:], state['others_hand'])
        legal_action_id = self._get_legal_actions()
        extracted_state = {'obs': obs, 'legal_actions': legal_action_id}
        if self.allow_raw_data:
            extracted_state['raw_obs'] = state
            extracted_state['raw_legal_actions'] = [
                a for a in state['legal_actions']
            ]
        if self.record_action:
            extracted_state['action_record'] = self.action_recorder
        return extracted_state

    def get_payoffs(self):

        return self.game.get_payoffs()

    def _decode_action(self, action_id):
        legal_ids = self._get_legal_actions()
        if action_id in legal_ids:
            return ACTION_LIST[action_id]
        #if (len(self.game.dealer.deck) + len(self.game.round.played_cards)) > 17:
        #    return ACTION_LIST[60]
        return ACTION_LIST[np.random.choice(legal_ids)]

    def _get_legal_actions(self):
        legal_actions = self.game.get_legal_actions()
        legal_ids = [ACTION_SPACE[action] for action in legal_actions]
        return legal_ids
Example #3
0
 def test_step_back(self):
     game = Game(allow_step_back=True)
     _, player_id = game.init_game()
     action = np.random.choice(game.get_legal_actions())
     game.step(action)
     game.step_back()
     self.assertEqual(game.round.current_player, player_id)
     self.assertEqual(len(game.history), 0)
     success = game.step_back()
     self.assertEqual(success, False)
Example #4
0
 def test_get_payoffs(self):
     game = Game()
     game.init_game()
     while not game.is_over():
         actions = game.get_legal_actions()
         action = np.random.choice(actions)
         state, _ = game.step(action)
     payoffs = game.get_payoffs()
     total = 0
     for payoff in payoffs:
         total += payoff
     self.assertEqual(total, 0)
Example #5
0
 def test_get_payoffs(self):
     game = Game()
     game.init_game()
     while not game.is_over():
         actions = game.get_legal_actions()
         action = np.random.choice(actions)
         state, _ = game.step(action)
         total_cards = len(state['hand']) + len(state['others_hand']) + len(
             state['played_cards']) + len(game.round.dealer.deck)
         self.assertEqual(total_cards, 108)
     payoffs = game.get_payoffs()
     total = 0
     for payoff in payoffs:
         total += payoff
     self.assertEqual(total, 0)
Example #6
0
 def __init__(self, config):
     self.game = Game()
     super().__init__(config)
     self.state_shape = [7, 4, 15]
Example #7
0
 def test_get_legal_actions(self):
     game = Game()
     game.init_game()
     actions = game.get_legal_actions()
     for action in actions:
         self.assertIn(action, ACTION_LIST)
Example #8
0
 def test_get_player_id(self):
     game = Game()
     _, player_id = game.init_game()
     current = game.get_player_id()
     self.assertEqual(player_id, current)
Example #9
0
 def test_init_game(self):
     game = Game()
     state, _ = game.init_game()
Example #10
0
 def test_get_action_num(self):
     game = Game()
     action_num = game.get_action_num()
     self.assertEqual(action_num, 61)
Example #11
0
 def test_get_player_num(self):
     game = Game()
     num_player = game.get_player_num()
     self.assertEqual(num_player, 2)
Example #12
0
 def test_init_game(self):
     game = Game()
     state, _ = game.init_game()
     total_cards = list(state['hand'] + state['others_hand'])
     self.assertGreaterEqual(len(total_cards), 14)
Example #13
0
 def test_get_num_actions(self):
     game = Game()
     num_actions = game.get_num_actions()
     self.assertEqual(num_actions, 61)
Example #14
0
 def test_get_num_player(self):
     game = Game()
     num_players = game.get_num_players()
     self.assertEqual(num_players, 2)