0

我有一个程序 ( ),它在其代码中blackjack.py访问另一个程序的 (cards.py和)。games.py其中大部分来自一本书,所以我无法理解它是如何工作的。

继承人的代码cards.py

class Card(object):
    """ A playing card. """
    RANKS = ["A", "2", "3", "4", "5", "6", "7",
             "8", "9", "10", "J", "Q", "K"]
    SUITS = ["c", "d", "h", "s"]
    def __init__(self, rank, suit, face_up = True):
        self.rank = rank
        self.suit = suit
        self.is_face_up = face_up

    def __str__(self):
        if self.is_face_up:
            rep = self.rank + self.suit
        else:
            rep = "XX"
        return rep

    def flip(self):
        self.is_face_up = not self.is_face_up


class Hand(object):
    """ A Hand of playing cards. """
    def __init__(self):
        self.cards = []

    def __str__(self):
        if self.cards:
            rep = ""
            for card in self.cards:
                rep += str(card) + "\t"
        else:
            rep = "<empty>"
        return rep

    def clear(self):
        self.cards = []

    def add(self, card):
        self.cards.append(card)

    def give(self, card, other_hand):
        self.cards.remove(card)
        other_hand.add(card)


class Deck(Hand):
    """ A deck of playing cards. """
    def populate(self):
        for suit in Card.SUITS:
            for rank in Card.RANKS:
                self.add(Card(rank, suit))

    def shuffle(self):
        import random
        random.shuffle(self.cards)

    def deal(self, hands, per_hand = 1):
        for round in range(per_hand):
            for hand in hands:
                if self.cards:
                    top_card = self.cards[0]
                    self.give(top_card, hand)
                else:
                    print "Can't continue deal. Out of cards!"

if __name__ == "__main__":
    print "This is a module with classes for playing cards."
    raw_input("\n\nPress the enter key to exit.")

我正在为 写一个错误检查,blackjack.py我需要收集到目前为止已使用的卡的数量。我想我可以通过访问cards[]. 问题是,我不确定如何做到这一点。

不过,这都是理论上的。我也会包含 `blackjack.py' 代码,所以你们都可以看到我正在尝试做什么,并帮助我确定我的逻辑是否有缺陷。

blackjack.py 代码

任何和所有输入表示赞赏。

4

1 回答 1

1

虽然我并不完全清楚您在这里的预期结构,但您有几个选择。

首先,为了使用模块中的任何函数或类,cards.pyblackjack.py可以使用import语句导入它们。这样做有两种风格:

# blackjack.py
import cards

可以让您访问cards模块中的所有内容,并且您可以通过在每个函数/类前面加上cards.<name>. 所以,如果你想初始化一个Deck类的实例,

# blackjack.py
import cards
mydeck = cards.Deck()

另一种方式是from <X> import <Y>,它使您无需添加前缀即可访问函数和类。例子:

# blackjack.py
from cards import Deck  # or, to import everything, "from cards import *"
mydeck = Deck()

这些方法中的任何一个都会为您提供该类的实例cards.Deck

选项 0

您已经可以在您的Deck班级中说明这一点,因为它是 的子类Hand,因此每次您使用卡片时,它都会从的属性give中取出。因此,发出的卡片数量将是:Deckcards

class Deck(Hand):
    # ...
    def number_cards_used(self):
         return 52 - len(self.cards)

或者,如果您无法编辑cards.py,您可以简单地通过以下方式获取您给定的卡片数量Deck

# blackjack.py
def get_number_cards_used_from_deck(deck):
    return 52 - len(deck.cards)

正在使用:

# blackjack.py
import cards
mydeck = cards.Deck()
# ...
# Do other operations with deck
# ...
cards_used = get_number_cards_used_from_deck(mydeck)

选项1

如果您可以隔离所有同时玩的手,您可以实现一个方法card_count

class Hand(object):
   # ...
   # other code
   # ....

   def card_count(self):
       return len(self.cards)

然后,例如,如果您有所有手牌的列表,您可以执行以下操作:

sum(map(lambda h: h.card_count(), list_of_hands))

选项 2

在您的Deck类中,由于它是子类Hand,因此您可以简单地保留另一个已分发的卡片的运行列表,这些卡片通常会被刷新。这看起来像:

class Deck(Hand):
    # ...
    # other code
    # ...

    def __init__(self):
        self.populate()
        self.used_cards = []

    def give(self, card, other_hand):
        self.used_cards.append(card)
        super(Deck, self).give(card, other_hand)

    def number_cards_used(self):
        return len(self.used_cards)

当然,还有其他方法。

于 2012-11-25T06:03:00.100 回答