diff --git a/poker.py b/poker.py index 0a41306..f188e1e 100644 --- a/poker.py +++ b/poker.py @@ -11,13 +11,16 @@ Deck = List[Card] player1: Hand player2: Hand deck: Deck +new_card: Card def create_deck() -> Deck: deck: Deck = [(rank, suit) for suit in SUITS for rank in RANKS] return deck -def deal_card(deck) -> Card: - return deck.pop() +def deal_card(deck: Deck) -> Card: + pull_card = pop_list(deck, 0) + deck.remove(pull_card) + return pull_card def deal_hand(deck, num=5) -> Hand: hand = random.sample(deck, num) @@ -25,6 +28,14 @@ def deal_hand(deck, num=5) -> Hand: deck.remove(card) return hand +def pop_list(lst: List, index: int) -> Tuple: + """Removes and returns the element at the specified index from the list.""" + if index < 0: + raise IndexError("Index out of range") + element = lst[index] + lst.remove(element) + return element + def hand_rank(hand): ranks = sorted([RANK_VALUES[card[0]] for card in hand], reverse=True) suits = [card[1] for card in hand] @@ -62,16 +73,12 @@ def replace_card_from_hand(hand, card, new_card) -> bool: 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 + user_input = input("Player, enter a card to pull from your hand (e.g., A of Spades) ") 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 + new_card = deal_card(Deck) # Example new card from the deck success = replace_card_from_hand(hand, card_to_replace, new_card) print(success) @@ -91,7 +98,7 @@ def display_initial_hands(player1: Hand, player2: Hand): print() def display_hands(player1: Hand, player2: Hand): - print("\nAfter pulling cards:") + print("\nPlayer's current hands:") print("Player 1's hand:\n", show_hand(player1)) print() print("Player 2's hand:\n", show_hand(player2)) @@ -105,13 +112,27 @@ def main(): display_initial_hands(player1, player2) print("Player 1's turn to pull a card.") p1_num = int(input("How many cards do you want to pull? (0-3): ")) - for _ in range(p1_num): - handle_player_input(player1) + if p1_num < 0 or p1_num > 3: + print("Invalid number of cards. Please choose between 0 and 3.") + return + elif p1_num != 0: + for _ in range(p1_num): + handle_player_input(player1) + else: + print("Player 1 chose not to pull any cards.") + display_hands(player1, player2) + print("Player 2's turn to pull a card.") p2_num = int(input("How many cards do you want to pull? (0-3): ")) - for _ in range(p2_num): - handle_player_input(player2) - display_hands(player1, player2) + if p2_num < 0 or p2_num > 3: + print("Invalid number of cards. Please choose between 0 and 3.") + return + elif p2_num != 0: + for _ in range(p2_num): + handle_player_input(player2) + else: + print("Player 2 chose not to pull any cards.") + display_hands(player1, player2) # Evaluate hands and determine winner rank1 = hand_rank(player1)