Projects/poker.py

125 lines
3.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
def create_deck():
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)
for card in hand:
deck.remove(card)
return hand
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, 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)
display_initial_hands(player1, player2)
handle_player_input(player1)
handle_player_input(player2)
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()