Hylke
Hylke

Reputation: 181

generating card deck Python 3

I am currently trying to program a (drinking) game called: Ride the bus. My problem right now doesn't necessarily lie in programming the game, but it is generating a deck of cards without any duplicates. I have looked all over this forum to find it, but the only things I found were how to generate random cards and not how to exclude duplicates. This is the code I have right now for generating cards:

    suitnum = ["spade","club","heart","diamond"]
    cardnum = [2,3,4,5,6,7,8,9,10,"jack","queen","king","ace"]

    suit1 = random.choice(suitnum)
    card1 = random.choice(cardnum)

Now, for each round in the game I generate a new card in the same way, except the suit and card name are different each round. I take the previously generated card into each round (each of which is a seperate def) because I need them. My issue right now is this code allows for duplicates to occur. I would like to find a way to basically generate a 'shuffled' deck of cards in the beginnning of the program and in each round be able to refer to that deck and take the top card of the deck. I think this would be the easiest way to deal with the problem. Does anyone have any ideas how to code this, or maybe any easier ways to solve my problem?

Upvotes: 1

Views: 6229

Answers (3)

Hylke
Hylke

Reputation: 181

Thanks for your help, I figured out a way which works for me. I created a def which looks like this:

    def carddraw():
        suits = ["spade","club","heart","diamond"]
        faces = [2,3,4,5,6,7,8,9,10,"jack","queen","king","ace"]

        deck = []
        for suit in suits:
            for face in faces:
                deck.append((suit, face))

In each def where I need to draw a card, I refer to the deck, ask for one card and split it up in to suit and face again (which I need for the game). That looks like this:

    card = random.choice(deck)
    deck.remove(card)
    suit1, face1 = card

Now every time I need to take another card in a different def I can just change the variable names of suit1 and face1 to suit2 and face2 etc.

Upvotes: 0

Aman Singh
Aman Singh

Reputation: 1104

I would recommend using your current suitnum and cardnum arrays to generate an actual card deck, with which you pick cards randomly from. This way, you can then delete cards as you pick them up from the array.

deck = []
for suit in suitnum:
    for card in cardnum:
        deck.append(suit + ' ' + str(card))

selection = random.choice(deck)
deck.remove(selection)
suit1 = selection.split()[0]
card1 = selection.split()[1]

Upvotes: 0

hyperneutrino
hyperneutrino

Reputation: 5425

Generate the list of cards and then take out a random element each time:

import random

suits = ["spade", "club", "heart", "diamond"]
faces = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"]

cards = []
for suit in suits:
    for face in faces:
        cards.append((suit, face))

Then, every time you need a new card:

random.shuffle(cards)
card = cards.pop() # or: suit, face = cards.pop()

Upvotes: 2

Related Questions