178 lines
5.8 KiB
Python
178 lines
5.8 KiB
Python
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
|
|
new_card: Card
|
|
|
|
def create_deck() -> Deck:
|
|
Deck = [(rank, suit) for suit in SUITS for rank in RANKS]
|
|
return Deck
|
|
|
|
def deal_card(deck) -> Card:
|
|
pull_card = deck.pop(0)
|
|
print("DEBUG - Dealt card:", pull_card)
|
|
return pull_card
|
|
|
|
def deal_hand(deck, num=5) -> Hand:
|
|
hand = random.sample(deck, num)
|
|
for card in 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 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):
|
|
ranks = sorted([RANK_VALUES[card[0]] for card in hand], reverse=True)
|
|
suits = [card[1] for card in hand]
|
|
rank_counts = {r: ranks.count(r) for r in ranks}
|
|
is_flush = len(set(suits)) == 1
|
|
is_straight = all(ranks[i] - 1 == ranks[i+1] for i in range(len(ranks)-1))
|
|
counts = sorted(rank_counts.values(), reverse=True)
|
|
if is_straight and is_flush:
|
|
return (8, ranks) # Straight flush
|
|
elif counts[0] == 4:
|
|
return (7, ranks) # Four of a kind
|
|
elif counts[0] == 3 and counts[1] == 2:
|
|
return (6, ranks) # Full house
|
|
elif is_flush:
|
|
return (5, ranks) # Flush
|
|
elif is_straight:
|
|
return (4, ranks) # Straight
|
|
elif counts[0] == 3:
|
|
return (3, ranks) # Three of a kind
|
|
elif counts[0] == 2 and counts[1] == 2:
|
|
return (2, ranks) # Two pair
|
|
elif counts[0] == 2:
|
|
return (1, ranks) # One pair
|
|
else:
|
|
return (0, ranks) # High card
|
|
|
|
def show_hand(hand):
|
|
return '\n '.join([f"{rank} of {suit}" for rank, suit in hand])
|
|
|
|
def replace_card_from_hand(hand: Hand, card: Card, new_card: Card) -> bool:
|
|
for i, existing_card in enumerate(hand):
|
|
if existing_card[0].strip().upper() == card[0].strip().upper() and \
|
|
existing_card[1].strip().capitalize() == card[1].strip().capitalize():
|
|
hand[i] = new_card
|
|
return True
|
|
|
|
|
|
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:
|
|
rank, suit = user_input.split(' of ', 1)
|
|
rank = rank.strip().upper()
|
|
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)
|
|
|
|
if success:
|
|
print(f"Replaced {rank} of {suit} with {new_card[0]} of {new_card[1]}.")
|
|
else:
|
|
print("Card replacement failed unexpectedly.")
|
|
except Exception as e:
|
|
print(f"Unexpected error: {e}")
|
|
|
|
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: Hand, player2: Hand):
|
|
print("\nPlayer's current hands:")
|
|
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)
|
|
random.shuffle(deck)
|
|
player1 = deal_hand(deck)
|
|
player2 = deal_hand(deck)
|
|
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): "))
|
|
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, deck)
|
|
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): "))
|
|
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, deck)
|
|
else:
|
|
print("Player 2 chose not to pull any cards.")
|
|
display_hands(player1, player2)
|
|
|
|
# Evaluate hands and determine winner
|
|
rank1 = hand_rank(player1)
|
|
rank2 = hand_rank(player2)
|
|
if rank1 > rank2:
|
|
print("Player 1 wins! with rank:", rank1)
|
|
print("Player 2 loses with rank:", rank2)
|
|
elif rank2 > rank1:
|
|
print("Player 2 wins! with rank:", rank2)
|
|
print("Player 1 loses with rank:", rank1)
|
|
else:
|
|
print("It's a tie!")
|
|
print("Both players have rank:", rank1)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main() |