利用Python裝飾器實現鬥地主之洗牌和抓牌,效果槓槓的!
阿新 • • 發佈:2018-12-11
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()