def test_blinds_fold_to_big_blind(self): self.initialize(game.Configuration( max_players=5, game_type=game.GameType.LIMIT, limits=(100, 200), blinds=(50, 100))) self.perform_actions([ game.Action(1, game.ActionType.FOLD), game.Action(3, game.ActionType.FOLD), game.Action(4, game.ActionType.FOLD), ]) self.assertFalse(self.manager.current_hand.is_betting_active()) self.assertEqual(150, self.manager.current_hand.pot) self.assertEqual([900, 1000, None, 1000, 950], self.get_stacks()) self.assertEqual([True, False, False, False, False], self.manager.current_hand.live_players())
def test_allowed_action_api(self): # This test covers the external API of AllowedAction. Other test cases will verify the code # that generates AllowedAction for different game states. allowed = game.AllowedAction() allowed._player_idx = 3 allowed._action_map[game.ActionType.CHECK] = None allowed._action_map[game.ActionType.BET] = (10, 20) allowed._action_map[game.ActionType.CALL] = (5, 5) self.assertTrue(allowed.is_action_type_allowed(game.ActionType.CHECK)) self.assertTrue(allowed.is_action_type_allowed(game.ActionType.BET)) self.assertFalse(allowed.is_action_type_allowed(game.ActionType.RAISE)) self.assertTrue(allowed.is_action_type_allowed(game.ActionType.CALL)) with self.assertRaises(ValueError): allowed.range_for_action(game.ActionType.CHECK) with self.assertRaises(game.ActionNotAllowedError): allowed.range_for_action(game.ActionType.RAISE) self.assertEqual((10, 20), allowed.range_for_action(game.ActionType.BET)) self.assertEqual((5, 5), allowed.range_for_action(game.ActionType.CALL)) with self.assertRaises(game.ActionOutOfTurnError): allowed.check_action(game.Action(0, game.ActionType.CHECK)) with self.assertRaises(game.ActionAmountError): allowed.check_action(game.Action(3, game.ActionType.BET, 9)) with self.assertRaises(game.ActionAmountError): allowed.check_action(game.Action(3, game.ActionType.BET, 21)) allowed.check_action(game.Action(3, game.ActionType.BET, 10)) allowed.check_action(game.Action(3, game.ActionType.BET, 20)) allowed.check_action(game.Action(3, game.ActionType.CALL)) allowed._action_map[game.ActionType.RAISE] = (100, 200) with self.assertRaises(game.ActionAmountError): allowed.check_action(game.Action(3, game.ActionType.RAISE, 9999)) allowed.check_action(game.Action(3, game.ActionType.RAISE, 200))
def test_no_blinds_check_around(self): self.initialize(game.Configuration( max_players=5, game_type=game.GameType.LIMIT, limits=(100, 200))) self.perform_actions([ game.Action(4, game.ActionType.CHECK), game.Action(0, game.ActionType.CHECK), game.Action(1, game.ActionType.CHECK), game.Action(3, game.ActionType.CHECK), ]) self.assertFalse(self.manager.current_hand.is_betting_active()) self.assertEqual(0, self.manager.current_hand.pot) self.assertEqual([1000, 1000, None, 1000, 1000], self.get_stacks()) self.assertEqual([True, True, False, True, True], self.manager.current_hand.live_players())
def test_actions_verified(self): # This is just testing one case of actions being checked. Detailed look at all the weird cases for # allowed actions are in the AllowedActionTestCase. self.initialize(game.Configuration( max_players=5, game_type=game.GameType.LIMIT, limits=(100, 200), blinds=(50, 100))) with self.assertRaises(game.ActionAmountError): self.manager.act(game.Action(1, game.ActionType.RAISE, amount=999))
def test_fold_river(self): self.manager.start_game() self.assertEqual(game.GameState.PRE_DEAL, self.manager.state) self.manager.proceed() self.assertEqual(game.GameState.HOLE_CARDS_DEALT, self.manager.state) check_call_all(self.manager) self.manager.proceed() self.assertEqual(game.GameState.FLOP_DEALT, self.manager.state) check_call_all(self.manager) self.manager.proceed() self.assertEqual(game.GameState.TURN_DEALT, self.manager.state) check_call_all(self.manager) self.manager.proceed() self.assertEqual(game.GameState.RIVER_DEALT, self.manager.state) self.manager.act(game.Action(1, game.ActionType.BET, 20)) fold_all(self.manager) self.recorder.clear() self.manager.proceed() self.assertEqual(game.GameState.PAYING_OUT, self.manager.state) self.assertEqual(1, len(self.recorder.events)) self.assertEqual( "Event(EventType.PAYING_OUT" + ", net_profit=[-10, 20, -10, None]" ", pot_winnings=[0, 50, 0, None]" ")", str(self.recorder.events[0])) self.assertEqual(990, self.manager.players[0].stack) self.assertEqual(1020, self.manager.players[1].stack) self.assertEqual(990, self.manager.players[2].stack)
def check_call_all(manager): while manager.current_hand.is_betting_active(): allowed = manager.current_hand.allowed_action() for act in [game.ActionType.CHECK, game.ActionType.CALL]: if allowed.is_action_type_allowed(act): #print("{} acting with {}".format(allowed.player_idx, act)) manager.act(game.Action(player_idx=allowed.player_idx, action_type=act)) break
def test_limit_raise_sizes(self): self.initialize(game.Configuration( max_players=3, game_type=game.GameType.LIMIT, limits=(100, 200), blinds=(50, 100))) # pre-flop allowed = self.manager.current_hand.allowed_action() self.assert_allowed_actions(allowed, 0, {game.ActionType.CALL: (100, 100), game.ActionType.RAISE: (100, 100), game.ActionType.FOLD: None,}) # flop check_call_all(self.manager) self.manager.proceed() self.assertEqual(game.GameState.FLOP_DEALT, self.manager.state) self.manager.act(game.Action(1, game.ActionType.BET, 100)) allowed = self.manager.current_hand.allowed_action() self.assert_allowed_actions(allowed, 2, {game.ActionType.CALL: (100, 100), game.ActionType.RAISE: (100, 100), game.ActionType.FOLD: None,}) # turn check_call_all(self.manager) self.manager.proceed() self.assertEqual(game.GameState.TURN_DEALT, self.manager.state) self.manager.act(game.Action(1, game.ActionType.BET, 200)) allowed = self.manager.current_hand.allowed_action() self.assert_allowed_actions(allowed, 2, {game.ActionType.CALL: (200, 200), game.ActionType.RAISE: (200, 200), game.ActionType.FOLD: None,}) # river check_call_all(self.manager) self.manager.proceed() self.assertEqual(game.GameState.RIVER_DEALT, self.manager.state) self.manager.act(game.Action(1, game.ActionType.BET, 200)) allowed = self.manager.current_hand.allowed_action() self.assert_allowed_actions(allowed, 2, {game.ActionType.CALL: (200, 200), game.ActionType.RAISE: (200, 200), game.ActionType.FOLD: None,})
def act(self): self.actions.append( game.Action( self, { 'x': randrange(0, self.scene.width - self.width), 'y': randrange(0, self.scene.height - self.height) }, speed=10, #time=10000, pause=2000))
def test_simple_limit(self): self.initialize(game.Configuration( max_players=3, game_type=game.GameType.LIMIT, limits=(100, 200), blinds=(50, 100))) # pre flop allowed = self.manager.current_hand.allowed_action() self.assert_allowed_actions(allowed, 0, {game.ActionType.CALL: (100, 100), game.ActionType.RAISE: (100, 100), game.ActionType.FOLD: None,}) self.manager.act(game.Action(0, game.ActionType.CALL)) allowed = self.manager.current_hand.allowed_action() self.assert_allowed_actions(allowed, 1, {game.ActionType.CALL: (50, 50), game.ActionType.RAISE: (100, 100), game.ActionType.FOLD: None,}) self.manager.act(game.Action(1, game.ActionType.CALL)) allowed = self.manager.current_hand.allowed_action() self.assert_allowed_actions(allowed, 2, {game.ActionType.CHECK: None, game.ActionType.RAISE: (100, 100), game.ActionType.FOLD: None,}) self.manager.act(game.Action(2, game.ActionType.CHECK)) for expected_state, expected_bet in [[game.GameState.FLOP_DEALT, 100], [game.GameState.TURN_DEALT, 200], [game.GameState.RIVER_DEALT, 200]]: self.manager.proceed() self.assertEqual(expected_state, self.manager.state) for player_idx in [1, 2, 0]: allowed = self.manager.current_hand.allowed_action() self.assert_allowed_actions(allowed, player_idx, {game.ActionType.CHECK: None, game.ActionType.BET: (expected_bet, expected_bet), game.ActionType.FOLD: None}) self.manager.act(game.Action(player_idx, game.ActionType.CHECK))
def test_no_blinds_reraise(self): self.initialize(game.Configuration( max_players=5, game_type=game.GameType.LIMIT, limits=(100, 200))) self.perform_actions([ game.Action(4, game.ActionType.BET, amount=100), game.Action(0, game.ActionType.CALL), game.Action(1, game.ActionType.RAISE, amount=100), game.Action(3, game.ActionType.FOLD), game.Action(4, game.ActionType.RAISE, amount=100), game.Action(0, game.ActionType.FOLD), game.Action(1, game.ActionType.CALL), ]) self.assertFalse(self.manager.current_hand.is_betting_active()) self.assertEqual(700, self.manager.current_hand.pot) self.assertEqual([900, 700, None, 1000, 700], self.get_stacks()) self.assertEqual([False, True, False, False, True], self.manager.current_hand.live_players())
def test_fold_preflop(self): self.manager.start_game() self.assertEqual(game.GameState.PRE_DEAL, self.manager.state) self.manager.proceed() self.assertEqual(game.GameState.HOLE_CARDS_DEALT, self.manager.state) self.manager.act(game.Action(0, game.ActionType.RAISE, 10)) fold_all(self.manager) self.recorder.clear() self.manager.proceed() self.assertEqual(game.GameState.PAYING_OUT, self.manager.state) self.assertEqual(1, len(self.recorder.events)) self.assertEqual( "Event(EventType.PAYING_OUT" + ", net_profit=[15, -5, -10, None]" ", pot_winnings=[35, 0, 0, None]" ")", str(self.recorder.events[0])) self.assertEqual(1015, self.manager.players[0].stack) self.assertEqual(995, self.manager.players[1].stack) self.assertEqual(990, self.manager.players[2].stack)
def test_check_call_all(self): # We are only checking the betting related events here. self.manager.start_game() self.assertEqual(game.GameState.PRE_DEAL, self.manager.state) # Pre flop self.recorder.clear() self.manager.proceed() self.assertEqual(game.GameState.HOLE_CARDS_DEALT, self.manager.state) self.assertEqual(4, len(self.recorder.events)) e = self.recorder.events[0] self.assertEqual(game.EventType.HOLE_CARDS_DEALT, e.event_type) e = self.recorder.events[1] self.assertEqual(game.EventType.ACTION, e.event_type) self.assertEqual(1, e.action.player_idx) self.assertEqual(game.ActionType.BLIND_BET, e.action.action_type) self.assertEqual(5, e.action.amount) e = self.recorder.events[2] self.assertEqual(game.EventType.ACTION, e.event_type) self.assertEqual(2, e.action.player_idx) self.assertEqual(game.ActionType.BLIND_BET, e.action.action_type) self.assertEqual(10, e.action.amount) e = self.recorder.events[3] self.assertEqual(game.EventType.ACTION_ON, e.event_type) self.assertEqual(0, e.hand_player.base_player.position) self.assertIsInstance(e.allowed, game.AllowedAction) with self.assertRaises(game.BettingActiveError): self.manager.proceed() self.recorder.clear() check_call_all(self.manager) self.assertEqual(5, len(self.recorder.events)) e = self.recorder.events[0] self.assertEqual(game.EventType.ACTION, e.event_type) self.assertEqual(0, e.action.player_idx) self.assertEqual(game.ActionType.CALL, e.action.action_type) e = self.recorder.events[1] self.assertEqual(game.EventType.ACTION_ON, e.event_type) self.assertEqual(1, e.hand_player.base_player.position) self.assertIsInstance(e.allowed, game.AllowedAction) e = self.recorder.events[2] self.assertEqual(game.EventType.ACTION, e.event_type) self.assertEqual(1, e.action.player_idx) self.assertEqual(game.ActionType.CALL, e.action.action_type) e = self.recorder.events[3] self.assertEqual(game.EventType.ACTION_ON, e.event_type) self.assertEqual(2, e.hand_player.base_player.position) self.assertIsInstance(e.allowed, game.AllowedAction) e = self.recorder.events[4] self.assertEqual(game.EventType.ACTION, e.event_type) self.assertEqual(2, e.action.player_idx) self.assertEqual(game.ActionType.CHECK, e.action.action_type) with self.assertRaisesRegex(game.ActionOutOfTurnError, "(0, None)"): self.manager.act(game.Action(0, game.ActionType.CALL)) for expected_state, expected_event_type in [ [game.GameState.FLOP_DEALT, game.EventType.FLOP_DEALT], [game.GameState.TURN_DEALT, game.EventType.TURN_DEALT], [game.GameState.RIVER_DEALT, game.EventType.RIVER_DEALT]]: self.recorder.clear() self.manager.proceed() self.assertEqual(expected_state, self.manager.state) self.assertEqual(2, len(self.recorder.events)) e = self.recorder.events[0] self.assertEqual(expected_event_type, e.event_type) e = self.recorder.events[1] self.assertEqual(game.EventType.ACTION_ON, e.event_type) self.assertEqual(1, e.hand_player.base_player.position) self.assertIsInstance(e.allowed, game.AllowedAction) with self.assertRaises(game.BettingActiveError): self.manager.proceed() self.recorder.clear() check_call_all(self.manager) self.assertEqual(5, len(self.recorder.events)) e = self.recorder.events[0] self.assertEqual(game.EventType.ACTION, e.event_type) self.assertEqual(1, e.action.player_idx) self.assertEqual(game.ActionType.CHECK, e.action.action_type) e = self.recorder.events[1] self.assertEqual(game.EventType.ACTION_ON, e.event_type) self.assertEqual(2, e.hand_player.base_player.position) self.assertIsInstance(e.allowed, game.AllowedAction) e = self.recorder.events[2] self.assertEqual(game.EventType.ACTION, e.event_type) self.assertEqual(2, e.action.player_idx) self.assertEqual(game.ActionType.CHECK, e.action.action_type) e = self.recorder.events[3] self.assertEqual(game.EventType.ACTION_ON, e.event_type) self.assertEqual(0, e.hand_player.base_player.position) self.assertIsInstance(e.allowed, game.AllowedAction) e = self.recorder.events[4] self.assertEqual(game.EventType.ACTION, e.event_type) self.assertEqual(0, e.action.player_idx) self.assertEqual(game.ActionType.CHECK, e.action.action_type) with self.assertRaisesRegex(game.ActionOutOfTurnError, "(0, None)"): self.manager.act(game.Action(0, game.ActionType.CALL))
def test_error_in_wrong_state(self): with self.assertRaisesRegex(game.ActionInWrongStateError, "GameState.WAITING_FOR_START"): self.manager.act(game.Action(0, game.ActionType.CALL))
def fold_all(manager): while manager.current_hand.is_betting_active(): allowed = manager.current_hand.allowed_action() manager.act(game.Action(player_idx=allowed.player_idx, action_type=game.ActionType.FOLD))