如何添加2名玩家并给他们卡? [蟒蛇]

时间:2015-10-16 23:44:25

标签: python

我是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名玩家提供相同数量的牌,我该怎么做?!

1 个答案:

答案 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())