坚持创建一个纸牌游戏unicode直方图

时间:2014-02-04 21:32:23

标签: python unicode

我已经制作了一款纸牌游戏,可以将一张卡片改为一张卡片给4名玩家,这里是代码:

def shuffle_deck():
deck = []
for suit in ['H','C','D','S']:
    for denom in['A','2','3','3','4','5','6','7','8','9','10','J','Q','K']:
        deck += [denom + suit]
random.shuffle(deck)
return deck

def deal_hand(n,deck):
    hand = []
    for i in range(n):
        card = deck.pop()
        hand += [card]
    return hand

def deal_cards(n,cards,deck):
    player = []
    for i in range(n):
        player.append(deal_hand(cards,deck))
    return player

def deal_hands():
    deck = shuffle_deck()
    players = deal_cards(4, 13,deck)
    print(players)

但我对直方图感到困惑,想要在四个列表输出中寻找心脏和黑桃。就像这样:

>>>print(hands) 
       [['8H','KC','KS','AC','QD','7H','7D','4D','9D','3H','KD','6H','9S'], 
        ['2C','7C','TC','2S','QC','6D','JC','5H','2H','8D','3D','7S','4H'], 
        ['8S','8C','JD','6S','3C','5C','AD','5D','2D','4C','QH','4S','AH'], 
        ['AS','9C','JS','QS','TH','JH','TS','3S','6C','5S','KH','9H','TD']] 

>>>card_histogram(hands)! 
        ❤❤❤❤          #four hearts in the first list   

        ❤❤❤            #three hearts in the second list 

        ❤❤             #two hearts in the third list

        ❤❤❤❤♠         #four heart and one spades in the fourth list

如何根据我写的代码创建card_histogram函数?

1 个答案:

答案 0 :(得分:0)

我不确定您提供的输出对于您提供的输入是否正确,但是,您应该遍历每个子列表并计算每个套装的卡数,然后执行您应该进行的任何打印。

def card_histogram(hands):
    for hand in hands:
        numHearts, numSpades, numClubs, numDiamonds = 0, 0, 0, 0
        for card in hand:
            if card[1] == 'H': numHearts += 1
            elif card[1] == 'S': numSpades += 1
            elif card[1] == 'C': numClubs += 1
            else: numDiamonds += 1
        for x in range(numHearts): print_heart()
        for x in range(numSpades): print_spade()
        # etc

注意card[1]是对字符串进行子索引并取第1个(即第2个)字符,这就是你所知道的西装。打印是伪代码,因为我不知道你对打印的unicode输出的期望是什么,但是如果你试图打印那些unicode字符,你可以直接在你的文件中输入它们,因为在Python 3中默认的文件编码是utf8 (如果我错了,请有人纠正我。)