コード例 #1
0
    def test_all_in(self):
        game = Game()

        _, player_id = game.init_game()
        game.step(Action.ALL_IN)
        step_raised = game.round.raised[player_id]
        self.assertEqual(100, step_raised)
        self.assertEqual(100, game.players[player_id].in_chips)
        self.assertEqual(0, game.players[player_id].remained_chips)
コード例 #2
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('fold', state['legal_actions'])
        for i in range(3,100):
            self.assertIn(i , state['legal_actions'])
コード例 #3
0
    def test_bet_more_than_chips(self):
        game = Game()

        # test check
        game.init_game()
        player = game.players[0]
        in_chips = player.in_chips
        player.bet(50)
        self.assertEqual(50 + in_chips, player.in_chips)

        player.bet(150)
        self.assertEqual(100, player.in_chips)
コード例 #4
0
    def test_auto_step(self):
        game = Game()

        game.init_game()
        self.assertEqual(Stage.PREFLOP, game.stage)
        game.step(Action.ALL_IN)
        game.step(Action.CALL)

        self.assertEqual(Stage.RIVER, game.stage)
コード例 #5
0
    def __init__(self, config):
        ''' Initialize the Limitholdem environment
        '''
        self.game  =Game()
        super().__init__(config)
        self.actions = ['call', 'fold', 'check', 'all-in']
        self.state_shape = [54]
        for raise_amount in range(1, self.game.init_chips+1):
            self.actions.append(raise_amount)

        with open(os.path.join(rlcard.__path__[0], 'games/limitholdem/card2index.json'), 'r') as file:
            self.card2index = json.load(file)
コード例 #6
0
    def test_raise_half_pot(self):
        game = Game()

        _, player_id = game.init_game()
        self.assertNotIn(Action.RAISE_HALF_POT,
                         game.get_legal_actions())  # Half pot equals call
        game.step(Action.CALL)
        step_raised = game.round.raised[player_id]
        self.assertEqual(2, step_raised)

        player_id = game.round.game_pointer
        game.step(Action.RAISE_HALF_POT)
        step_raised = game.round.raised[player_id]
        self.assertEqual(4, step_raised)

        player_id = game.round.game_pointer
        game.step(Action.RAISE_HALF_POT)
        step_raised = game.round.raised[player_id]
        self.assertEqual(5, step_raised)
コード例 #7
0
    def test_raise_half_pot(self):
        game = Game()

        _, player_id = game.init_game()
        game.step(Action.RAISE_HALF_POT)
        step_raised = game.round.raised[player_id]
        self.assertEqual(2, step_raised)

        player_id = game.round.game_pointer
        game.step(Action.RAISE_HALF_POT)
        step_raised = game.round.raised[player_id]
        self.assertEqual(4, step_raised)

        player_id = game.round.game_pointer
        game.step(Action.RAISE_HALF_POT)
        step_raised = game.round.raised[player_id]
        self.assertEqual(5, step_raised)
コード例 #8
0
    def test_wrong_steps(self):
        game = Game()

        game.init_game()
        self.assertRaises(Exception, game.step, Action.CHECK)
コード例 #9
0
    def test_step_2(self):
        game = Game()

        # test check
        game.init_game()
        self.assertEqual(Stage.PREFLOP, game.stage)
        game.step(Action.CALL)
        game.step(Action.RAISE_POT)
        game.step(Action.CALL)

        self.assertEqual(Stage.FLOP, game.stage)
        game.step(Action.CHECK)
        game.step(Action.CHECK)

        self.assertEqual(Stage.TURN, game.stage)
        game.step(Action.CHECK)
        game.step(Action.CHECK)

        self.assertEqual(Stage.RIVER, game.stage)
コード例 #10
0
class NolimitholdemEnv(Env):
    ''' Limitholdem Environment
    '''
    def __init__(self, config):
        ''' Initialize the Limitholdem environment
        '''
        self.game = Game()
        super().__init__(config)
        self.actions = ['call', 'fold', 'check']
        self.state_shape = [54]
        for raise_amount in range(1, self.game.init_chips + 1):
            self.actions.append(raise_amount)

        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']
        my_chips = state['my_chips']
        all_chips = state['all_chips']
        cards = public_cards + hand
        idx = [self.card2index[card] for card in cards]
        obs = np.zeros(54)
        obs[idx] = 1
        obs[52] = float(my_chips)
        obs[53] = float(max(all_chips))
        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 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
