Updated poker.py to fix card replacement and display hands correctly.

This commit is contained in:
Donald Calloway 2025-09-16 09:16:50 -07:00
parent 4fb57a3ff5
commit 02d551de2a

View File

@ -1,11 +1,23 @@
import random
from typing import List, Tuple
SUITS = ['Hearts', 'Diamonds', 'Clubs', 'Spades']
RANKS = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']
RANK_VALUES = {rank: i for i, rank in enumerate(RANKS, 2)}
Card = Tuple[str, str] # ('Rank', 'Suit')
Hand = List[Card]
Deck = List[Card]
player1: Hand
player2: Hand
deck: Deck
def create_deck():
return [(rank, suit) for suit in SUITS for rank in RANKS]
deck: Deck = [(rank, suit) for suit in SUITS for rank in RANKS]
return deck
def deal_card(deck):
return deck.pop()
def deal_hand(deck, num=5):
hand = random.sample(deck, num)
@ -42,48 +54,60 @@ def hand_rank(hand):
def show_hand(hand):
return '\n '.join([f"{rank} of {suit}" for rank, suit in hand])
def remove_card_from_hand(hand, card):
def replace_card_from_hand(hand, card, new_card) -> bool:
if card in hand:
hand.remove(card)
hand.append(new_card)
return True
return False
def handle_player_input(hand):
user_input = input("Player, enter a card to pull from your hand (e.g., 'A of Spades', or 'none'): ").strip()
if 'none' in user_input.lower():
print("No card pulled.")
return
try:
rank, suit = user_input.split(' of ')
card_to_replace = (rank.strip().upper(), suit.strip().capitalize())
new_card = deal_card(deck) # Example new card from the deck
success = replace_card_from_hand(hand, card_to_replace, new_card)
print(success)
if success:
print(f"Player 1 pulled {rank} of {suit} from their hand.")
print(f"Player 1 received new card: {new_card[0]} of {new_card[1]}")
else:
print("Card not found in Player 1's hand.")
except ValueError:
print("Invalid input format. Please use 'Rank of Suit'.")
def display_initial_hands(player1: Hand, player2: Hand):
print("\nPlayer's hands:")
print("Player 1\n", show_hand(player1))
print()
print("Player 2\n", show_hand(player2))
print()
def display_hands(player1, player2):
print("\nAfter pulling cards:")
print("Player 1's hand:\n", show_hand(player1))
print()
print("Player 2's hand:\n", show_hand(player2))
print()
def main():
deck = create_deck()
random.shuffle(deck)
player1 = deal_hand(deck)
player2 = deal_hand(deck)
print("Player 1's hand:\n", show_hand(player1))
print()
print("Player 2's hand:\n", show_hand(player2))
pull_card_player1 = input("Player 1, enter a card to pull from your hand (e.g., 'A of Spades', or 'none'): ")
if 'none' not in pull_card_player1.lower():
rank, suit = pull_card_player1.split(' of ')
if remove_card_from_hand(player1, (rank, suit)):
player1.remove((rank, suit))
print("Player 1 pulled", rank, "of", suit, "from his hand.")
else:
print("Card not found in Player 1's hand.")
pull_card_player2 = input("Player 2, enter a card to pull from your hand (e.g., 'A of Spades', or 'none'): ")
if 'none' not in pull_card_player2.lower():
rank, suit = pull_card_player2.split(' of ')
if remove_card_from_hand(player2, (rank, suit)):
player2.remove((rank, suit))
print("Player 2 pulled", rank, "of", suit, "from his hand.")
else:
print("Card not found in Player 2's hand.")
print("\nAfter pulling cards:")
print("Player 1's hand:\n", show_hand(player1))
print()
print("Player 2's hand:\n", show_hand(player2))
print()
display_initial_hands(player1, player2)
handle_player_input(player1)
handle_player_input(player2)
display_hands(player1, player2)
# Evaluate hands and determine winner
# Evaluate hands and determine winner
rank1 = hand_rank(player1)
rank2 = hand_rank(player2)
if rank1 > rank2: