#-*- coding:cp936 -*-
import random
import threading
mylock=threading.RLock()
ren=[]
name=['lidandan','zhangqi','fanfan']
pai={1:'黑桃3',2:'红杏3',3:'梅花3',4:'方块3',5:'黑桃4',6:'红杏4',7:'梅花4'
,8:'方块4',9:'黑桃5',10:'红杏5',11:'梅花5',12:'方块5',13:'黑桃6',14:'红杏6'
,15:'梅花6',16:'方块6',17:'黑桃7',18:'红杏7',19:'梅花7',20:'方块7',21:'黑桃8'
,22:'红杏8',23:'梅花8',24:'方块8',25:'黑桃9',26:'红杏9',27:'梅花9',28:'方块9'
,29:'黑桃10',30:'红杏10',31:'梅花10',32:'方块10',33:'黑桃J',34:'红杏J',35:'梅花J'
,36:'方块J',37:'黑桃Q',38:'红杏Q',39:'梅花Q',40:'方块Q',41:'黑桃K',42:'红杏K',
43:'梅花K',44:'方块K',45:'黑桃A',46:'红杏A',47:'梅花A',48:'方块A',49:'黑桃2',
50:'红杏2',51:'梅花2',52:'方块2',53:'小王',54:'大王'}
num=53
class puke(threading.Thread):
def __init__(self,name):
threading.Thread.__init__(self)
self.name=name
self.dict={}
def run(self): #发牌
global lidandan,zhangqi,fanfan,num
while len(self.dict)<17: #每人同时发17张
mylock.acquire() #上锁
rd=random.randint(0,num) #这个不是很随机
keys=list(pai.keys()) #列表化,便于处理
random.shuffle(keys) #打乱key的顺序,尽量随机化
self.dict[keys[rd]]=pai[keys[rd]] #将pai的第rd个(key,value)插入到类自己的字典
del pai[keys[rd]]
num=num-1
mylock.release() #解锁
def dizhuadd(ren): #抢地主
global pai
print("开始抢地主!")
dizhu=random.randint(0,2)
for j in range(3):
banbie=0
banbie=int(input(ren[dizhu].name+"你要地主吗?要请输入1,不要请输入0\n"))
if(banbie==1):
for key in pai:
ren[dizhu].dict[key]=pai[key]
del pai
print(ren[dizhu].name+'抢到地主!\n\n')
return
dizhu=(dizhu+1)%3
print("没人叫地主,重新开始!\n")
def printpai(ren):
for i in ren:
print (i.name+':')
for key in sorted(i.dict.keys()): #扑克牌排序
print(i.dict[key],end=' ')
print(end='\n\n')
def paithreads(ren,name):
for i in name:
ren.append(puke(i))
for i in ren:
i.start()
paithreads(ren,name)
#打印抢地主之前三人的牌
printpai(ren)
#抢地主
dizhuadd(ren)
#打印抢地主之后三人的牌
printpai(ren)