Skip to content Skip to sidebar Skip to footer

What Is The Best Way To Create A Deck Of Cards?

I was thinking about making a deck of cards for a card game. I could make a list of all of the cards (I don't really care about the suits), but I was wondering if there was a much

Solution 1:

I propose you a solution with a basic class usage.

First, let's make a Card class:

classCard:def__init__(self, value, color):
        self.value = value
        self.color = color

Then, let's make a list of colors:

colors = ['heart', 'diamonds', 'spades', 'clubs']

Finally, let's build your deck with a list comprehension:

deck = [Card(value, color) for value in range(1, 14) for color in colors]

The Card class is only a wrapper, just to manipulate cards instead of tuples, which feels more natural.

In this current state, it's almost equivalent to renaming the tuple type... Basically, it only consists in a constructor, __init__, that sets the attributes of the instance.

So when I call Card(value, color) in the list comprehension, so for example Card(11, 'spades'), a new instance of the Card class is created, which has its value attribute set to 11, and its color attribute set to 'spades'.

I recommend you read some tutorial about OOP for an in-depth understanding of the concepts.


Now, you can try and improve this idea, for instance by using a more detailed values list instead of the range(1, 14):

values = ['ace', '2', ..., 'king']

Solution 2:

Another approach can be done using namedtuple from collections module, like this example:

from collections importnamedtupleCard= namedtuple('Card', ['value', 'suit'])
suits = ['hearts', 'diamonds', 'spades', 'clubs']
cards = [Card(value, suit) for value in range(1, 14)for suit in suits]

And you can access to the values like this:

print(cards[0])
>>> Card(value=1, suit='hearts')
print(cards[0].value, cards[0].suit)
>>> 1 hearts

Solution 3:

You can represent your deck as a list of tuples. Which is a lighter weight alternative to classes. In dynamic languages like python, you will often do this to avoid the boilerplate code incurred by defining your own classes.

import itertools
import random

vals = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'jack', 'queen', 'king', 'ace']
suits = ['spades', 'clubs', 'hearts', 'diamonds']

deck = list(itertools.product(vals, suits))

random.shuffle(deck)

forval, suit in deck:
    print('The %s of %s' % (val, suit))

You may wish to represent the card values by an integer, this could easily be achieved by altering the input list.

Solution 4:

values = ['2','3','4','5','6','7','8','9','10','Jack','Queen','King','Ace']
suites = ['Hearts', 'Clubs', 'Diamonds', 'Spades']
deck = [[v + ' of ' + s,v] for s in suites for v in values]

Solution 5:

This solution uses enum class (package enum34).

Two enum classes represent the Suit and the Number with a custom str function. The Card class takes a Suit + a Number

from enum import Enum          
from enum import unique        

@uniqueclassSuit(Enum):              
    Spade = 1
    Heart = 2
    Dimond = 3 
    Club = 4def__str__(self):
        return self.name

@uniqueclassNumber(Enum):
    N1 = 1
    N2 = 2
    N3 = 3
    N4 = 4
    N5 = 5
    N6 = 6
    N7 = 7
    N8 = 8
    N9 = 9
    N10 = 10
    J = 11
    Q = 12
    K = 13def__str__(self):
        if self.value <= 10:
            returnstr(self.value)          
        return self.name

classCard(object):
    def__init__(self, suit, number):
        self.suit = suit
        self.number = number

    def__str__(self):
        return'{} {}'.format(self.suit, self.number)

cards = [ Card(suit, number) for suit in Suit for number in Number ]
for card in cards:
    print card

Post a Comment for "What Is The Best Way To Create A Deck Of Cards?"