0

I'm trying to create an interface with Python 3 on which to play a variety of card games. At the moment I've been making classes for cards, decks, players and "tables" and I'm having a small problem with the latter.

When writing my .deal() method to deal cards to the players at the table, I loop through players, draw the end card in the list of cards contained in the deck, removed it from the deck and assign it to the hand of the player in the loop. Seems simple enough? However, what actually happens is that all of the cards dealt go to all of the players, and I just can't see why? Could anyone help me? I'll put my code below.

for player in self.players:
    card = self.deck.cards[-1]
    self.deck.cards.remove(card)
    player.hand.append(card)
Omniswitcher
  • 368
  • 3
  • 13
  • 4
    Without a [mre] we can only guess at why, but evidently all of your players share a reference to a single list as "their hand". – jonrsharpe Sep 12 '22 at 12:36
  • Ok thanks for the info - would this be helpful? `deck=Deck(); deck.generate(); player1=Player("One"); player2=Player("Two"); player3=Player("Three"); player4=Player("Four"); players=[player1,player2,player3,player4]; table=Table(deck, players); table.deal();` produces all players having the same hand, despite each player.hand attribute being specific to the player. Apologies for formatting, still working out how to do this. – Linus Kelsey Sep 12 '22 at 12:45
  • The method itself has the for loop above as the main loop, the rest of it is simply a check to see if the deck needs to be shuffled first or not. – Linus Kelsey Sep 12 '22 at 12:47
  • No because Player is still undefined. Think about what someone else would actually need to recreate the issue. – jonrsharpe Sep 12 '22 at 12:49
  • Ahh ok. So the Player classes are classes I've made to model players in a game. Each player has `self.name` and `self.hand` attributes, and each should be individual but I don't know why they would be collective. – Linus Kelsey Sep 12 '22 at 12:52
  • Yes I understand that, but just a vague description doesn't actually help us solve the problem. Maybe you've made on of these common mistakes: https://stackoverflow.com/q/1680528/3001761, https://stackoverflow.com/q/1132941/3001761 – jonrsharpe Sep 12 '22 at 12:55
  • Okay - apologies for my comments, this is my first post on here! What other information _might_ be more useful for this sort of thing? Maybe if I were to add my `__init__` for Player? – Linus Kelsey Sep 12 '22 at 12:59
  • I worked it out! Thanks so much for the advice - the second link answered it well :) my Player class had the hand attribute set to a default of `[]`, and the second link had somebody having that same problem. Thanks for advice around posting too :) – Linus Kelsey Sep 12 '22 at 13:09
  • The same thing I (and the [help]) keep asking for would be useful: a MRE. An E that is both M and R. One you've actually tested before posting to ensure someone else could actually recreate your issue with it (i.e. without NameErrors, etc.). An orphaned method still isn't runnable. – jonrsharpe Sep 12 '22 at 13:12
  • Oh okay - I think I'm still getting my head around what R actually looks like. I'll check out some posts on here to see more examples. Thank you :) – Linus Kelsey Sep 12 '22 at 13:14

1 Answers1

0

First of all, as stated in comments we are lacking a reproductible example. I will still allow myself to suggest something for your code. you could merge those two lines :

card = self.deck.cards[-1]
self.deck.cards.remove(card)

into one like this :

card = self.deck.cards.pop()

In case you are not familiar with the pop function, when applied to list, it removes the last value when no argument is given, else removes the argument's position in the list (therefore must follow list access elements rules). And you can retrieve the removed elements into a variable or not.

For example, you could simply turn :

self.deck.cards.remove(card)

into

self.deck.cards.pop()

and it would be perfectly valid.

bylal
  • 161
  • 10