The examples in this article share the specific code of python distributing playing cards for your reference. The specific content is as follows
52 Four playing cards are dealt to 4 players, each with 13 cards.
Claim:
Automatically generate a poker deck; shuffle the cards; distribute the cards to the players; arrange the poker cards in the players' hands according to suit size.
Idea one
import random
import operator
def 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 pokers
poker=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)))
Idea two
import random
import time
A=['♥','♠','♦','♣']
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("Start shuffling....")
random.shuffle(pokers)
def xipai(x):for i in x:
pokers.remove(i)return pokers
def 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("Start dealing cards to player1:\n")print(fapai(a))
b=random.sample(pokers,13)
pokers=xipai(b)print("Start dealing cards to player2:\n")print(fapai(b))
c=random.sample(pokers,13)
pokers=xipai(c)print("Start dealing cards to player3:\n")print(fapai(c))
d=random.sample(pokers,13)
pokers=xipai(d)print("Start dealing cards to player4:\n")print(fapai(d))
a.sort()
b.sort()
c.sort()
d.sort()
time.sleep(3)print("Player1's cards:\n")print(paixu(a))print("Player2's cards:\n")print(paixu(b))print("Player3's cards:\n")print(paixu(c))print("Player4's cards:\n")print(paixu(d))
The above is the whole content of this article, I hope it will be helpful to everyone's study.
Recommended Posts