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 easier way to do this.
cards = ['1','1','1','1'....]
I'm positive you could make a for
loop to create 4 cards of the same value and add it to a list, but I was wondering if that was the best solution. I am not advanced enough to know about or create a Class
which I have seen to be offered as other solutions, but I am open to explanations.
I have already made a dictionary defining the card values.
Working in Photoshop or Illustrator is probably the best idea for making your cards. Illustrator is great because you need not worry about size issues too much, but Photoshop will work wonders as well. You choose.
A standard 52-card deck comprises 13 ranks in each of the four French suits: clubs (♣), diamonds (♦), hearts (♥) and spades (♠). Each suit includes three court cards (face cards), King, Queen and Jack, with reversible (double-headed) images. Each suit also includes ten numeral cards or pip cards, from one to ten.
A good quality deck of playing cards is almost certain to have embossing on the tuck box. This is where part of the lettering or image is raised against the background, to create a visual element of depth, simultaneously adding a tactile feel.
I propose you a solution with a basic class usage.
First, let's make a Card
class:
class Card:
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']
Another approach can be done using namedtuple
from collections
module, like this example:
from collections import namedtuple
Card = 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With