def check_game_over(self): """Checks if the game is over. If the game is not over (i.e. all but one player has no chips), ask the user if they would like to continue the game. Returns: bool: True if the game is over, False otherwise. """ for player in self.get_active_players(): if player.chips == 0: player.is_in_game = False active_players = self.get_active_players() if len(active_players) == 1: text_prompt.show_table(self.players, self.table) text_prompt.show_game_winners(self.players, [active_players[0].name]) return True else: while True: text_prompt.clear_screen() user_choice = io_utils.input_no_return( "Continue on to next hand? Press (enter) to continue or (n) to stop. " ) if 'n' in user_choice.lower(): max_chips = max(self.get_active_players(), key=lambda player: player.chips).chips winners_names = [ player.name for player in self.get_active_players() if player.chips == max_chips ] text_prompt.show_table(self.players, self.table) text_prompt.show_game_winners(self.players, winners_names) return True return False
def determine_winners(self): """Determine the winners of each pot and award them their chips.""" if self.table.pots[-1][0] == 0: self.table.pots = self.table.pots[:-1] unfolded_players = [ player for player in self.get_active_players() if not player.is_folded ] if len(unfolded_players) == 1: winnings = 0 for pot in self.table.pots: winnings += pot[0] winner = unfolded_players[0] winner.chips += winnings text_prompt.show_table(self.players, self.table) text_prompt.show_default_winner_fold(winner.name) else: # If only 1 player is eligible for last side pot (i.e. other players folded/all-in), award player that pot players_eligible_last_pot = [] for player in self.table.pots[-1][1]: if not player.is_folded: players_eligible_last_pot.append(player) if len(players_eligible_last_pot) == 1: hand_winner = players_eligible_last_pot[0] text_prompt.show_table(self.players, self.table) text_prompt.show_default_winner_eligibility( hand_winner.name, len(self.table.pots) - 1) hand_winner.chips += self.table.pots[-1][0] self.table.pots = self.table.pots[:-1] while len(self.table.community) < 5: self.table.community.extend(self.deck.deal(1)) self.showdown()
def reset_table(self) -> None: active_players = self.get_active_players() self.table.reset(active_players) if self.table.check_increase_big_blind(): text_prompt.clear_screen() text_prompt.show_table(self.players, self.table) text_prompt.show_blind_increase(self.table.big_blind, self.long_pause)
def run_big_blind_bet(self) -> None: player = next(player for player in self.players if player.is_BB) text_prompt.show_bet_blind(player.name, 'big', self.pause) wentAllIn = self.table.take_big_blind(player) if wentAllIn: text_prompt.show_player_move(player, BettingMove.ALL_IN, self.pause) text_prompt.show_table(self.players, self.table)
def deal_hole(self) -> None: """Deals two cards to each player. In poker, you deal one card to each player at a time. """ text_prompt.show_table(self.players, self.table, self.short_pause) text_prompt.show_dealing_hole(self.dealer.name, self.pause) for i in range(2): for player in self.get_active_players(): card = self.deck.deal(1) player.hand.extend(card)
def run_round_of_betting(self): """Runs a round of betting.""" self.table.num_times_raised = 0 active_players = self.get_active_players() if self.phase is Phase.PREFLOP: self.run_small_blind_bet() self.run_big_blind_bet() first_act = self.get_index_first_act() # End round of betting when all but one player fold or when all unfolded players have locked in their bets self.bet_util_all_locked_in(first_act, active_players) for player in active_players: if not player.is_folded and not player.is_all_in: player.is_locked = False self.table.calculate_side_pots(active_players) text_prompt.show_table(self.players, self.table)
def deal_cards(self) -> None: """Deals cards to the hold and the community.""" if self.phase is Phase.PREFLOP: text_prompt.show_table(self.players, self.table) text_prompt.show_phase_change_alert(self.phase, self.dealer.name, self.long_pause) self.deal_hole() elif self.phase is Phase.FLOP: text_prompt.show_phase_change_alert(self.phase, self.dealer.name, self.long_pause) self.deal_community(3) else: text_prompt.show_phase_change_alert(self.phase, self.dealer.name, self.long_pause) self.deal_community(1) text_prompt.show_table(self.players, self.table)
def showdown(self): """Runs the showdown phase.""" text_prompt.show_table(self.players, self.table) # Need to fix this # text_prompt.show_phase_change_alert('Showdown', self.dealer, self.pause) # Divvy chips to the winner(s) of each pot/side pot for i in reversed(range(len(self.table.pots))): showdown_players = [] for player in self.table.pots[i][1]: if not player.is_folded: showdown_players.append(player) hand_winners = hand_ranking_utils.determine_showdown_winner( showdown_players, self.table.community) for winner in hand_winners: winner.chips += int(self.table.pots[i][0] / len(hand_winners)) text_prompt.show_showdown_results(self.players, self.table, hand_winners, showdown_players, pot_num=i)
def bet_util_all_locked_in(self, first_act: int, active_players: list[Player]) -> None: betting_index = first_act while True: if all(player.is_locked or player.is_all_in for player in active_players): break if [player.is_folded for player in active_players].count(False) == 1: break betting_player = active_players[betting_index % len(active_players)] if betting_player.is_folded or betting_player.is_all_in: betting_index += 1 continue self.table.update_raise_amount(self.phase) move = betting_player.choose_next_move(self.table.raise_amount, self.table.num_times_raised, self.table.last_bet) self.table.take_bet(betting_player, move) text_prompt.show_player_move(betting_player, move, self.pause, betting_player.bet) if move is BettingMove.RAISED or move is BettingMove.BET: for active_player in active_players: if not active_player.is_folded: active_player.is_locked = False for person in active_players: if person.is_all_in: person.is_locked = True elif move is BettingMove.ALL_IN: pass # if move is BettingMove.FOLDED and betting_player.is_human: if move is BettingMove.FOLDED and isinstance( betting_player, Human): self.set_game_speed(is_fast=True) betting_player.is_locked = True betting_index += 1 text_prompt.show_table(self.players, self.table)