def new_round(self): if len(self.players) < 2: raise PokerRuleViolationException( "To start a new round, there must be at least 2 players") if self.bet_round != BetRound.GAME_OVER: raise PokerRuleViolationException( "Cant start new round, previous round isn't over yet") self.active_players = self.players[:] self.dealer = self.next_seat(self.dealer) self.pots = [Pot()] self.deck = Deck() self.deck.shuffle() self.bet_round = BetRound.PREFLOP if len(self.players) == 2: self.small_blind_player = self.dealer else: self.small_blind_player = self.next_seat(self.dealer) self.big_blind_player = self.next_seat(self.small_blind_player) self.board = [] for player in self.players: player._has_called = False player.bet = 0 player.hand = self.deck.draw(2) self.last_bet_raise_delta = self.big_blind self.active_players[self.small_blind_player].bet_small_blind() self.active_players[self.big_blind_player].bet_big_blind() self.set_next_player(from_player_idx=self.big_blind_player)
def raise_bet(self, amount): self._check_player_may_act() delta = amount - self.to_call_amount() if delta <= 0: raise PokerRuleViolationException( f"Raise amount is smaller than or equal to to_call amount, consider calling instead: to_call=={self.to_call_amount()}, amount=={amount}" ) if amount > self.stakes: raise PokerRuleViolationException("Cant bet more than he has got") # NOT ALL IN if amount < self.stakes: if delta < self.table.last_bet_raise_delta: raise PokerRuleViolationException( f"Delta amount of bet/raise must be at least the last delta amount, delta== {delta}, last_delta=={self.table.last_bet_raise_delta}" ) self.table.last_bet_raise = delta # ALL IN --> self.stakes == amount else: if delta > self.table.last_bet_raise_delta: self.table.last_bet_raise_delta = delta self.table.reset_players_called_var() self._bet(amount) self._has_called = True self.table.set_next_player()
def _bet(self, amount): if amount > self.stakes: raise PokerRuleViolationException("Can't bet more than he has got") if amount < 0: raise PokerRuleViolationException("Can't bet less than 0") self.bet += amount self.stakes -= amount self.table.bet(amount, self)
def _check_player_may_act(self): if self.has_called: raise PokerRuleViolationException("This Player has already called") if self.table.bet_round == BetRound.SHOWDOWN or self.table.bet_round == BetRound.GAME_OVER: raise PokerRuleViolationException("Cannot bet, round is over") if self.table.next_player_idx == -1: raise PokerRuleViolationException( "This betround has already ended") if self != self.table.next_player: raise PokerRuleViolationException("It's not this players turn")
def action_from_amount(self, amount): self._check_player_may_act() # CHECK || FOLD if amount == 0: if self.to_call_amount() == 0: self.call_check() else: self.fold() return # CALL || RAISE delta = amount - self.to_call_amount() # IF ALL-IN if amount == self.stakes: if delta <= 0: self.call_check() else: self.raise_bet(amount) else: if delta == 0: self.call_check() elif delta > 0: self.raise_bet(amount) else: raise PokerRuleViolationException( f"Cannot bet less than to call amount: amount=={amount}, to_call=={self.to_call_amount()}" )
def __init__(self, small_blind=25, big_blind=50): if small_blind < 0: raise PokerRuleViolationException("Small blind must be at least 0") if big_blind < 0: raise PokerRuleViolationException("Big blind must be at least 0") if big_blind < small_blind: raise PokerRuleViolationException( "Big blind cant be less than small blind") self.players = [] self.active_players = [] self.small_blind = small_blind self.big_blind = big_blind self.dealer = -1 self.small_blind_player = -1 self.big_blind_player = -1 self.pots = [] self.deck = None self.bet_round = BetRound.GAME_OVER self.board = [] self.last_bet_raise_delta = big_blind self.next_player_idx = 0
def start_next_bet_round(self): if not self.all_players_called(): raise Exception("Everyone must call first") self.last_bet_raise_delta = self.big_blind if len(self.active_players) <= 1: self.bet_round = BetRound.SHOWDOWN return if self.check_everyone_all_in(): self.board += self.deck.draw(5 - len(self.board)) self.bet_round = BetRound.SHOWDOWN return if self.bet_round == BetRound.PREFLOP: self.board = self.deck.draw(3) self.bet_round = BetRound.FLOP self.reset_players_called_var() self.set_next_player(from_player_idx=self.dealer) elif self.bet_round == BetRound.FLOP: self.board += self.deck.draw(1) self.bet_round = BetRound.TURN self.reset_players_called_var() self.set_next_player(from_player_idx=self.dealer) elif self.bet_round == BetRound.TURN: self.board += self.deck.draw(1) self.bet_round = BetRound.RIVER self.reset_players_called_var() self.set_next_player(from_player_idx=self.dealer) elif self.bet_round == BetRound.RIVER: self.bet_round = BetRound.SHOWDOWN elif self.bet_round == BetRound.SHOWDOWN: raise PokerRuleViolationException( "After the showdown, the round ends")