利用装饰器写一个斗地主洗牌发牌的功能

Posted on 2018-06-13 18:04  魔幻白夜  阅读(201)  评论(0编辑  收藏  举报
from random import sample
import random
str_L=['2','3','4','5','6','7','8','9','10','J','Q','K','A']
color_L=['红桃','黑桃','方块','梅花']
l=['大王','小王']
for i in str_L:
    for j in color_L:
        l.append(j+i)
def washCards(func):
    def wrapper():
        random.shuffle(l)
        print("洗牌:"+str(l))
        func()
    return wrapper
@washCards
def catchCards():
    lA,lB,lC=[],[],[]
    for i in range(0,51,3):
        lA.append(l[i])
    print("玩家A:"+str(lA))
    for j in range(1,51,3):
        lB.append(l[j])
    print("玩家B:"+str(lB))
    for k in range(2,51,3):
        lC.append(l[k])
    print("玩家C:"+str(lC))
    for x in lA+lB+lC:
        l.remove(x)
    print("地主:"+str(l))
if __name__=="__main__":
    catchCards()
运行结果: