def test_Player(self): """Test Player class""" player = Player(name="Bob") self.assertIsNotNone(player) self.assertEqual(str(player), "Bob") self.assertTrue(player.is_sitting_out()) self.assertEqual(player.get_status(), "sitting out") with self.assertRaises(ZeroStackException): player.make_active() player2 = Player(stack=10) self.assertEqual(str(player2), "Unnamed Player") self.assertTrue(player2.is_active()) self.assertEqual(player2.get_status(), "active") player2.sit_out() self.assertTrue(player2.is_sitting_out()) player2.make_active() self.assertTrue(player2.is_active()) player.new_hand() self.assertTrue(player.is_active()) player.muck_hand() self.assertTrue(player.is_folded()) self.assertEqual(player.get_status(), "folded") player.message("This should go nowhere") player.stack = 100 action = Action.new_bet(30) player.process_action(action) self.assertEqual(player.stack, 70) self.assertEqual(player.bet, 30) action = Action.new_bet(100) self.assertRaises(InvalidActionException, player.process_action, action) player.win(10) self.assertEqual(player.stack, 80)
def test_BettingRound_called_to_blind(self): """Test of BettingRound with calls around to blind. Blind should still have option to act.""" players = [ Player(stack=500), Player(stack=100), Player(stack=1000), Player(stack=700) ] table = Table() table.seat_players(players, in_order=True) message_handler = MessageHandler(table, self.console) pot = Pot(players) round = BettingRound(table, pot, message_handler) action_is_on = players[0] round.set_action(action_is_on) # Player 0 50 blind, fold, fold, fold round.process_action(Action.new_blind(50)) round.process_action(Action.new_fold()) round.process_action(Action.new_call(50)) round.process_action(Action.new_fold()) # Back on blind self.assertFalse(round.is_pot_good()) round.process_action(Action.new_check()) self.assertTrue(round.is_pot_good()) round.sweep_bets_into_pot() # At this point we should have a pot of 50 with only player # 0 contending. pot = round.pot self.assertEqual(pot.amount, 100) self.assertListEqual(pot.contending_players, [players[0], players[2]]) self.assertIsNone(pot.parent)
def test_BettingRound_blind_walk(self): """Test of BettingRound with a walk for the blind""" players = [ Player(stack=500), Player(stack=100), Player(stack=1000), Player(stack=700) ] table = Table() table.seat_players(players, in_order=True) message_handler = MessageHandler(table, self.console) pot = Pot(players) round = BettingRound(table, pot, message_handler) action_is_on = players[0] round.set_action(action_is_on) # Player 0 50 blind, fold, fold, fold round.process_action(Action.new_blind(50)) round.process_action(Action.new_fold()) round.process_action(Action.new_fold()) round.process_action(Action.new_fold()) self.assertTrue(round.is_pot_good()) round.sweep_bets_into_pot() # At this point we should have a pot of 50 with only player # 0 contending. pot = round.pot self.assertEqual(pot.amount, 50) self.assertListEqual(pot.contending_players, [players[0]]) self.assertIsNone(pot.parent)
def test_Game(self): """Test Game class""" players = [ Player(name="Player One", stack=1000), Player(name="Player Two", stack=1000), Player(name="Player Three", stack=1000) ] table = Table(players = players) structure = Structure(Structure.LIMIT, ante=5, blinds=[10]) game = Game(table, structure, console=self.console) self.assertIsNotNone(game) self.assertEqual(game.table, table) self.assertEqual(game.structure, structure) # Default MessageHander should be created. self.assertIsNotNone(game.message_handler) game.message("Test message") game.debug("Test debug message") # Simulate play_hand() hand_state = HandState(table, game.message_handler) game.antes(hand_state) self.assertEqual(hand_state.pot.amount, 15) game.action_to_left_of_dealer(hand_state) betting_round = hand_state.get_current_betting_round() self.assertIsInstance(betting_round, BettingRound) self.assertIsInstance(betting_round.get_action_player(), Player) game.blinds(hand_state) self.assertEqual(betting_round.total_pot(), 25) # Antes + 10 blind game.deal_hands(hand_state) for player in players: self.assertEqual(len(player._hand), 5) # Simulate betting_round() self.assertFalse(betting_round.is_pot_good()) action_request = game._get_action_request(hand_state) self.assertIsInstance(action_request, ActionRequest) # Should be call of blind self.assertTrue(action_request.is_call_request()) self.assertEqual(action_request.amount, 10) self.assertEqual(action_request.raise_amount, 20) # Fold UTG betting_round.process_action(Action.new_fold()) self.assertFalse(betting_round.is_pot_good()) # Should be same request as before action_request2 = game._get_action_request(hand_state) self.assertIsInstance(action_request2, ActionRequest) self.assertTrue(action_request2.is_call_request()) self.assertEqual(action_request2.amount, 10) self.assertEqual(action_request2.raise_amount, 20) # Now raise betting_round.process_action(Action.new_raise(20)) self.assertFalse(betting_round.is_pot_good()) # Action back on blind action_request3 = game._get_action_request(hand_state) self.assertIsInstance(action_request3, ActionRequest) self.assertTrue(action_request3.is_call_request()) self.assertEqual(action_request3.amount, 10) self.assertEqual(action_request3.raise_amount, 20) betting_round.process_action(Action.new_call(10)) self.assertTrue(betting_round.is_pot_good()) game.pot_to_high_hand(hand_state)
def test_Action_check(self): """Test check Action""" action = Action.new_check() self.assertIsNotNone(action) self.assertTrue(action.is_check()) self.assertEqual(action.amount, 0) self.assertFalse(action.is_all_in()) self.assertEqual(str(action), "check")
def test_Action_call(self): """Test call Action""" action = Action.new_call(200, all_in=True) self.assertIsNotNone(action) self.assertTrue(action.is_call()) self.assertEqual(action.amount, 200) self.assertTrue(action.is_all_in()) self.assertEqual(str(action), "call 200 (all-in)")
def test_Action_blind(self): """Test blind Action""" action = Action.new_blind(50) self.assertIsNotNone(action) self.assertTrue(action.is_blind()) self.assertEqual(action.amount, 50) self.assertFalse(action.is_all_in()) self.assertEqual(str(action), "blind 50")
def test_Action_raise(self): """Test raise Action""" action = Action.new_raise(500, all_in=True) self.assertIsNotNone(action) self.assertTrue(action.is_raise()) self.assertEqual(action.amount, 500) self.assertTrue(action.is_all_in()) self.assertEqual(str(action), "raise 500 (all-in)")
def test_Action_ante(self): """Test ante Action""" action = Action.new_ante(1) self.assertIsNotNone(action) self.assertTrue(action.is_ante()) self.assertEqual(action.amount, 1) self.assertFalse(action.is_all_in()) self.assertEqual(str(action), "ante 1")
def test_ActionRequest(self): """Test ActionRequest class""" request = ActionRequest(ActionRequest.ANTE_REQUEST, 10) self.assertIsNotNone(request) self.assertTrue(request.is_ante_request()) self.assertEqual(request.amount, 10) self.assertEqual(str(request), "ante request for 10") request.validate_action(Action.new_ante(10))
def test_Action_fold(self): """Test fold Action""" action = Action.new_fold() self.assertIsNotNone(action) self.assertTrue(action.is_fold()) self.assertEqual(action.amount, 0) self.assertFalse(action.is_all_in()) self.assertEqual(str(action), "fold")
def test_BettingRound_with_side_pots(self): """Test of BettingRound with side pots.""" players = [ Player(stack=500), Player(stack=100), Player(stack=1000), Player(stack=700) ] table = Table() table.seat_players(players, in_order=True) message_handler = MessageHandler(table, self.console) pot = Pot(players) round = BettingRound(table, pot, message_handler) action_is_on = players[0] round.set_action(action_is_on) # Player 0 50 blind, player 1 100 blind (allin), # player 2 raises to 300, player 4 calls round.process_action(Action.new_blind(50)) round.process_action(Action.new_blind(100, all_in=True)) round.process_action(Action.new_raise(300)) round.process_action(Action.new_call(300)) # Player 0 raises 450 all-in, player 2 raises 700 all-in # Player 3 folds round.process_action(Action.new_raise(450, all_in=True)) # Make sure player 1, who is all-in, is skipped self.assertEqual(round.get_action_player(), players[2]) round.process_action(Action.new_raise(700, all_in=True)) round.process_action(Action.new_fold()) self.assertTrue(round.is_pot_good()) round.sweep_bets_into_pot() # At this point we should have a main point with 400 # between 0,1 and 2, a side pot with 1000 between players 0 and 2, # and a side pot of 500 with just player 2. side_pot1 = round.pot self.assertEqual(side_pot1.amount, 500) self.assertListEqual(side_pot1.contending_players, [players[2]]) self.assertIsNotNone(side_pot1.parent)
def test_Action_bet(self): """Test bet Action""" action = Action.new_bet(100) self.assertIsNotNone(action) self.assertTrue(action.is_bet()) self.assertFalse(action.is_raise()) self.assertEqual(action.amount, 100) self.assertFalse(action.is_all_in()) self.assertEqual(str(action), "bet 100")
def test_opening_bet_ActionRequest(self): """Test ActionRequest for opening bet""" amount = 50 request = ActionRequest.new_opening_bet_request(amount) self.assertIsNotNone(request) self.assertTrue(request.is_opening_bet_request()) self.assertEqual(request.amount, amount) request.validate_action(Action.new_check()) request.validate_action(Action.new_bet(amount)) request.validate_action(Action.new_fold()) with self.assertRaises(InvalidActionException): request.validate_action(Action.new_ante(amount)) with self.assertRaises(InvalidActionException): request.validate_action(Action.new_blind(amount)) with self.assertRaises(InvalidActionException): request.validate_action(Action.new_call(amount)) with self.assertRaises(InvalidActionException): request.validate_action(Action.new_bet(amount+1)) with self.assertRaises(InvalidActionException): request.validate_action(Action.new_raise(amount))
def test_call_ActionRequest(self): """Test ActionRequest for call""" amount = 50 request = ActionRequest.new_call_request(amount, raise_amount=amount*2) self.assertIsNotNone(request) self.assertTrue(request.is_call_request()) self.assertEqual(request.amount, amount) self.assertEqual(request.raise_amount, amount*2) self.assertEqual(str(request), "call request for 50 or raise of 100") request.validate_action(Action.new_call(amount)) request.validate_action(Action.new_raise(amount*2)) request.validate_action(Action.new_fold()) with self.assertRaises(InvalidActionException): request.validate_action(Action.new_ante(amount)) with self.assertRaises(InvalidActionException): request.validate_action(Action.new_blind(amount)) with self.assertRaises(InvalidActionException): request.validate_action(Action.new_call(amount+1)) with self.assertRaises(InvalidActionException): request.validate_action(Action.new_bet(amount)) with self.assertRaises(InvalidActionException): request.validate_action(Action.new_raise(amount*2-1))
def test_ante_ActionRequest(self): """Test ActionRequest for ante""" amount = 20 request = ActionRequest.new_ante_request(amount) self.assertIsNotNone(request) self.assertTrue(request.is_ante_request()) self.assertEqual(request.amount, amount) request.validate_action(Action.new_ante(amount)) request.validate_action(Action.new_fold()) with self.assertRaises(InvalidActionException): request.validate_action(Action.new_ante(amount+1)) with self.assertRaises(InvalidActionException): request.validate_action(Action.new_call(amount)) with self.assertRaises(InvalidActionException): request.validate_action(Action.new_bet(amount)) with self.assertRaises(InvalidActionException): request.validate_action(Action.new_raise(amount))
def test_BettingRound(self): """Test BettingRound class""" players = [ Player(stack=1000), Player(stack=1000), Player(stack=1000), Player(stack=1000) ] table = Table() table.seat_players(players, in_order=True) message_handler = MessageHandler(table, self.console) pot = Pot(players) round = BettingRound(table, pot, message_handler) self.assertIsNotNone(round) self.assertEqual(round.table, table) self.assertEqual(round.pot, pot) self.assertRaises(PokerGameStateException, round.get_action_player) action_is_on = players[1] round.set_action(action_is_on) self.assertEqual(round.get_action_player(), action_is_on) self.assertIsNone(round.last_to_bet) self.assertEqual(len(round.action_record), 0) self.assertEqual(round.action_to_next_player(), players[2]) self.assertEqual(round.action_to_next_player(), players[3]) self.assertEqual(round.action_to_next_player(), players[0]) self.assertEqual(round.action_to_next_player(), players[1]) self.assertEqual(round.required_to_call(players[0]), 0) self.assertFalse(round.is_pot_good()) # Have all players ante round.process_action(Action.new_ante(5)) self.assertEqual(players[1].stack, 995) self.assertEqual(round.get_action_player(), players[2]) self.assertEqual(round.last_to_bet, players[1]) round.process_action(Action.new_ante(5)) self.assertEqual(players[2].stack, 995) self.assertEqual(round.get_action_player(), players[3]) self.assertEqual(round.last_to_bet, players[1]) round.process_action(Action.new_ante(5)) self.assertEqual(players[3].stack, 995) self.assertEqual(round.get_action_player(), players[0]) self.assertEqual(round.last_to_bet, players[1]) round.process_action(Action.new_ante(5)) self.assertEqual(players[0].stack, 995) self.assertEqual(round.get_action_player(), players[1]) self.assertEqual(round.last_to_bet, players[1]) self.assertTrue(round.is_pot_good()) self.assertEqual(round.sweep_bets_into_pot(), pot) self.assertEqual(pot.amount, 20) # New round round = BettingRound(table, pot, message_handler) self.assertIsNotNone(round) round.set_action(action_is_on) # Player 1 blinds, 2 blinds, 3 fold, 0 raises to 300 round.process_action(Action.new_blind(50)) self.assertEqual(players[1].bet, 50) round.process_action(Action.new_blind(100)) round.process_action(Action.new_fold()) self.assertTrue(players[3].is_folded()) round.process_action(Action.new_raise(300)) self.assertFalse(round.is_pot_good()) self.assertEqual(round.get_action_player(), players[1]) self.assertEqual(len(round.action_record), 4) self.assertEqual(round.total_pot(), 470) # Player 1 calls, 2 raises all in, 3 has folded, 0 folds self.assertEqual(round.required_to_call(), 250) round.process_action(Action.new_call(250)) self.assertEqual(round.required_to_call(), 200) round.process_action(Action.new_raise(895, all_in=True)) self.assertTrue(players[2].is_all_in()) # Make sure we skipped player 3 who folded earlier self.assertEqual(round.get_action_player(), players[0]) self.assertEqual(round.required_to_call(), 695) round.process_action(Action.new_fold()) self.assertFalse(round.is_pot_good()) self.assertEqual(round.total_pot(), 1615) # Plater 1 calls and pot should be good self.assertEqual(round.required_to_call(), 695) round.process_action(Action.new_call(695, all_in=True)) self.assertTrue(round.is_pot_good()) self.assertEqual(round.total_pot(), 2310) round.sweep_bets_into_pot() # Should only be main pot self.assertIsNone(pot.parent) # All players bets should have beet swept into pot for player in players: self.assertEqual(0, player.bet, "Bet for %s == %d != 0" % (player, player.bet)) self.assertEqual(pot.amount, 2310)