Search code examples
pythonclassexecution

Executing card and deck classes in python


class Card:
RANKS = ['Ace', '2', '3', '4', '5', '6', '7', '8', '9', 'Jack', 'Queen', 'King']

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

def get_name(self):
    return Card.RANKS[self.rank - 1] + ' of ' + self.suit

def equals(self, other_card):
    if self.rank == self.other_card:
        return True
    elif self.rank != self.other_card:
        return False 

def greater_than(self, other_card):
    if self.rank > self.other_card:
        return True
    elif self.rank < self.other_card:
        return False

def less_than(self, other_card):
    if self.rank < self.other_card:
        return True
    elif self.rank > self.other_card:
        return False 


class Deck:
def __init__(self):
    self.available_cards = []
    self.dealt_cards = []
    for suit in ['Hearts', 'Spades', 'Diamonds', 'Clubs']:
        for rank in range(1, 14):
            self.available_cards.append(Card(rank, suit))

def shuffle(self):
    """
    Shuffles the cards in the deck.
    """
    import random
    return random.shuffle(self.available_cards)

def deal_card(self):
    self.available_cards.add(Card)
    self.available_cards.remove(Card)
    self.dealt_cards.append(Card)

def get_size(self):
    return len[self.available_cards]

c = Card()
print(c.available_cards)

d = Deck()
d.shuffle()
print(d.dealt_cards)

So what I've done here is that I've made a Card class and a Deck class, but I'm having trouble executing it. I'm trying to test the two classes by shuffling it, and using it to deal some cards. But when I try to run it won't work. What should I do?


Solution

  • One issue is surely:

    c = Card()
    print(c.available_cards)
    

    Because 1) does the constructor of the Card class take 2 arguments (rank and suit and 2) does the Cards class not have available_cards, so trying to acces c.available_cards will not work

    So not knowing what you intended with those two lines I omitted them from further debugging.

    Now you are doing:

    d = Deck()
    

    okay, that doesn't need any argument and creates a new deck with all the cards in a standard 52 deck and initializes dealt_cards to [].

    Now you immediatly print

    print(d.dealt_cards)
    

    which will of course result in an empty list:

    print(d.dealt_cards)
    >>>[]
    

    What you might have intended is probably to deal some cards first and then print the result:

    for i in range(4):
        d.deal_card()
    print(d.dealt_cards)
    

    This however results in an error message since your deal_card is not correct. You don't want to add anything to the available_cards, but remove one item from it and add it to your dealt_cards like so:

    def deal_card(self):
        # self.available_cards.add(Card)
        # self.available_cards.remove(Card)
        self.dealt_cards.append(self.available_cards.pop())
    

    Now you will notice that print(d.dealt_cards) does not output the cards in the list in a "pretty" way, instead you might see something like this:

    [<__main__.Card object at 0x00000285E8C0A978>, <__main__.Card object at 0x00000285E8C0AA58>, <__main__.Card object at 0x00000285E8C0A4E0>, <__main__.Card object at 0x00000285E8BFCF28>]
    

    That is because you havn't taught your script how to print a card. For this we add a method to the Cards class:

    def __repr__(self):
        return self.get_name()
    

    whenever printing an instance of the cards class now, we will get the desired output.

    Final code:

    class Card:
        
    
        def __init__(self, rank, suit):
            self.suit = suit
            self.rank = rank
            self.ranks = ['Ace', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'Jack', 'Queen', 'King']
        def get_name(self):
            return Card.ranks[self.rank - 1] + ' of ' + self.suit
    
        def equals(self, other_card):
            if self.rank == other_card.rank:
                return True
            elif self.rank != other_card.rank:
                return False
    
        def greater_than(self, other_card):
            if self.rank > other_card.rank:
                return True
            elif self.rank < other_card.rank:
                return False
    
        def less_than(self, other_card):
            if self.rank < other_card.rank:
                return True
            elif self.rank > other_card.rank:
                return False
    
        def __repr__(self):
            return self.get_name()
    
    
    class Deck:
        def __init__(self):
            self.available_cards = []
            self.dealt_cards = []
            for suit in ['Hearts', 'Spades', 'Diamonds', 'Clubs']:
                for rank in range(1, 14):
                    self.available_cards.append(Card(rank, suit))
    
        def shuffle(self):
            """
            Shuffles the cards in the deck.
            """
            import random
            return random.shuffle(self.available_cards)
    
        def deal_card(self):
            # self.available_cards.add(Card)
            # self.available_cards.remove(Card)
            self.dealt_cards.append(self.available_cards.pop())
    
        def get_size(self):
            return len(self.available_cards)
    
    # c = Card()
    # print(c.available_cards)
    
    d = Deck()
    d.shuffle()
    for i in range(4):
        d.deal_card()
    print(d.dealt_cards)
    >>>[6 of Spades, Jack of Hearts, 4 of Spades, Queen of Spades]