コード例 #11
0
    def test_step(self):
        game = Game()

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

        # test fold
        _, player_id = game.init_game()
        game.step(Action.FOLD)

        self.assertEqual(PlayerStatus.FOLDED, game.players[player_id].status)

        # test check
        game.init_game()
        game.step(Action.CALL)
        game.step(Action.CHECK)
        self.assertEqual(game.round_counter, 1)
コード例 #12
0
 def test_payoffs_2(self):
     game = Game()
     np.random.seed(0)
     game.init_game()
     game.step(Action.CALL)
     game.step(Action.RAISE_POT)
     game.step(Action.ALL_IN)
     game.step(Action.FOLD)
     self.assertTrue(game.is_over())
     self.assertEqual(2, len(game.get_payoffs()))
コード例 #13
0
 def test_payoffs_1(self):
     game = Game()
     game.init_game()
     game.step(Action.CALL)
     game.step(Action.RAISE_HALF_POT)
     game.step(Action.FOLD)
     self.assertTrue(game.is_over())
     self.assertEqual(2, len(game.get_payoffs()))
コード例 #14
0
    def test_raise_pot(self):
        game = Game()

        _, player_id = game.init_game()
        game.step(Action.RAISE_POT)
        step_raised = game.round.raised[player_id]
        self.assertEqual(4, step_raised)

        player_id = game.round.game_pointer
        game.step(Action.RAISE_POT)
        step_raised = game.round.raised[player_id]
        self.assertEqual(8, step_raised)

        player_id = game.round.game_pointer
        game.step(Action.RAISE_POT)
        step_raised = game.round.raised[player_id]
        self.assertEqual(16, step_raised)

        game.step(Action.CALL)
        player_id = game.round.game_pointer
        game.step(Action.RAISE_POT)
        step_raised = game.round.raised[player_id]
        self.assertEqual(32, step_raised)
コード例 #15
0
 def test_payoffs_2(self):
     game = Game()
     np.random.seed(0)
     game.init_game()
     game.step('call')
     game.step(4)
     game.step('all-in')
     game.step('fold')
     self.assertTrue(game.is_over())
     self.assertListEqual([6.0, -6.0], game.get_payoffs())
コード例 #16
0
    def test_step(self):
        game = Game()

        # test raise
        _, player_id = game.init_game()
        init_raised = game.round.raised[player_id]
        game.step(10)
        step_raised = game.round.raised[player_id]
        self.assertEqual(init_raised+10, 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)
コード例 #17
0
    def test_step_3_players(self):
        game = Game(num_players=3)

        # test check
        _, first_player_id = game.init_game()
        self.assertEqual(Stage.PREFLOP, game.stage)
        game.step(Action.CALL)
        game.step(Action.CALL)
        game.step(Action.RAISE_POT)
        game.step(Action.FOLD)
        game.step(Action.CALL)

        self.assertEqual(Stage.FLOP, game.stage)
        self.assertEqual((first_player_id - 2) % 3, game.round.game_pointer)
        game.step(Action.CHECK)
        game.step(Action.RAISE_POT)
        game.step(Action.CALL)

        self.assertEqual(Stage.TURN, game.stage)
        self.assertEqual((first_player_id - 2) % 3, game.round.game_pointer)
        game.step(Action.CHECK)
        game.step(Action.CHECK)

        self.assertEqual(Stage.RIVER, game.stage)
コード例 #18
0
 def test_all_in_to_call(self):
     game = Game()
     game.init_chips = [50, 100]
     game.dealer_id = 0
     game.init_game()
     game.step(Action.CALL)
     game.step(Action.ALL_IN)
     game.step(Action.CALL)
     self.assertTrue(game.is_over())
コード例 #19
0
 def test_get_action_num(self):
     game = Game()
     action_num = game.get_action_num()
     self.assertEqual(action_num, 103)
コード例 #20
0
    def test_all_in_rounds(self):
        game = Game()

        game.init_game()
        game.step(Action.CALL)
        game.step(Action.CHECK)
        self.assertEqual(game.round_counter, 1)
        self.assertTrue(Action.CALL not in game.get_legal_actions())

        game.step(Action.CHECK)
        game.step(Action.ALL_IN)
        self.assertListEqual([Action.FOLD, Action.CALL],
                             game.get_legal_actions())
        game.step(Action.CALL)
        self.assertEqual(game.round_counter, 4)
        self.assertEqual(200, game.dealer.pot)
コード例 #21
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)