Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to create a deck of cards?

Tags:

python

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.

like image 848
Mushy0364 Avatar asked Feb 01 '17 02:02

Mushy0364


People also ask

Can you design deck of cards?

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.

What is the layout of a deck of cards?

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.

What makes a good card deck?

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.


2 Answers

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']
like image 61
Right leg Avatar answered Oct 03 '22 01:10

Right leg


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
like image 26
Chiheb Nexus Avatar answered Oct 03 '22 01:10

Chiheb Nexus