def test_collect_blinds(self): initial_stack = 1000 small_blind_size = 5 big_blind_size = small_blind_size * 2 button_player = self.setup_new_player("Button", initial_stack) player2 = self.setup_new_player("SmallBlind", initial_stack) player3 = self.setup_new_player("BigBlind", initial_stack) seating = Seating([button_player, player2, player3]) deck = Deck() dealer = Dealer(deck, seating) dealer.pot = Pot() dealer.collect_blinds(small_blind_size) self.assertEqual(initial_stack, button_player.stack) self.assertEqual(initial_stack - small_blind_size, player2.stack) self.assertEqual(initial_stack - big_blind_size, player3.stack) self.assertEqual(big_blind_size + small_blind_size, dealer.pot.total_count()) dealer.move_button() dealer.pot = Pot() dealer.collect_blinds(small_blind_size) self.assertEqual(initial_stack - big_blind_size, button_player.stack) self.assertEqual(initial_stack - small_blind_size, player2.stack) self.assertEqual(initial_stack - big_blind_size - small_blind_size, player3.stack) self.assertEqual(big_blind_size + small_blind_size, dealer.pot.total_count()) self.assertTrue(button_player in dealer.pot.chips_per_player) self.assertTrue(player3 in dealer.pot.chips_per_player)
def play_preflop(self, small_blind_size): self.big_blind_size = small_blind_size * 2 self.pot = Pot() self.collect_blinds(small_blind_size) self.deal_cards_to_players() bb_player = self.seating.big_blind_player() amount_to_match = small_blind_size * 2 return self.ask_players_for_actions(bb_player, amount_to_match, True)
def test_player_calls__when_players_allin_with_stack_that_is_too_small__side_pot_is_formed(self): player1 = Player("Player1", 1000) player2 = Player("Player2", 90) player3 = Player("Player3", 80) pot = Pot() pot.player_calls(player1, 100) pot.player_calls(player2, 90) pot.player_calls(player3, 80) pots = pot.get_all_pots() self.assertEqual(3, len(pots)) main_pot = pots[0] self.assertEqual(240, main_pot.pot_size()) self.assertTrue(player1 in main_pot.chips_per_player) self.assertTrue(player2 in main_pot.chips_per_player) self.assertTrue(player3 in main_pot.chips_per_player) side_pot = pots[1] self.assertEqual(20, side_pot.pot_size()) self.assertTrue(player1 in side_pot.chips_per_player) self.assertTrue(player2 in side_pot.chips_per_player) self.assertTrue(player3 not in side_pot.chips_per_player) side_pot = pots[2] self.assertEqual(10, side_pot.pot_size()) self.assertTrue(player1 in side_pot.chips_per_player) self.assertTrue(player2 not in side_pot.chips_per_player) self.assertTrue(player3 not in side_pot.chips_per_player)
def test_player_calls__when_two_players_allin__two_side_pots_are_formed(self): player1 = Player("Player1", 1000) player2 = Player("Player2", 600) player3 = Player("Player3", 200) pot = Pot() pot.player_calls(player3, 200) pot.player_calls(player1, 999) pot.player_calls(player2, 600) pots = pot.get_all_pots() self.assertEqual(3, len(pots)) main_pot = pots[0] self.assertEqual(600, main_pot.pot_size()) self.assertTrue(player1 in main_pot.chips_per_player) self.assertTrue(player2 in main_pot.chips_per_player) self.assertTrue(player3 in main_pot.chips_per_player) side_pot = pots[1] self.assertEqual(800, side_pot.pot_size()) self.assertTrue(player1 in side_pot.chips_per_player) self.assertTrue(player2 in side_pot.chips_per_player) self.assertTrue(player3 not in side_pot.chips_per_player) side_pot = pots[2] self.assertEqual(399, side_pot.pot_size()) self.assertTrue(player1 in side_pot.chips_per_player) self.assertTrue(player2 not in side_pot.chips_per_player) self.assertTrue(player3 not in side_pot.chips_per_player)
def test_player_calls__when_three_players_allin_with_stacks_that_are_too_small__three_side_pots_are_formed(self): player1 = Player("Player1", 1000) player2 = Player("Player2", 600) player3 = Player("Player3", 200) player4 = Player("Player4", 50) pot = Pot() pot.player_calls(player1, 999) pot.player_calls(player2, 600) pot.player_calls(player3, 200) pot.player_calls(player4, 50) pots = pot.get_all_pots() self.assertEqual(4, len(pots)) main_pot = pots[0] self.assertEqual(200, main_pot.pot_size()) self.assertTrue(player1 in main_pot.chips_per_player) self.assertTrue(player2 in main_pot.chips_per_player) self.assertTrue(player3 in main_pot.chips_per_player) self.assertTrue(player4 in main_pot.chips_per_player) side_pot = pots[1] self.assertEqual(450, side_pot.pot_size()) self.assertTrue(player1 in side_pot.chips_per_player) self.assertTrue(player2 in side_pot.chips_per_player) self.assertTrue(player3 in side_pot.chips_per_player) self.assertTrue(player4 not in side_pot.chips_per_player) side_pot = pots[2] self.assertEqual(800, side_pot.pot_size()) self.assertTrue(player1 in side_pot.chips_per_player) self.assertTrue(player2 in side_pot.chips_per_player) self.assertTrue(player3 not in side_pot.chips_per_player) self.assertTrue(player4 not in side_pot.chips_per_player) side_pot = pots[3] self.assertEqual(399, side_pot.pot_size()) self.assertTrue(player1 in side_pot.chips_per_player) self.assertTrue(player2 not in side_pot.chips_per_player) self.assertTrue(player3 not in side_pot.chips_per_player) self.assertTrue(player4 not in side_pot.chips_per_player)
def test_collect_blinds__when_player_dont_have_enough_chips(self): initial_stack = 100 small_blind_size = 5 big_blind_size = small_blind_size * 2 button_player = self.setup_new_player("Button", 100) player2 = self.setup_new_player("SmallBlind", small_blind_size - 1) player3 = self.setup_new_player("BigBlind", big_blind_size - 1) seating = Seating([button_player, player2, player3]) deck = Deck() dealer = Dealer(deck, seating) dealer.pot = Pot() dealer.collect_blinds(small_blind_size) self.assertEqual(initial_stack, button_player.stack) self.assertEqual(0, player2.stack) self.assertEqual(0, player3.stack) self.assertEqual(13, dealer.pot.total_count()) self.assertTrue(player2 in dealer.pot.chips_per_player) self.assertTrue(player3 in dealer.pot.chips_per_player)
class Dealer: def __init__(self, deck, seating, random_seed_for_shuffling=None): self.community_cards = [] self.deck = deck self.seating = seating self.pot = None self.big_blind_size = None self.random_seed_for_shuffling = random_seed_for_shuffling def deal_cards_to_players(self): for player in self.seating.players: player.receive_cards(self.deck.draw(2)) def move_button(self): self.seating.move_button() def collect_blinds(self, small_blind_size): for player in self.seating.players: player.money_in_pot = 0 sb_player = self.seating.small_blind_player() bb_player = self.seating.big_blind_player() available_size_of_small_blind = min(small_blind_size, sb_player.stack) self.pot.player_calls(sb_player, available_size_of_small_blind) available_size_of_big_blind = min(small_blind_size * 2, bb_player.stack) self.pot.player_calls(bb_player, available_size_of_big_blind) def setup_deck(self): self.deck = Deck() self.deck.initialize() self.deck.shuffle(self.random_seed_for_shuffling) def add_community_cards(self, card_count): self.community_cards += self.deck.draw(card_count) for player in self.seating.players: player.see_community_cards(self.community_cards) print("Community cards: " + ", ".join([str(card) for card in self.community_cards])) def play_preflop(self, small_blind_size): self.big_blind_size = small_blind_size * 2 self.pot = Pot() self.collect_blinds(small_blind_size) self.deal_cards_to_players() bb_player = self.seating.big_blind_player() amount_to_match = small_blind_size * 2 return self.ask_players_for_actions(bb_player, amount_to_match, True) def play_flop(self): self.add_community_cards(3) last_player_to_go = self.seating.button_player() return self.ask_players_for_actions(last_player_to_go, last_player_to_go.money_in_pot, True) def play_turn(self): self.add_community_cards(1) last_player_to_go = self.seating.button_player() return self.ask_players_for_actions(last_player_to_go, last_player_to_go.money_in_pot, True) def play_river(self): self.add_community_cards(1) last_player_to_go = self.seating.button_player() winner = self.ask_players_for_actions(last_player_to_go, last_player_to_go.money_in_pot, True) if winner: return True else: for pot in self.pot.get_all_pots(): winners = card_showdown.find_winners(pot.chips_per_player, self.community_cards) players_share = int(pot.pot_size() / len(winners)) for player in winners: player.stack += players_share def ask_players_for_actions(self, player_who_raised, new_raised_amount, include_last_player): next_player = self.seating.next_player_after_player(player_who_raised) amount_of_calls_to_make = len(self.seating.players) if not include_last_player: amount_of_calls_to_make -= 1 for i in range(amount_of_calls_to_make): if next_player.cards and next_player.stack > 0: p_action, p_amount = next_player.act(new_raised_amount) if p_action == Action.ACTION_FOLD: print(str(next_player) + " folds") self.player_folds(next_player) elif p_action == Action.ACTION_CALL: print(str(next_player) + " calls " + str(p_amount)) self.pot.player_calls(next_player, p_amount) elif p_action == Action.ACTION_RAISE: print(str(next_player) + " raises to " + str(p_amount)) self.pot.player_calls(next_player, p_amount) return self.ask_players_for_actions( next_player, p_amount, False) if self.is_winner_determined(): winner = list(self.pot.chips_per_player.keys())[0] winner.stack += self.pot.pot_size() return True next_player = self.seating.next_player_after_player(next_player) return False def is_winner_determined(self): return len(self.pot.chips_per_player) == 1 def player_folds(self, player): player.money_in_pot = 0 player.release_cards() if player in self.pot.chips_per_player: self.pot.size += self.pot.chips_per_player[player] self.pot.chips_per_player.pop(player, None)