-5

我正在做我的心脏游戏编程,但我对如何计算每张卡片中的所有分数感到困惑 这是有价值的卡片,其他卡片有 0 价值

>>> card_value ={"QS":13,"2H":1,"3H":1,"4H":1,"5H":1,"6H":1,"7H":1,"8H":1,"9H":1,"0H":1,"JH":1,"QH":1,"KH":1,"AH":1,"0D": -10}

但是如果一组列表中的所有子列表都包含一颗心,那么分数将为-26,如果一组列表中的所有子列表都包含一颗心和 10 颗钻石(“0D”),那么分数将为-36

例子

>>> list1 = [['QD', '7D', '5H', '0D'], ['7C', '6D', '2H', 'KS'], ['8S', '3D', '9C', 'KD'], ['4D', '9D', 'KH', '4C'],['7S', 'JH', '4H', 'QC'], ['2S', 'KC', 'AD', 'AC'], ['JD', '8D', 'QS', '6H'], ['AH', '9H', '9S', '6S'],['5S', '0S', 'JC', '2D'],['5D', '8H', '0C', '7H'], ['JS', '6C', '2C', 'AS'], ['3S', '4S', '5C', '0H'], ['3C', 'QH', '3H', '8C']]

输出将是 -36

>>> list2 = ["5C","QS","JH"]

输出将是 14

这是我到目前为止所尝试的:

def score(list,score=0):
    for cards in list:
        if cards in card_value.keys():
            score += card_value[cards]    
        if cards == "H":
            score = -36
        if cards == "H" or "0D":
            score = -26
   return score
4

1 回答 1

2

total在两种情况下都设置为零然后执行类似的操作

for i in list2: total=total+card_value.get(i,0)

for l in list1:
   for i in l:
    total=total+card_value.get(i,0)

使用这种技术从 list1 返回的总数是 16 而不是 -36 但我认为你必须明白

编辑:现在有了这个“如果列表中的所有卡片都有所有的心,那么分数将为-26”业务。我从您下面的评论中假设这实际上意味着“如果一组列表中的所有子列表都包含至少一个心脏,那么分数将为 -26”

for l in list1:
   for i in l:
    total=total+card_value.get(i,0)
    if i[0] == 'H':   # first character of card string is a H- so it's a heart
      heart_counter = heart_counter + 1
if heart_counter == len(list1):
   total = total - 26
于 2013-05-24T16:03:44.537 回答