扑克牌发牌程序
功能:
3人斗地主随机发牌系统。共54张扑克牌,每人17张基础牌,随机地主再发3张牌
编程思路:
1、枚举所有花色,除大王外所有牌值,运用三元表达式遍历得到52张牌,再添加大小王,凑齐54张牌,创建deal函数进行发牌.
2、random.sample从puke中随机抽取17张牌添加到lis列表中.再将抽取到的17张牌从puke中移除.循环3次.最后puke中只剩下3张地主牌.
3、此时lis中有三组牌,故索引为0,1,2.利用random.randint(0,2)随机生成一个数对应索引,将剩余的3张牌进行分配.故完成扑克发牌
代码知识点分析
https://changxuan.blog.csdn.net/article/details/124591081
import random color=['♠','♣','♥','♦'] numbers=['A','2','3','4','5','6','7','8','9','10','J','Q','K'] puke=[i+k for i in color for k in numbers] puke.extend(['大王','小王']) def deal(puke:list): lis=list() for i in range(3): lis.append(random.sample(puke,17)) for k in lis[i]:puke.remove(k) else: lis[random.randint(0,2)].extend(puke) return lis [print('player:'+str(i)) for i in deal(puke)]