我是OOP的新手,我在过去一小时内一直在写这篇文章。我只是不明白如何将我创建的牌组分配给2名玩家!
class Card:
def __init__(self, faceNum, suitNum):
self.faceNum = faceNum
self.suitNum = suitNum
def getCardName(self):
nameSuit = ['Ace','2','3','4','5','6','7','8','9','10','Jack','Queen','King']
nameFace = ['Coeur','Pique','Carreau','Trèfle']
return "%s of %s" % (nameFace[self.faceNum], nameSuit[self.suitNum])
def __str__(self):
carte_print1 = str(self.faceNum)
carte_print2 = str(self.suitNum)
return carte_print1 +('-')+ carte_print2
class Player:
def __init__(self,ID,Card):
self.PlayerID = ID
self.CardForPlayer = Card
import random
playerA = list()
playerB = list()
deck = list()
def deck():
deck = []
for suitNum in range(13):
for faceNum in range(4):
deck.append(Card(faceNum, suitNum))
return deck
deck = deck()
random.shuffle(deck)
for card in deck:
print(card.getCardName())
现在我只需要为2名玩家提供相同数量的牌,我该怎么做?!
答案 0 :(得分:1)
因此Player
类中应该有一个名称为deck
的属性。像这样修改你的Player
类。
class Player:
def __init__(self,ID,deck):
self.PlayerID = ID
self.deck = deck # this will hold a list of Card objects
现在,在您的主要代码中,在您撰写的内容的最后,添加以下行。
player1 = Player(1, deck())
player2 = Player(2, deck())