我有一个程序 ( ),它在其代码中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
代码
任何和所有输入表示赞赏。