简体   繁体   English

使用Python类创建纸牌游戏

[英]Creating a card game with Python classes

I am trying to practice programming classes in Python by creating a card game. 我正在尝试通过创建一个纸牌游戏来练习Python中的编程类。 Right now what I want to achieve is to get a player to draw a card from the deck. 现在我想要实现的是让玩家从套牌中抽出一张牌。 I have the code as follows: 我的代码如下:

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

And the player class: 和播放器类:

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

In the draw_card() method from the Player class I've called the pickup method from the Deck class. 在Player类的draw_card()方法中,我从Deck类中调用了pickup方法。 Which I believe is the wrong thing to do but I'm not sure how else to pickup a card from the Deck object. 我认为这是错误的做法,但是我不确定如何从Deck对象中拾取卡。

Also, the draw_card method obviously doesn't work the way it's supposed to as it is creating a new deck every time and then picking up from the new deck (at least that's what I think it is doing right now). 此外, draw_card方法显然不能按照预期的方式工作,因为它每次都会创建一个新的牌组,然后从新牌组中获取(至少我认为它正在做的事情)。 This brings me back to my original question, how do I get a player to pickup a card from the same Deck such that I don't need to create a new Deck every time? 这让我回到原来的问题,如何让玩家从同一个Deck中取出一张卡片,这样我每次都不需要创建新的Deck?

Try something like 尝试类似的东西

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM