我正在尝试编写一个脚本,将扑克牌列表转换为扑克牌对象,然后洗牌并运行测试手牌,这样您就可以根据扑克牌中的数量知道手中拿到扑克牌的可能性。(理想情况下,我希望让它一次模拟10,000只手)。
问题是我不知道如何将这些卡对象标记为一个或多个对象。例如,红心皇后有很多东西:它是一张脸卡,一张皇后牌,以及红心的花色。
现在,我正在获取一个卡片列表,以及每张卡片的拷贝数,并使用一个类将它们转换为卡片对象的列表。这最终返回一个包含卡片名称和类别的元组列表。然后,我使用一系列if-语句根据硬编码列表将卡片分类为特定类别,该列表列出了哪些卡片需要哪些标签。
问题是我不知道如何一次给他们一个以上的标签。
我也是python的新手,所以我不知道这是不是一个聪明的方法。
List = [("queen of hearts", 4), ("king of hearts", 4), ("joker", 3), ("2 of clubs", 4), ("4 of clubs", 2), ("2 of diamonds", 3)]
Type_hearts = {"queen of hearts", "king of hearts", "2 of hearts", "ace of heats"}
Type_face = {"queen of hearts", "queen of diamonds", "king of diamonds", "joker"}
Type_queen = {"queen of hearts", "queen of diamonds"}
Type_joker = {"joker"}
class Card(object):
def __init__(self, name):
self.name = name
#the following if-statement codes card objects into types
##in order for an object to fit into these categories, the full and complete name must match exactly
###also, right now it looks like these can currently only be one thing at a time :(
if name in Type_hearts:
Type = "heart"
if name in Type_face:
Type = "face card"
if name in Type_queen:
Type = "is queen"
else:
Type = None
self.card_type = Type
def __repr__(self):
return f"<{self.name}, {self.card_type}>"
#now we need to tell the computer how to REPRESENT this class as a string
#when you implement one of these, just implement both of these to have a textual representation of your class.
def __str__(self):
return f"<{self.name}, {self.card_type}>"
class DeckList(object):
def __init__(self, decklist):
self.decklist = decklist
#self.name = name
#self.count = count
self.deck = []
#decklist
for name, count in decklist:
for i in range(count):
self.deck.append(Card(name))我很想找出一种方法,让程序能够初始化一个对象来换取一张牌,比如说,红心之王,并能够知道它是一张国王,一张脸牌,以及一套红心花色。到目前为止,我只是不知道如何做到这一点,而且我不能一次用多个东西来标记事物。
发布于 2019-05-10 08:02:04
您可以指定一个列表,而不是将单个值赋给card_type,这样类就会变成如下所示:
def __init__(self, name):
self.name = name
#the following if-statement codes card objects into types
##in order for an object to fit into these categories, the full and complete name must match exactly
###also, right now it looks like these can currently only be one thing at a time :(
type_list = []
if name in Type_hearts:
type_list.append("heart")
if name in Type_face:
type_list.append("face card")
if name in Type_queen:
type_list.append("is queen")
self.card_type = type_list
def __repr__(self):
return f"<{self.name}, {self.card_type}>"
#now we need to tell the computer how to REPRESENT this class as a string
#when you implement one of these, just implement both of these to have a textual representation of your class.
def __str__(self):
return f"<{self.name}, {self.card_type}>"```发布于 2019-05-10 08:20:54
看起来你可能对字符串做了太多的事情。我们有一个Card类是很好的。我们目前正在以human readable格式向它传递卡片的名称,并处理该格式。如果我们重新组织我们的数据,使其更易于使用,会怎么样?
如果我们得到更多的值,而不是只取一个名字,会怎么样
class Card:
def __init__(self, name, suit, value):
self.name = name
self.suit = suit
self.value = value我们可以开始在类中添加一些辅助方法来提取您想要的数据
def is_face(self):
return self.value > 10 # Jack and up is a face card
def __str__(self):
return "{} of {}".format(self.name, self.suit)
def is_queen(self):
return self.name == "Queen"
def is_king(self):
return self.name == "King"等。
我们可以做几个
king_of_hearts = Card("King", "Hearts", 13)
queen_of_spades = Card("Queen" ,"Spades", 12)
ten_of_diamonds = Card("Ten", "Diamonds", 10)
print(king_of_hearts) # King of Hearts
print(queen_of_spades) # Queen of Spades
print(ten_of_diamonds) # Ten of Diamonds我们应该能够根据它的数据确定卡的type。我们不需要在构造时检查它应该有什么类型。
https://stackoverflow.com/questions/56068962
复制相似问题