python之实现斗地主

from random import shuffle, choice


def deal_cards():
    # 1)准备一副牌
    colors = ['♥', '♠', '♣', '♦']
    nums = [str(x) for x in range(2, 11)] + ['J', 'Q', 'K', 'A']
    pokers = []
    for n in nums:
        for c in colors:
            pokers.append(c+n)
    pokers.extend(('JOKER', 'joker'))
    # print(pokers)

    # 2)洗牌
    landlord = choice(pokers)
    pokers.remove(landlord)
    landlord = '地' + landlord
    pokers.append(landlord)

    shuffle(pokers)

    # print(pokers)

    # 3)发牌
    player1 = []
    player2 = []
    player3 = []
    d_pokers = iter(pokers)
    for _ in range(17):
        player1.append(next(d_pokers))
        player2.append(next(d_pokers))
        player3.append(next(d_pokers))

    # 4)理牌

    table = {str(x): x for x in range(3, 11)}
    table.update({'J': 11, 'Q': 12, 'K': 13, 'A': 14, '2': 15, 'oker': 16, 'OKER': 17})
    # print(table)
    player1.sort(key=lambda item: table[item[2:]] if item.startswith('地') else table[item[1:]], reverse=True)
    player2.sort(key=lambda item: table[item[2:]] if item.startswith('地') else table[item[1:]], reverse=True)
    player3.sort(key=lambda item: table[item[2:]] if item.startswith('地') else table[item[1:]], reverse=True)

    return player1, player2, player3, list(d_pokers)


p1, p2, p3, d = deal_cards()
print(p1)
print(p2)
print(p3)
print(d)

你可能感兴趣的:(python学习之路,python)