简单的用不重复随机random.simple()以及列表取值的pop()函数做一个斗地主自动洗牌发牌小代码,因为没有什么技术含量,就不做注释及说明了。(纯小白,大佬请忽略)
import random
def xipai():
paixing=['A','2','3','4','5','6','7','8','9','10','J','Q','K']
all_pai=['小王','大王']
for i in paixing:
all_pai.append(i)
all_pai.append(i)
all_pai.append(i)
all_pai.append(i)
return all_pai
def fapai(all_pai):
i=random.sample(range(54),17)
A = []
a=sorted(i,reverse=True)
for j in a:
A.append(all_pai.pop(j))
i=random.sample(range(37),17)
B = []
a=sorted(i,reverse=True)
for j in a:
B.append(all_pai.pop(j))
i=random.sample(range(20),17)
C = []
a=sorted(i,reverse=True)
for j in a:
C.append(all_pai.pop(j))
return A,B,C,all_pai
a=xipai()
A,B,C,D=fapai(a)
print(f'A:{A}')
print(f'B:{B}')
print(f'C:{C}')
print(f'D:{D}')
代码片
一次运行结果:
A:['K', 'Q', 'J', '10', '10', '8', '7', '6', '6', '3', '3', '3', '2', '2', '2', 'A', '小王']
B:['J', 'J', 'J', '10', '9', '9', '9', '8', '8', '7', '5', '5', '4', '4', '3', '2', 'A']
C:['K', 'K', 'K', 'Q', 'Q', '9', '8', '7', '7', '6', '6', '5', '5', '4', 'A', 'A', '大王']
D:['4', '10', 'Q']