0

I'm learning Python and trying to write a simulated card game however I'm encountering a problem and I don't know what I'm doing wrong.

I have defined 3 classes: Card, Hand and Deck as below:

Card:

class Card(object):

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

    def __str__(self):
        return self.suit + self.pip

    def get_suit(self):
        return self.suit

    def get_pip(self):
        return self.pip

Hand:

class Hand(object):

    def __init__(self, cards = []):
        self.cards = cards

    def __str__(self):

        hand_cards = ''

        for card in self.cards:
            hand_cards += ' ' + card.__str__() +','

        return 'Your hand is:' + hand_cards

    def draw(self, card):
        self.cards.append(card)

Deck:

class Deck(object):

    def __init__(self):

        self.deck = []

        for suit in suits:
            for pip in pips:
                self.deck.append(Card(suit,pip))

        self.deck = self.deck*numdecks


    def shuffle(self):
        random.shuffle(self.deck)

    def deal(self):
        return self.deck.pop()

Then I have a list called player_hands which stores a list of Hand objects (numplayers is a variable from an earlier input):

player_hands = []

for x in range(numplayers):
    player_hands.append(Hand())

And finally I have a function called dealcard which I want to use to pass a Card object from the Deck to the Hand (where the Deck and Hand to use are passed as arguments):

def dealcard(deck, hand):
    hand.draw(deck.deal())

And this is where I have my problem. When I perform the below code, the function seems to append the same Card to every instance of the Hand class at once:

d = Deck()
dealcard(d,player_hands[0])

For example if there are 3 Hands and I then run this:

for h in player_hands:
    print h

I get:

Your hand is: D6,
Your hand is: D6,
Your hand is: D6,

Where D is the suit and 6 is the pip of the Card that I tried to append to player_hands[0] - which I thought would be the first Hand in the player_hands list? Why is it being appended to each instance in the list and how can I stop this from happening?

I've been looking at it and trying variations on this and different ways of printing to make sure it's not just because I'm printing it out incorrectly. I can't understand what's going wrong!

Sorry it's a bit of a long question, I'm just trying to give as much info as possible. It's probably something really simple, I just can't see it as I'm only just learning. Any help would be much appreciated!

1 Answers1

0

There is a problem in passing the list to constructor in Hand class. You should use cards = None instead of cards = [] as shown below.

class Hand(object):

    def __init__(self, cards = None):
        if cards is None:
            cards = []
        self.cards = cards

    def __str__(self):

        hand_cards = ''

        for card in self.cards:
            hand_cards += ' ' + card.__str__() +','

        return 'Your hand is:' + hand_cards

    def draw(self, card):
        self.cards.append(card)

You can read the following link for more information Passing a list to class python

tourism
  • 155
  • 12