计算在100张2张卡交易中发生的二十一点的数量

时间:2015-09-23 00:00:38

标签: python blackjack playing-cards

我需要计算Blackjack在5张牌套鞋的100张2张牌中发生的次数。到目前为止,我有这个:

str() - 转换为字符串

def str(draw):
    suits = {
        'S':'Spades',
        'H':'Hearts',
        'D':'Diamonds',
        'C':'Clubs'
        }
    cards = [
        'Ace', '2', '3', '4', '5',
        '6', '7', '8', '9', '10',
        'Jack', 'Queen', 'King'
        ]
    # create the human-readable string for the given card
    str = '{} of {}'.format(cards[draw[0]], suits[draw[1]])
    return str

convert() - 将一个整数(0-259;即5个甲板)转换为卡

#             Return list with card (0-12) and suit (S, H, D, C)
def convert(x):
    if x >= 0 and x <= 51:
        card = x%13
        suit = 'SHDC'[x/13]
        return card, suit, str([card,suit])
    elif x >= 52 and x <= 103:
        x = x - 52
        card = x%13
        suit = 'SHDC'[x/13]
        return card, suit, str([card,suit])
    elif x >= 104 and x <= 155:
        x = x - 104
        card = x%13
        suit = 'SHDC'[x/13]
        return card, suit, str([card,suit])
    elif x >= 156 and x <= 207:
        x = x - 156
        card = x%13
        suit = 'SHDC'[x/13]
        return card, suit, str([card,suit])
    elif x >= 208 and x <= 259:
        x = x - 208
        card = x%13
        suit = 'SHDC'[x/13]
        return card, suit, str([card,suit])

draw_n() - 从牌组中抽取n张牌而无需替换

def draw_n(n):
    from random import sample
    cards = []
    # Make sure a valid number is entered
     if n >0 and n <= 260:
        # Sample without replacement
        for x in sample(xrange(0,260),n):
            # Append converted card to list 'cards'
            cards.append(convert(x))
    return cards

得分1(手牌) - 计算2张牌中第一张牌的得分

def score1(hand):
    if hand[0][0] == 0:
        val1 = 11
        return val1
    elif hand[0][0] == 1:
        val1 = 2
        return val1
    elif hand[0][0] == 2:
        val1 = 3
    return val1
elif hand[0][0] == 3:
    val1 = 4
    return val1
elif hand[0][0] == 4:
    val1 = 5
    return val1
elif hand[0][0] == 5:
    val1 = 6
    return val1
elif hand[0][0] == 6:
    val1 = 7
    return val1
elif hand[0][0] == 7:
    val1 = 8
    return val1
elif hand[0][0] == 8:
    val1 = 9
    return val1
elif hand[0][0] == 9 or hand[0][0] == 10 or hand[0][0] == 11 or hand[0][0] == 12:
    val1 = 10
    return val1

得分2(手) - 计算2张牌中第二张牌的得分

def score2(hand):
    if hand[1][0] == 0:
        val2 = 11
        return val2
    elif hand[1][0] == 1:
        val2 = 2
        return val2
    elif hand[1][0] == 2:
        val2 = 3
        return val2
    elif hand[1][0] == 3:
        val2 = 4
        return val2
    elif hand[1][0] == 4:
        val2 = 5
        return val2
    elif hand[1][0] == 5:
        val2 = 6
        return val2
    elif hand[1][0] == 6:
        val2 = 7
        return val2
    elif hand[1][0] == 7:
        val2 = 8
        return val2
    elif hand[1][0] == 8:
        val2 = 9
        return val2
    elif hand[1][0] == 9 or hand[1][0] == 10 or hand[1][0] == 11 or hand[1][0] == 12:
        val2 = 10
        return val2

我希望能够画出10只手:

hands = [draw_n(2) for i in range(100)]

在此之后,计算二十一点发生次数的最有效方法是什么?

1 个答案:

答案 0 :(得分:0)

hand_total = [sum(x) for x in hands]
hand_total.count(21)