python模拟实现分发扑克牌

时间:2021-05-22

本文实例为大家分享了python分发扑克牌的具体代码,供大家参考,具体内容如下

52张扑克牌发个4个玩家,每人13张。

要求:

自动生成一幅扑克牌组;洗牌;发牌到玩家手中;将玩家手中扑克牌按花色大小整理好。

思路一

import randomimport operatordef auto(): pokers=[] poker=[] for i in ['♥','♠','♦','♣']: for j in ['A','2','3','4','5','6','7','8','9','10','J','Q','K']: poker.append(i) poker.append(j) pokers.append(poker) poker=[] return pokerspoker=auto()random.shuffle(poker)li={}for k in ['player1','player2','player3','player4']: b=random.sample(poker,13) for s in b: poker.remove(s) li.setdefault(k,b) print('player1:',sorted(li['player1'],key=operator.itemgetter(0,1)))print('player2:',sorted(li['player2'],key=operator.itemgetter(0,1))) print('player3:',sorted(li['player3'],key=operator.itemgetter(0,1)))print('player4:',sorted(li['player4'],key=operator.itemgetter(0,1)))

思路二

import randomimport timeA=['♥','♠','♦','♣']B=['A','2','3','4','5','6','7','8','9','10','J','Q','K']poker=[]pokers=[]n=1for i in A: for j in B: pokers.append((n,(i+j))) n=n+1print("开始洗牌....")random.shuffle(pokers)def xipai(x): for i in x: pokers.remove(i) return pokersdef fapai(y): for i in y: print(i[1],',',end=" ")def paixu(z): for i in z: print(i[1],',',end=" ")time.sleep(3)a=random.sample(pokers,13) pokers=xipai(a) print("开始给player1发牌:\n")print(fapai(a))b=random.sample(pokers,13) pokers=xipai(b) print("开始给player2发牌:\n")print(fapai(b))c=random.sample(pokers,13) pokers=xipai(c) print("开始给player3发牌:\n")print(fapai(c))d=random.sample(pokers,13) pokers=xipai(d) print("开始给player4发牌:\n")print(fapai(d))a.sort()b.sort()c.sort()d.sort()time.sleep(3)print("player1的牌:\n")print(paixu(a))print("player2的牌:\n")print(paixu(b))print("player3的牌:\n")print(paixu(c))print("player4的牌:\n")print(paixu(d))

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章