I'm trying to develop a simple BlackJack game with Python.I'm at the stage where I want to deal a single card to the dealer's hand (a list in the dealer class) and the player's hand(a list in the player's class). When I deal to the dealer there is no problem however I have the players that are in a list called playerSet, I use a for loop to pop a card from the deck and append it to each player's hand. The problem is that each time I try to append to one player's hand the cards are duplicated to each player's hand.
This is some of the relevant code:
def DealCards():
global BlackJackDeck
Dealer1.addToHand(BlackJackDeck.drawCard())
#Would love to know why same card is ent to both decks
for i in range(0,len(playerSet)):
playerSet[i].addToHand(BlackJackDeck.drawCard())
break
This is the draw method located in the deck class
def drawCard(self):
#may need to change this method
return self.Deck.pop()
In terms of the player's class this is how the hand is declared as a list
class player(object):
name = None
Token = int(500)
score = 0
bet = 0
hand = []
def __init__(self,name):
self.name = name
This is the method used to add a card to a player's hand - A list in the player class
def addToHand(self,card):
if isinstance(card,Card):
self.hand.append(card)
else:
print("This is not a Card..")
The Blackjack deck is just an instance of the deck class which I will attach below
BlackJackdeck =Deck()
from Card import Card
import random
class Deck():
Deck=[]
def __init__(self):
self.Deck =[]
self.build()
def build(self):
for s in ["Hearts", "Spades", "Diamonds","Clubs"]:
for v in range(1,14):
if v>10:
for royalty in ["Jack","Queen", "King"]:
self.Deck.append(Card("{} of {}".format(royalty,s),s,10))
break
else:
self.Deck.append(Card("{} of {}".format(v,s),s,v))
def view(self):
for view in self.Deck:
#view.printer()
#print("{} of {} ".format(view.value, view.suit))
print("{}".format(view.name))
def drawCard(self):
#may need to change this method
return self.Deck.pop()
def shuffle(self):
random.shuffle(self.Deck)
def returnHand(self,playerHand=[]):
if len(self.Deck)>52:
print(f"Player hand couldn't be added there are too many cards in the deck {playerHand}")
elif len(self.Deck)+len(playerHand)<=52:
self.Deck.append(playerHand)
I also think that for for some reason all of the instantiated player objects either have the same hand or they are having the same card objects to their hand respectively and I can't figure out why.