I am fairly new to intermediate programming, yet have played around with code for a while. At the moment I am making a simple card game. This issue is I am not sure where to use my functions or when to make them class methods.
For example, here is a simple function that deals out 5 cards to each player (from a predefined list) and then turns a card from the top of the pack (actually all just random selections). The cards are returned as a list of items (3 lists). I have also made a class called "Player".
p1_hand = []
p2_hand = []
flip_card = []
def deal_deck():
count = 0
for i in range(0, 10, 1):
count += 1
if count % 2 == 0:
card = random.choice(deck)
p2_hand.append(card)
deck.remove(card)
else:
card = random.choice(deck)
p1_hand.append(card)
deck.remove(card)
if count == 10:
flip_card.append(random.choice(deck))
return p2_hand, p1_hand, flip_card
In this example, it's just a deal, so I wonder why it would need to be a class method of "Player"? Possibly the class "Player" does not do much at all except keep score, keep track of who is dealing and what the score is?
To put it simply I am having trouble understanding the class as an object that interacts and preforms actions, I have made other classes and they have ended up working like a mini database rather than using much complexity in the methods at all.
Deckinstance does, possibly by telling aPlayer(or better,Hand) instance to draw a card from itself. If you have a weird kind of dealing, I'd put it inGame. Specifically here, if this is something that happens at the start of a game, I wold haveGame.__init__initialise aDeckinstance, then tell it to deal, then tell it to reveal the top card. I would not have dealing as aPlayerclass method: as you note, deaing is not really related to abstract "playerness", and especially not the turning over the top card.