Esempio n. 1
0
 def test_init_game(self):
     game = Game()
     state, player_id = game.init_game()
     test_id = game.get_player_id()
     self.assertEqual(test_id, player_id)
     self.assertIn('call', state['legal_actions'])
     self.assertIn('raise', state['legal_actions'])
     self.assertIn('fold', state['legal_actions'])
Esempio n. 2
0
    def __init__(self, config):
        ''' Initialize the Limitholdem environment
        '''
        self.game = Game()
        super().__init__(config)
        self.actions = ['call', 'raise', 'fold', 'check']
        self.state_shape=[72]

        with open(os.path.join(rlcard.__path__[0], 'games/limitholdem/card2index.json'), 'r') as file:
            self.card2index = json.load(file)
Esempio n. 3
0
 def test_payoffs(self):
     game = Game()
     np.random.seed(0)
     for _ in range(5):
         game.init_game()
         while not game.is_over():
             legal_actions = game.get_legal_actions()
             action = np.random.choice(legal_actions)
             game.step(action)
         payoffs = game.get_payoffs()
         total = 0
         for payoff in payoffs:
             total += payoff
         self.assertEqual(total, 0)
Esempio n. 4
0
class LimitholdemEnv(Env):
    ''' Limitholdem Environment
    '''
    def __init__(self, config):
        ''' Initialize the Limitholdem environment
        '''
        self.game = Game()
        super().__init__(config)
        self.actions = ['call', 'raise', 'fold', 'check']
        self.state_shape = [72]

        with open(
                os.path.join(rlcard.__path__[0],
                             'games/limitholdem/card2index.json'),
                'r') as file:
            self.card2index = json.load(file)

    def _get_legal_actions(self):
        ''' Get all leagal actions

        Returns:
            encoded_action_list (list): return encoded legal action list (from str to int)
        '''
        return self.game.get_legal_actions()

    def _extract_state(self, state):
        ''' Extract the state representation from state dictionary for agent

        Note: Currently the use the hand cards and the public cards. TODO: encode the states

        Args:
            state (dict): Original state from the game

        Returns:
            observation (list): combine the player's score and dealer's observable score for observation
        '''
        extracted_state = {}

        legal_actions = [self.actions.index(a) for a in state['legal_actions']]
        extracted_state['legal_actions'] = legal_actions

        public_cards = state['public_cards']
        hand = state['hand']
        raise_nums = state['raise_nums']
        cards = public_cards + hand
        idx = [self.card2index[card] for card in cards]
        obs = np.zeros(72)
        obs[idx] = 1
        for i, num in enumerate(raise_nums):
            obs[52 + i * 5 + num] = 1
        extracted_state['obs'] = obs

        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):
        ''' Get the payoff of a game

        Returns:
           payoffs (list): list of payoffs
        '''
        return self.game.get_payoffs()

    def _decode_action(self, action_id):
        ''' Decode the action for applying to the game

        Args:
            action id (int): action id

        Returns:
            action (str): action for the game
        '''
        legal_actions = self.game.get_legal_actions()
        if self.actions[action_id] not in legal_actions:
            if 'check' in legal_actions:
                return 'check'
            else:
                return 'fold'
        return self.actions[action_id]

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

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

    def get_perfect_information(self):
        ''' Get the perfect information of the current state

        Returns:
            (dict): A dictionary of all the perfect information of the current state
        '''
        state = {}
        state['chips'] = [
            self.game.players[i].in_chips for i in range(self.player_num)
        ]
        state['public_card'] = [c.get_index() for c in self.game.public_cards
                                ] if self.game.public_cards else None
        state['hand_cards'] = [[
            c.get_index() for c in self.game.players[i].hand
        ] for i in range(self.player_num)]
        state['current_player'] = self.game.game_pointer
        state['legal_actions'] = self.game.get_legal_actions()
        return state
Esempio n. 5
0
 def test_get_num_actions(self):
     game = Game()
     num_players = game.get_num_players()
     self.assertEqual(num_players, 2)
Esempio n. 6
0
 def test_step_back(self):
     game = Game(allow_step_back=True)
     game.init_game()
     self.assertEqual(game.step_back(), False)
     index = 0
     previous = None
     while not game.is_over():
         index += 1
         legal_actions = game.get_legal_actions()
         if index == 2:
             result = game.step_back()
             now = game.get_player_id()
             if result:
                 self.assertEqual(previous, now)
             else:
                 self.assertEqual(len(game.history), 0)
             break
         previous = game.get_player_id()
         action = np.random.choice(legal_actions)
         game.step(action)
Esempio n. 7
0
    def test_step(self):
        game = Game()

        # test raise
        game.init_game()
        init_raised = game.round.have_raised
        game.step('raise')
        step_raised = game.round.have_raised
        self.assertEqual(init_raised + 1, step_raised)

        # test call
        game.init_game()
        init_not_raise_num = game.round.not_raise_num
        game.step('call')
        step_not_raise_num = game.round.not_raise_num
        self.assertEqual(init_not_raise_num + 1, step_not_raise_num)

        # test fold
        game.init_game()
        game.step('fold')
        self.assertTrue(game.round.player_folded)

        # test check
        game.init_game()
        game.step('call')
        game.step('check')
        self.assertEqual(game.round_counter, 1)

        # test play 4 rounds
        game.init_game()
        for i in range(19):
            if (i + 1) % 5 == 0:
                game.step('call')
            else:
                game.step('raise')
            self.assertEqual(game.is_over(), False)
        game.step('call')
        self.assertEqual(game.is_over(), True)

        # Test illegal actions
        game.init_game()
        with self.assertRaises(Exception):
            game.step('check')

        # Test the upper limit of raise
        game.init_game()
        for _ in range(4):
            game.step('raise')

        legal_actions = game.get_legal_actions()
        self.assertNotIn('raise', legal_actions)
Esempio n. 8
0
 def test_get_num_actions(self):
     game = Game()
     num_actions = game.get_num_actions()
     self.assertEqual(num_actions, 4)
Esempio n. 9
0
 def test_get_player_num(self):
     game = Game()
     player_num = game.get_player_num()
     self.assertEqual(player_num, 2)
Esempio n. 10
0
 def test_get_action_num(self):
     game = Game()
     action_num = game.get_action_num()
     self.assertEqual(action_num, 4)