Im trying to make a card class that I can use in other programs, the main goal of this is to make a deck of cards that would be valid, only 52 cards - no repeats, 4 suits - 13 cards each. My problem is I don't know how to work out the loops or any mechanism for testing what has already been drawn and then removing or only drawing different cards after that. Any help would be appreciated.

from random import *
from graphics import *

class DeckOfCards:
    def __init__(self):
        """Make a deck of cards with variables rank, suit and value"""
        self.rank = None
        self.suit = None
        self.value = None
        
    def getRank(self):
        return self.rank
    
    def getSuit(self):
        return self.suit

    def getValue(self):
        return self.value

    def RandomCard(self):
        """Test a card to see if its a valid draw"""
        self.PossCards=["1","2","3","4","5","6","7","8","9","10","j","q","k"]
        self.PossSuit = ["h","s","d","c"]

        rankCount=[0]*13
        cardCount,hearts,spades,diamonds,clubs=0,0,0,0,0
        
        while cardCount<53:
            cardCount+=1
            
            while self.PossCards:
                self.value = randrange(0,len(self.PossCards))+1
                self.rank = self.PossCards[self.value-1]

                rankCount[self.value-1]+=1
                for x in range(len(rankCount)):
                    print(rankCount)
                    if rankCount[x]>4:
                        rem=str(rankCount[x])
                        self.PossCards.remove(rem)
                
                    while self.PossSuit:
                        self.suit = self.PossSuit[randrange(0,len(self.PossSuit))]
                                             
                        if self.suit=="h":
                            hearts+=1
                            if hearts>12:
                                self.PossSuit.remove("h")
                                             
                        elif self.suit=="s":
                            spades+=1
                                  self.PossSuit.remove("s")
                                             
                        elif self.suit=="d":
                            diamonds+=1
                            if diamonds>12:
                                self.PossSuit.remove("d")
                        else:
                            clubs+=1
                            if clubs>12:
                                self.PossSuit.remove("c")

I would do something like:

import random
from graphics import *

class DeckOfCards(object):
    def __init__(self, *cards):
        """Make a deck of cards with variable suit and value"""
        if not cards:
            self.make_deck()
            random.shuffle(self.deck)
        else:
            self.deck = cards
        
    def deal(self, num=1):
        for count in range(num):
            yield self.deck.pop(0)
        
    def make_deck(self):
        """Prepare deck"""
        self.values=["1","2","3","4","5","6","7","8","9","10","j","q","k"]
        self.suits = ["h","s","d","c"]

        self.deck = [(value, suit)
                     for value in self.values
                     for suit in self.suits]
        
    def __repr__(self):
        return 'DeckOfCards(%s)' % (','.join(repr(card) for card in self.deck))

    def __len__(self):
        return len(self.deck)

    def __iter__(self):
        return iter(self.deck)

    def __getitem__(self, n):
        return self.deck[n]

mydeck = DeckOfCards()
print 'Your five cards are:\n ',tuple(mydeck.deal(5))
print 'There are %i cards in deck now:\n%s' % (len(mydeck), mydeck)

print 'In other way the cards are:'
for card in mydeck:
    print card

print 'Next card dealt will be:', mydeck[0]

Edited 4 Years Ago by pyTony

Here's Python 3.x code I wrote for cards and decks. Maybe it will give you some inspiration.

http://pastebin.com/wujeXQjV

In most implementations, suits and ranks are simply represented by numbers. I make my implementation only slightly more complex for extra expressiveness. (My suits have symbols and words, and my ranks have words.)

I do a lot of early computation in some __init__ methods, this is usually a bad idea if you create a lot of objects and don't need the data immediately. However, my card and deck objects get created only once. Each new deck is a copy.

On my machine I can create a deck, shuffle it, and draw 7 cards 500,000 times in under a minute. (I have a top-tier CPU though.)

Edited 5 Years Ago by lrh9: n/a

This article has been dead for over six months. Start a new discussion instead.