Search code examples
pythonpoker

Making a simple poker game


I'm making a simple poker game to print the probability of getting: 1 pair, then 2 pair, then 3 of a kind and 4 of a kind.

What I want to do is make it where we have the user who gets a hand of 5 cards.

Then have it where you traverse through the list to check if each card matches with any of the other cards and to check if it's one pair, two pair, three of a kind, then four of a kind.

Then lastly checking the probability of getting those hands.

I'm just trying to get this started, I'm not sure what method to use to check if any two elements are equal then if there are two pairs then 3 and 4 of a kind.

For each, if-statement I know to use a break and return false so it'll end the while loop.

I am using 1-13 instead of using a dictionary to check for suits.

So far I have just printed the random set for the cards.

def poker():
    count = 0
    cards = []
    while(True):
        for i in range(0,5):
            cards = random.choice([1,2,3,4,5,6,7,8,9,10,11,12,13])
            if(cards[0] == cards[1,2,3,4]):
                 count+=1

Solution

  • Here's a quick modification to your current code:

    def poker():
        cards = []
        for i in range(5): #This is the same as range(0,5)
            # Use 'append' instead of '=' to add the new card to list instead of overwriting it
            cards.append(random.choice([1,2,3,4,5,6,7,8,9,10,11,12,13])) 
        # set() will give you a set of unique numbers in your hand
        # If you have a pair, there will only be 4 cards in your set!
        for card in set(cards):
            number = cards.count(card) # Returns how many of this card is in your hand
            print(f"{number} x {card}")
    
    

    Output:

    "1 x 2"
    "1 x 6"
    "2 x 8" # <-- Pair!
    "1 x 10"
    

    This should get you on the right track! Now that you can tell how many of each card you have in your hand, you can use that to track each player's hand!

    If you have any further, specific questions relating to a piece of code, feel free to ask another question, after brushing up on the question guidelines.