I have written a simple card game where upon initialising the game each player is given two cards.
At the bottom I have created a method called returnCards that puts the cards of a player back in the deck.
I am new to OOP in python and am curious if it's good convention to create a standalone method like this?
I feel as though this method should actually be a Player class method but I'm unsure how to write it as such. More than anything I'm trying to understand good practices when writing OOP code
import random
class Card:
def __init__(self, suit, val):
self.suit = suit
self.value = val
class Deck:
def __init__(self):
self.cards = []
self.build()
def build(self):
for i in ["Hearts", "Diamonds", "Spades", "Clubs"]:
for j in range(1,14):
self.cards.append(Card(i, j))
def length(self):
return len(self.cards)
class Game:
def __init__(self, players, deck):
self.players = players
self.deck = deck
self.cards = deck.cards
def deal(self):
for player in self.players:
for i in range(2):
player.cards.append(self.drawCard())
def drawCard(self):
drawnCard = self.deck.cards[0]
self.deck.cards = self.deck.cards[1:]
return drawnCard
class Player:
def __init__(self, name):
self.name = name
self.cards = []
def returnCards(player, game):
game.deck.cards.append(card for card in player.cards)
player.cards = []
deckOfCards = Deck()
playerOne = Player("John")
playerTwo = Player("Harrry")
newGame = Game([playerOne, playerTwo], deckOfCards)
newGame.deal()
returnCards()