Yes
Yes

Reputation: 31

Trying coding a 5 card poker

import random, time
from random import shuffle

from enum import Enum

class CardSuits(Enum):
    Hearts = "Hearts"
    Diamonds = "Diamonds"
    Clubs = "Clubs"
    Spades = "Spades"

class CardValues(Enum):
    Two = 2
    Three = 3
    Four = 4
    Five = 5
    Six = 6
    Seven = 7
    Eight = 8
    Nine = 9
    Ten = 10
    Jack = "J"
    Queen = "Q"
    King = "K"
    Ace = "A"


class Card:

    _symbols = {"Hearts": "♥️", "Diamonds": "♦️", "Clubs": "♣️", "Spades": "♠"}

    def __init__(self, suit: CardSuits, value: CardValues) -> None:
        self.suit = suit
        self.value = value

    def __str__(self) -> str:
        return f"{self.value.value}{self._symbols[self.suit.name]}"

    def __repr__(self) -> str:
        return f"{self.value.value}{self._symbols[self.suit.name]}"


def start():
    play = input("You wanna play? ")
    if play.lower() in ("yes", "sure", "yeah"):
        all_cards = [Card(suits, value) for suits in CardSuits for value in CardValues]
        shuffle(all_cards)
        unique_cards = set()
        while len(unique_cards) < 5:
            unique_cards.add(random.choice(all_cards))
        cards = list(unique_cards)
        print("Your cards are:", ", ".join(str(card) for card in cards))
        time.sleep(4)
        while True:
            change_cards = input("Want to change cards? ")
            if change_cards.lower() == "yes":
                quantity = input("Which cards? ")
                
                first = cards[0]
                second = cards[1]
                third = cards[2]
                fourth = cards[3]
                fifth = cards[4]
                first, second, third, fourth, fifth = ("first", "second", "third", "fourth", "fifth")
                def print_new_cards():
                    print("Your new cards are:", ", ".join(str(card) for card in cards))
                
                if quantity.lower() == "none":
                    break

                elif quantity.lower() == first:
                    cards[0] = random.choice(all_cards)
                    print_new_cards()
                    break
                elif quantity.lower() == second:
                    cards[1] = random.choice(all_cards)
                    print_new_cards()
                    break
                elif quantity.lower() == third:
                    cards[2] = random.choice(all_cards)
                    print_new_cards()
                    break
                elif quantity.lower() == fourth:
                    cards[3] = random.choice(all_cards)
                    print_new_cards()
                    break
                elif quantity.lower() == fifth:
                    cards[4] = random.choice(all_cards)
                    print_new_cards()
                    break
                
                elif quantity.lower() == "all":
                    all_cards = [Card(suits, value) for suits in CardSuits for value in CardValues]
                    unique_cards = set()
                    while len(unique_cards) < 5:
                        unique_cards.add(random.choice(all_cards))
                    cards = list(unique_cards)
                    print("Your new cards are:", ", ".join(str(card) for card in cards))
                    break                
                else:
                    print("Invalid cards selected")
            elif change_cards.lower() == "no":    
                break     

        #CHECKING PHASE
            


        #SCORE DASHBOARD?
        
        
        while True:        
            continue_playing = input("You want to keep playing?")
            if continue_playing.lower() == "yes":
                wanna_shuffle = input("Wanna shuffle the deck?")
                if wanna_shuffle.lower() == "yes":
                    shuffle(all_cards)
                    print("Deck shuffled")
                    break
                elif continue_playing.lower() == "no":
                    break
                else:
                    continue
            elif continue_playing.lower() == "no":
                exit()    
        start()   
        
        
    else:
        exit()
    

start()

the question is: how can i write a few simple lines of code to change cards by the index without writing every change possibility as I was doing?

My inital idea was to change them by writing in the terminal which cards i would change.

ex. hand: 5♦️, A♦️, K♠, J♣️, 2♦️

n the terminal:

Want to change cards?

yes

Which cards?

K spades, J clubs

Your new cards are: 5♦️, A♦️, 6♥️, 7♥️, 2♦️

But i realized that for me is a little too difficult, so I changed my mind and decided to change cards by writing the index of them.

ex. hand: 5♦️, A♦️, K♠, J♣️, 2♦️

Which cards?

3, 4 or 3rd, 4rth or third, fourth

Your new cards are: 5♦️, A♦️, 9♠, Q♣️, 2♦️

Upvotes: 0

Views: 84

Answers (1)

treuss
treuss

Reputation: 2388

First (and somewhat unrelated): As you shuffle the cards, why don't you just deal the first five (and remove them from the deck), rather than giving random cards and ensuring they are unique:

deck = all_cards
shuffle(deck)
hand = deck[:5]
deck = deck[5:]

You should follow the same for changing cards, e.g:

while True:
    ans = input("Which cards you want to change (1-5, separated by comma): ")
    try:
        cardstochange = [int(i) for i in ans.split(",")]   # turn answer into list of integers (might raise an error)
        for cardidx in cardstochange:
            if cardidx < 1 or cardidx > 5:
                raise ValueError            # Raise an error 'cos the user can't count to 5
            hand[cardidx-1] = deck[0]       # -1 because the index start at 0
            deck = deck[1:]
        break;
    except ValueError: # Catch any bs input here
        print("I didn't get you. Say again!")

Upvotes: 2

Related Questions