用Python类创建纸牌游戏



我正在尝试通过创建卡游戏在Python中练习编程课程。现在,我想实现的是让玩家从甲板上画一张卡片。我的代码如下:

class Deck():
def __init__(self):
   #create the deck
   self.deck = []
   self.discard_pile = []
def create_deck(self):
   #assign the number of cards for each type to a card (dict)
   deck_stats = {"A":4, "B":6, "C":5, "D":5, "E":5, "F":5, "G":5, "H":5, "I":5, 'J':5}
   for card in deck_stats.keys():
     for i in range(0,deck_stats[card]):
       self.deck.append(card)
   return self.deck
def shuffle(self):
   #randomise the deck or for when the shuffle card is played
   random.shuffle(self.deck)
   return self.deck
def pickup(self):
   #picks up the first card on the draw pile
   picked_up = self.deck.pop(0)
   print(picked_up)
return picked_up

和玩家类:

class Player(Deck):
def __init__(self):
   self.player_hand = ["defuse"]
   for i in range(6):
     self.draw_card()
def draw_card(self):
#draw pile reduces by one
   deck = Deck()
   deck.create_deck()
   deck.shuffle()
   self.player_hand.append(deck.pickup())
return self.player_hand

在player类的draw_card()方法中,我已经从甲板类中调用了pickup方法。我认为这是错误的事情,但我不确定还如何从甲板对象拾取卡。

另外,draw_card方法显然无法按照它的方式工作,因为它每次都会创建一个新甲板,然后从新甲板上拾取(至少这就是我认为它现在正在做的事情)。这使我回到了我的原始问题,我如何让玩家从同一甲板上拾取卡片,以免每次创建新甲板?

尝试

之类的东西
class Deck():
    def __init__(self):
        # create the deck
        self.discard_pile = []
        self.deck = self.create_deck()
        self.shuffle()
    def create_deck(self):
        deck = []
        # assign the number of cards for each type to a card (dict)
        deck_stats = {"A": 4, "B": 6, "C": 5, "D": 5, "E": 5, "F": 5, "G": 5, "H": 5, "I": 5, 'J': 5}
        for card in deck_stats.keys():
            for i in range(0, deck_stats[card]):
                deck.append(card)
        return deck
    def shuffle(self):
        # randomise the deck or for when the shuffle card is played
        random.shuffle(self.deck)
        return self.deck
    def pickup(self):
        # picks up the first card on the draw pile
        picked_up = self.deck.pop(0)
        print(picked_up)
        return picked_up

class Player:
    def __init__(self):
        self.player_hand = ["defuse"]
        self.deck = Deck()
        for i in range(6):
            self.draw_card()
    def draw_card(self):
        # draw pile reduces by one
        self.player_hand.append(deck.pickup())
        return self.player_hand

最新更新