0

I'm working on some homework, where we started OOP and I'm not really confident in it.

Let's say I have X instances of class Card, where Card is parent class for another subclasses like type of the cards etc, but overall I have all of them saved in variable "instances", so for example 20 instances. What I need is to get 10 of these instances into another Class named Deck, where I will create new list "deck" and have those 10 cards in it, where I will afterwords split them in a half to the 2 players.

My problem there is that I just don't know how to move these instances from class Card to the class Deck and also split them afterwords for these two players. Thanks for your help, I hope I made myself clear.

class Card: instances = [] def __init__(self, name): self.__name = name self.__class__.instances.append(self) class Deck: deck = [] def __init__(self): self.__class__.deck.append(Card.instances) class Player(Deck): def __init__(self, name, deck): super().__init__(deck) self.__name = name def main(): card1 = Card('Card1') card2 = Card('Card2') card3 = Card('Card3') p1 = Player('Tim') p2 = Player('Jill') if __name__ == '__main__': main() 
5
  • 1
    The way Deck is implemented here, it is a list of all cards, so you would never be able to have two different decks. Commented Dec 7, 2020 at 13:46
  • Okay, thanks. So how to possibly do that? Commented Dec 7, 2020 at 13:50
  • I'm not sure you even need a Deck class. Just a plain list of cards seems like it would work fine. Commented Dec 7, 2020 at 13:51
  • 1
    Also, why are you inheriting the Player class from the Deck class? This means that a player is a deck, which seems odd. I think it makes more sense to say that a player has a deck (i.e. composition), not that a player is a deck (i.e. inheritance). Commented Dec 7, 2020 at 13:59
  • Okay, but anyway. If I will have this list, how am I going to split it for these two players? Commented Dec 7, 2020 at 14:01

1 Answer 1

1

So first of all, you implemented Deck so that it contains a list of all Cards. You will want to init a deck just with its name (and maybe a few starting cards) and then have methods in this class to add and withdraw any other cards. You can use the code below for this:

class Deck: def __init__(self, init_cards=None): self.deck = [] if init_cards: self.deck.extend(init_cards) def add_card(self, card): self.deck.append(card) def withdraw_card(self, card_name): return_card = None for card in self.deck: if card.name == card_name: return_card = self.deck.remove(card) return return_card 

Next you can create the Decks with their cards and pass the cards between decks using the functions in the Deck class. Give each player a deck as a variable and then reference to it.

Sign up to request clarification or add additional context in comments.

1 Comment

__init__ probably wants to call .extend(init_cards), not .append(init_cards).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.