effa94
effa94

Reputation: 31

How to make a deck of cards with several variables in python?

I'm trying to make a abnormal deck of cards. Each card will have a color (red, green or blue), a degree (1, 2 or 3), a symbol (triangle, square or circle) and a number (1,2 or 3). I have a class that looks like this:

class card:
    def __init__(self, color, degree, symbol, number):
        self.color = color
        self.degree = degree
        self.symbol = symbol
        self.number = number
    
    def __repr__(self):
        return "(%s,%s,%s,%s)" %(self.color, self.degree, self.symbol, self.number)

I also have the following lists containg all the possible values for the fields and an empty deck where I want the cards to get into.

colors = ["red", "green", "blue"]
degrees = ["1", "2", "3"]
symbols = ["triangle", "square", "circle"]
numbers = ["1", "2", "3"]
deck = []

I want to create a full deck with every possible card. Preferably, they would be in a random order, but that's not necessary. I know that if it was only a number and color, I could easily do it this way:

deck = [card(value, color) for value in range(0, 2) for color in colors]

But I can't figure out how to do it with a symbol and a degree as well. I tried to build on more if statements to loop all the list, but that didn't work. I also don't want the same card to appear twice, and I dont want a card that doesn't follow the class rules: they must all be strutured as [color, degree, symbol, number].

Does anyone have a idea how to do this this?

Upvotes: 2

Views: 480

Answers (4)

vash_the_stampede
vash_the_stampede

Reputation: 4606

import itertools

identifiers = [colors, degrees, symbols, numbers]
deck = [[*i] for i in itertools.product(*identifiers)]
[['red', '1', 'triangle', '1'], ['red', '1', 'triangle', '2'], ['red', '1', 'triangle', '3'],...

Upvotes: -1

Mohit Solanki
Mohit Solanki

Reputation: 2130

use product from itertools

import itertools


deck = [
    card(color, degree, symbol, number)
    for color, degree, symbol, number in
    itertools.product(colors, degrees, symbols, numbers)
]

Upvotes: 1

Artyer
Artyer

Reputation: 40951

Do you want all combinations of colors, degrees, symbols and numbers?

If so, use nested for loops:

deck = []
for color in colors:
    for degree in degrees:
        for symbol in symbols:
            for number in numbers:
                deck.append(card(color, degree, symbol, number)

# Also written as a list comprehension
deck = [
    card(color, degree, symbol, number)
    for color in colors
        for degree in degrees
            for symbol in symbols
                for number in numbers
]  # The indent is just to show how it works. For style, put them all at the same indent.

Or use itertools.product (Which can also be lazy)

deck = itertools.starmap(card, itertools.product(colors, degrees, symbols, numbers))

deck = list(deck)  # If you really need it to be a list

Upvotes: 0

fabio.avigo
fabio.avigo

Reputation: 308

Full deck with every possible card combination:

deck = [card(color, degree, symbol, number) for color in colors \
        for degree in degrees for symbol in symbols for number in numbers]

For randomizing the card order in the deck, take a look at this: Shuffling a list of objects

Upvotes: 2

Related Questions