Skip to content

Commit

Permalink
Merge pull request #24 from cis3296s24/Joe_2
Browse files Browse the repository at this point in the history
after flop shows, the winner displayed
  • Loading branch information
Vstroma authored Apr 25, 2024
2 parents f02bbaf + 1966053 commit 502a969
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
30 changes: 30 additions & 0 deletions card.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,36 @@ def simulate_poker_games(card_objects,num_player):
# Calculate and return win rates and tie rate
win_rates = [wins / 10000 for wins in player_wins]
return win_rates
def simulate_poker_game_with_community_card(card_objects, num_players, community_cards):
player_wins = [0] * num_players
players_hands = [card_objects[i*2:(i+1)*2] for i in range(num_players)]

for _ in range(10000):
# Create a full deck
deck = [Card(count, color) for color in range(1, 5) for count in range(1, 14)]
# Remove cards that are already in use (player hands + community cards)
flat_player_hands = [card for sublist in players_hands for card in sublist]
used_cards = flat_player_hands + community_cards
deck = [card for card in deck if card not in used_cards]

# Shuffle the remaining deck
random.shuffle(deck)

# The community cards are predefined, no need to draw them from the deck
player_hands_with_community = [hand + community_cards for hand in players_hands]
player_best_hands = [Ranker.rank_all_hands([hand], return_all=False) for hand in player_hands_with_community]

# Compare and record results
best_hand_score = max(player_best_hands)
winners = [score == best_hand_score for score in player_best_hands]
for i in range(num_players):
if winners[i]:
player_wins[i] += 1

# Calculate and return win rates
win_rates = [wins / 10000 for wins in player_wins]
return win_rates


def create_deck():
return [Card(count, color) for color in range(1, 5) for count in range(1, 14)]
Expand Down
21 changes: 21 additions & 0 deletions tableModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
runFlop= False
current_flop_cards=None
win_rates= None
winner_message=None



pygame.display.set_caption("Texas Hold em Odds Calculator")
Expand Down Expand Up @@ -96,6 +98,17 @@ def convert_strings_to_cards(card_strings):
elif run_flop_button_rect.collidepoint(event.pos):
random_cards = dropDownMenu.get_random_cards([card[1] for card in selected_cards], dropdown_menu.card_options)
current_flop_cards = [(dropdown_menu.card_images[card_name], card_name) for card_name in random_cards]
s_cards = [card_name for _, card_name in selected_cards] # Extract card names
cards = convert_strings_to_cards(s_cards)
# Constructing each player's hand combined with the community cards
player_hands = [cards[i * 2:(i + 1) * 2] for i in range(num_players)]
community_cards = [convert_to_card(card_name) for _, card_name in current_flop_cards]
hands_with_community = [hand + community_cards for hand in player_hands]
# Rank all hands and find the highest ranking hand
player_results = [card.Ranker.rank_all_hands([hand], return_all=False) for hand in hands_with_community]
highest_rank = max(player_results, key=lambda x: x[0]) # Find the highest rank
winner_index = player_results.index(highest_rank) # Get the index of the winning hand
winner_message = f"Player {winner_index + 1} is the winner with the highest rank!"
elif calculation_button_rect.collidepoint(event.pos):
s_cards = [card_name for _, card_name in selected_cards] # Extract card names
cards = convert_strings_to_cards(s_cards)
Expand All @@ -122,6 +135,14 @@ def convert_strings_to_cards(card_strings):
text_surface = font.render("Calculate", True, (255, 255, 255))
screen.blit(text_surface, (calculation_button_rect.x + 20, calculation_button_rect.y + 10))

if winner_message: # Check if there's a winner message to display
font = pygame.font.Font(None, 36)
text_surface = font.render(winner_message, True, (0, 0, 0)) # Black text
# Position the text at the top center of the screen
# Calculate the position so that the text is centered
text_rect = text_surface.get_rect(center=(screen.get_width()/2, 10))
screen.blit(text_surface, text_rect)


# Check if a card is selected
selected_card = dropdown_menu.get_selected_card()
Expand Down

0 comments on commit 502a969

Please sign in to comment.