jeudi 9 mai 2019

How can I tag objects with one or more different types of tags that can be used to sort them later?

I am trying to write a script that takes a list of playing cards, turns them into card objects, and then shuffles and runs test hands so you know how likely it is to get one in your hand based on how many are in the deck. (ideally I would like to get it to simulate 10,000 hands at once).

The PROBLEM is that I don't know how to tag these card objects as one or more things. For instance, the queen of hearts is many things: It is a face card, a queen card, and in the suit of hearts.

Right now I am taking a list of cards and how many copies of each card are in the deck, and using a class to turn them into a list of card objects. This ultimately returns a list of tuples with the card's name and category. I am then using a series of if-statements to sort the cards into certain categories based on a hard-coded list of what cards need what tag.

The problem is that I don't know how to give them more than one tag at a time.

I am also new to python so I have no idea if this is a smart way to do this or not.

List = [("queen of hearts", 4), ("king of hearts", 4), ("joker", 3), ("2 of clubs", 4), ("4 of clubs", 2), ("2 of diamonds", 3)]

Type_hearts = {"queen of hearts", "king of hearts", "2 of hearts", "ace of heats"}
Type_face = {"queen of hearts", "queen of diamonds", "king of diamonds", "joker"}
Type_queen = {"queen of hearts", "queen of diamonds"}
Type_joker = {"joker"}

class Card(object):

    def __init__(self, name):
        self.name = name
        #the following if-statement codes card objects into types
        ##in order for an object to fit into these categories, the full and complete name must match exactly
        ###also, right now it looks like these can currently only be one thing at a time :(
        if name in Type_hearts:
            Type = "heart"
        if name in Type_face:
            Type = "face card"
        if name in Type_queen:
            Type = "is queen"
        else:
            Type = None
        self.card_type = Type


    def __repr__(self):
        return f"<{self.name}, {self.card_type}>"
    #now we need to tell the computer how to REPRESENT this class as a string
        #when you implement one of these, just implement both of these to have a textual representation of your class.

    def __str__(self):
        return f"<{self.name}, {self.card_type}>"

class DeckList(object):
    def __init__(self, decklist):
        self.decklist = decklist
        #self.name = name 

        #self.count = count 
        self.deck = []
        #decklist
        for name, count in decklist:
            for i in range(count):
                self.deck.append(Card(name))

I would love to figure out a way for the program to be able to initalize an object for a card, say, king of hearts, and be able to know that it is a king, a face card, and in the suit of hearts. So far I just don't know how to do that and I can't tag things with more than one thing at a time.

Aucun commentaire:

Enregistrer un commentaire