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 = [ EmbedField( "Betting has concluded. Dealing and revealing the remaining cards:", " ".join(str(card) for card in self.shared_cards), False, ) ] winners = self.pot.get_winners(self.shared_cards) for winner, winnings in sorted(winners.items(), key=lambda item: item[1]): winner.balance += winnings for player in self.pot.in_pot(): hand_name = str(best_possible_hand(self.shared_cards, player.cards)) value = f"{player.cards[0]} {player.cards[1]} ({hand_name})" if player in winners: name = f"**{player.user.name}:** ${player.balance} (+${winners[player]})" value = f"{player.cards[0]} {player.cards[1]} **({hand_name})**" elif player.balance == 0: name = f"~~{player.user.name}~~: ${player.balance}" else: name = f"{player.user.name}: ${player.balance}" messages.append(EmbedField(name, value, True)) # 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: self.players.pop(i) if len(self.players) == 1: # There's only one player, so they win messages.append(EmbedField(f":tada:{self.players[0].user.name} wins the game!", "Congratulations!", False)) 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() if not self.options["auto-deal"]: 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.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.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.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.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