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

I was thinking of creating a deck of cards for a card game. I could make a list of all the cards (I don't like the costumes anyway), but I was wondering if there was a much simpler way to do this.

cards = ['1','1','1','1'....]

I'm sure you could do a loop forto create 4 cards with the same value and add them to the list, but I was wondering if this was the best solution. I am not advanced enough to learn or create Class, which, as I have seen, is proposed as other solutions, but I am open to explanations.

I already made a dictionary that defines the values ​​of the map.

+5
source share
9 answers

I offer you a solution using the base class.

Card:

class Card:
    def __init__(self, value, color):
        self.value = value
        self.color = color

:

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

, :

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

Card - , , .

tuple... __init__, .

, Card(value, color) , Card(11, 'spades'), Card, value, 11, color, 'spades'.

.


, , values range(1, 14):

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

namedtuple collections, :

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]

, :

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

:

import random
from random import shuffle

def RANKS(): return [ "Ace", "2", "3", "4", "5", "6", "7","8", "9", "10", "Jack", "Queen", "King" ]
def SUITS(): return [ "Clubs", "Diamonds", "Hearts", "Spades" ]

:

class Card:

    def __init__( self, rank, suit ):
        self.rank = rank
        self.suit = suit

    def __str__( self ):
        return self.rank + " of " + self.suit

Deck:

class Deck:

    def __init__( self ):
        self.contents = []
        self.contents = [ Card( rank, suit ) for rank in RANKS() for suit in SUITS() ]
        random.shuffle( self.contents )
+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]
+1

. . , python, , , .

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)

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

, , .

+1

, , for loop , . Python, , psuedo , .

0

:

card_deck = []

for i in range(3,11):
    card_deck.extend([i]*4)

for c in ['Jack', 'Queen', 'King', 'Ace']:
    card_deck.extend([c]*4)
0

enum ( enum34).

str. Card +

from enum import Enum          
from enum import unique        

@unique
class Suit(Enum):              
    Spade = 1
    Heart = 2
    Dimond = 3 
    Club = 4

    def __str__(self):
        return self.name

@unique
class Number(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 = 13 

    def __str__(self):
        if self.value <= 10:
            return str(self.value)          
        return self.name

class Card(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
0

40 . 40 , [-1] - c b s d (, , ). 10- 2 , [: -1], 1,2,3... 9 10, 2 .

class Cards:
    def __init__(self):
        self.deck = []                  # the deck is empty
        for n in range(1,11):           # numbers
            for c in "cbsd":            # colors
                self.deck.append(str(n) + c)  # a string for each card


deck = Cards()
deck.deck

output:

['1c', '1b', '1s', '1d', '2c', '2b', '2s', '2d', '3c', '3b', '3s', '3d', '4c', '4b', '4s', '4d', '5c', '5b', '5s', '5d', '6c', '6b', '6s', '6d', '7c', '7b', '7s', '7d', '8c', '8b', '8s', '8d', '9c', '9b', '9s', '9d', '10c', '10b', '10s', '10d']

0

Source: https://habr.com/ru/post/1668473/


All Articles