Updated deal_card function to eliminate ValueError exception
This commit is contained in:
parent
eb5927a887
commit
d86423b3c5
76
poker.py
76
poker.py
@ -14,12 +14,12 @@ deck: Deck
|
|||||||
new_card: Card
|
new_card: Card
|
||||||
|
|
||||||
def create_deck() -> Deck:
|
def create_deck() -> Deck:
|
||||||
deck: Deck = [(rank, suit) for suit in SUITS for rank in RANKS]
|
Deck = [(rank, suit) for suit in SUITS for rank in RANKS]
|
||||||
return deck
|
return Deck
|
||||||
|
|
||||||
def deal_card(deck: Deck) -> Card:
|
def deal_card(deck) -> Card:
|
||||||
pull_card = pop_list(deck, 0)
|
pull_card = deck.pop(0)
|
||||||
deck.remove(pull_card)
|
print("DEBUG - Dealt card:", pull_card)
|
||||||
return pull_card
|
return pull_card
|
||||||
|
|
||||||
def deal_hand(deck, num=5) -> Hand:
|
def deal_hand(deck, num=5) -> Hand:
|
||||||
@ -36,6 +36,13 @@ def pop_list(lst: List, index: int) -> Tuple:
|
|||||||
lst.remove(element)
|
lst.remove(element)
|
||||||
return element
|
return element
|
||||||
|
|
||||||
|
def card_in_hand(card: Card, hand: Hand) -> bool:
|
||||||
|
rank, suit = card
|
||||||
|
for r, s in hand:
|
||||||
|
if r.strip().upper() == rank.strip().upper() and s.strip().capitalize() == suit.strip().capitalize():
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
def hand_rank(hand):
|
def hand_rank(hand):
|
||||||
ranks = sorted([RANK_VALUES[card[0]] for card in hand], reverse=True)
|
ranks = sorted([RANK_VALUES[card[0]] for card in hand], reverse=True)
|
||||||
suits = [card[1] for card in hand]
|
suits = [card[1] for card in hand]
|
||||||
@ -65,30 +72,48 @@ def hand_rank(hand):
|
|||||||
def show_hand(hand):
|
def show_hand(hand):
|
||||||
return '\n '.join([f"{rank} of {suit}" for rank, suit in hand])
|
return '\n '.join([f"{rank} of {suit}" for rank, suit in hand])
|
||||||
|
|
||||||
def replace_card_from_hand(hand, card, new_card) -> bool:
|
def replace_card_from_hand(hand: Hand, card: Card, new_card: Card) -> bool:
|
||||||
if card in hand:
|
for i, existing_card in enumerate(hand):
|
||||||
hand.remove(card)
|
if existing_card[0].strip().upper() == card[0].strip().upper() and \
|
||||||
hand.append(new_card)
|
existing_card[1].strip().capitalize() == card[1].strip().capitalize():
|
||||||
return True
|
hand[i] = new_card
|
||||||
return False
|
return True
|
||||||
|
|
||||||
def handle_player_input(hand):
|
|
||||||
user_input = input("Player, enter a card to pull from your hand (e.g., A of Spades) ")
|
def handle_player_input(hand: Hand, deck: Deck) -> None:
|
||||||
|
user_input = input("Enter a card to pull from your hand (e.g., A of Spades): ").strip()
|
||||||
|
print("DEBUG - Current hand:", hand)
|
||||||
|
# Basic format check
|
||||||
|
|
||||||
|
if ' of ' not in user_input:
|
||||||
|
print("Invalid input format. Please use Rank of Suit.")
|
||||||
|
return
|
||||||
|
|
||||||
try:
|
try:
|
||||||
rank, suit = user_input.split(' of ')
|
rank, suit = user_input.split(' of ', 1)
|
||||||
card_to_replace = (rank.strip().upper(), suit.strip().capitalize())
|
rank = rank.strip().upper()
|
||||||
new_card = deal_card(Deck) # Example new card from the deck
|
suit = suit.strip().capitalize()
|
||||||
|
card = (rank, suit)
|
||||||
|
print("DEBUG - Attempting to remove:", card)
|
||||||
|
if not card_in_hand(card, hand):
|
||||||
|
print("DEBUG - Normalized input:", card)
|
||||||
|
print("DEBUG - Hand contents:", hand)
|
||||||
|
print(f"Card '{rank} of {suit}' not found in your hand.")
|
||||||
|
return
|
||||||
|
|
||||||
|
if not deck:
|
||||||
|
print("No more cards left in the deck to draw.")
|
||||||
|
return
|
||||||
|
|
||||||
|
new_card = deal_card(deck)
|
||||||
|
success = replace_card_from_hand(hand, card, new_card)
|
||||||
|
|
||||||
success = replace_card_from_hand(hand, card_to_replace, new_card)
|
|
||||||
print(success)
|
|
||||||
if success:
|
if success:
|
||||||
print(f"Player 1 pulled {rank} of {suit} from their hand.")
|
print(f"Replaced {rank} of {suit} with {new_card[0]} of {new_card[1]}.")
|
||||||
print(f"Player 1 received new card: {new_card[0]} of {new_card[1]}")
|
|
||||||
else:
|
else:
|
||||||
print("Card not found in Player 1's hand.")
|
print("Card replacement failed unexpectedly.")
|
||||||
except ValueError:
|
except Exception as e:
|
||||||
print("Invalid input format. Please use 'Rank of Suit'.")
|
print(f"Unexpected error: {e}")
|
||||||
|
|
||||||
def display_initial_hands(player1: Hand, player2: Hand):
|
def display_initial_hands(player1: Hand, player2: Hand):
|
||||||
print("\nPlayer's hands:")
|
print("\nPlayer's hands:")
|
||||||
@ -107,6 +132,7 @@ def display_hands(player1: Hand, player2: Hand):
|
|||||||
def main():
|
def main():
|
||||||
deck = create_deck()
|
deck = create_deck()
|
||||||
random.shuffle(deck)
|
random.shuffle(deck)
|
||||||
|
random.shuffle(deck)
|
||||||
player1 = deal_hand(deck)
|
player1 = deal_hand(deck)
|
||||||
player2 = deal_hand(deck)
|
player2 = deal_hand(deck)
|
||||||
display_initial_hands(player1, player2)
|
display_initial_hands(player1, player2)
|
||||||
@ -117,7 +143,7 @@ def main():
|
|||||||
return
|
return
|
||||||
elif p1_num != 0:
|
elif p1_num != 0:
|
||||||
for _ in range(p1_num):
|
for _ in range(p1_num):
|
||||||
handle_player_input(player1)
|
handle_player_input(player1, deck)
|
||||||
else:
|
else:
|
||||||
print("Player 1 chose not to pull any cards.")
|
print("Player 1 chose not to pull any cards.")
|
||||||
display_hands(player1, player2)
|
display_hands(player1, player2)
|
||||||
@ -129,7 +155,7 @@ def main():
|
|||||||
return
|
return
|
||||||
elif p2_num != 0:
|
elif p2_num != 0:
|
||||||
for _ in range(p2_num):
|
for _ in range(p2_num):
|
||||||
handle_player_input(player2)
|
handle_player_input(player2, deck)
|
||||||
else:
|
else:
|
||||||
print("Player 2 chose not to pull any cards.")
|
print("Player 2 chose not to pull any cards.")
|
||||||
display_hands(player1, player2)
|
display_hands(player1, player2)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user