def get_winners(self, shared_cards: List[Card]) -> List[Player]: winners: List[Player] = [] best_hand: Hand = None for player in self.players: hand = best_possible_hand(shared_cards, player.cards) if best_hand is None or hand > best_hand: winners = [player] best_hand = hand elif hand == best_hand: winners.append(player) return winners
def showdown(self) -> List[str]: while len(self.shared_cards) < 5: self.shared_cards.append(self.cur_deck.draw()) messages = [ "We have reached the end of betting. " "All cards will be revealed." ] messages.append(" ".join(str(card) for card in self.shared_cards)) for player in self.pot.in_pot(): messages.append("**{}'s** hand: **{} {}**".format( player.user.mention, player.cards[0], player.cards[1])) winners = self.pot.get_winners(self.shared_cards) for winner, winnings in sorted(winners.items(), key=lambda item: item[1]): hand_name = str(best_possible_hand(self.shared_cards, winner.cards)) messages.append("**{}** wins $***{}*** with a **{}**.".format( winner.user.mention, winnings, hand_name)) print("{} WINS +{}".format(winner.user.name, winnings)) winner.balance += winnings # Remove players that went all in and lost i = 0 while i < len(self.players): player = self.players[i] if player.balance > 0: i += 1 else: messages.append( "**{}** has been knocked out of the game!".format( player.user.mention)) print("{} OUT.".format(player.user.name)) self.players.pop(i) if len(self.players) == 1: # There's only one player, so they win messages.append( "**{}** wins the game! Congratulations!".format( self.players[0].user.mention)) print("WINNER {}\n".format(self.players[0].name)) self.state = GameState.NO_GAME return messages if i <= self.dealer_index: self.dealer_index -= 1 # Go on to the next round self.state = GameState.NO_HANDS self.next_dealer() messages += self.status_between_rounds() return messages
def test_ranking(public: List[Card], hand1: HoleCards, hand2: HoleCards, expected: int) -> bool: best_hand1 = best_possible_hand(public, hand1) best_hand2 = best_possible_hand(public, hand2) if best_hand1 == best_hand2: winner = 0 elif best_hand1 > best_hand2: winner = 1 else: winner = 2 if winner == expected: return True expected_strs = ["a tie", "hand 1 to win", "hand 2 to win"] actualstrs = ["the two hands tied", "hand 1 won", "hand 2 won"] print("Test failed! Expected", expected_strs[expected], "but", actualstrs[winner]) print("Shared cards:", " ".join(str(card) for card in public)) print("Hole cards 1: ", " ".join(str(card) for card in hand1)) print("Hole cards 2: ", " ".join(str(card) for card in hand2)) print("Hand 1: ", " ".join(str(card) for card in best_hand1.cards)) print("Hand 2: ", " ".join(str(card) for card in best_hand2.cards)) print("") return False
def showdown(self) -> List[str]: while len(self.shared_cards) < 5: self.shared_cards.append(self.cur_deck.draw()) messages = [ "We have reached the end of betting. " "All cards will be revealed." ] messages.append(" ".join(str(card) for card in self.shared_cards)) for player in self.pot.in_pot(): messages.append(f"{player.user.display_name}'s hand: " f"{player.cards[0]} {player.cards[1]}") winners = self.pot.get_winners(self.shared_cards) for winner, winnings in sorted(winners.items(), key=lambda item: item[1]): hand_name = str(best_possible_hand(self.shared_cards, winner.cards)) messages.append( f"{winner.user.display_name} wins ${winnings} with a {hand_name}." ) winner.balance += winnings # Remove players that went all in and lost i = 0 while i < len(self.players): player = self.players[i] if player.balance > 0: i += 1 else: messages.append( f"{player.user.display_name} has been knocked out of the game!" ) self.players.pop(i) if len(self.players) == 1: # There's only one player, so they win messages.append( f"{self.players[0].user.display_name} wins the game! " "Congratulations!") self.state = GameState.NO_GAME return messages if i <= self.dealer_index: self.dealer_index -= 1 # Go on to the next round self.state = GameState.NO_HANDS self.next_dealer() messages += self.status_between_rounds() return messages