Python simulation to realize the distribution of playing cards

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

Python simulation to realize the distribution of playing cards
Python simulation of the landlord deal
Python implements the shuffling of the cards in Doudizhu
Use python to realize the aircraft war game
How to understand the introduction of packages in Python
Consolidate the foundation of Python (4)
Consolidate the foundation of Python(6)
Consolidate the foundation of Python(5)
Consolidate the foundation of Python (3)
How to find the area of a circle in python
Minimalism is the soul of Python | Python code to find bugs (10)
The usage of wheel in python
Python handles the 4 wheels of Chinese
What is the use of Python
Scrapy simulation login of Python crawler
The premise of Python string pooling
Secrets of the new features of Python 3.8
How to view the python module
The father of Python joins Microsoft
The operation of python access hdfs
The usage of tuples in python
End the method of running python
Understanding the meaning of rb in python
Can Python implement the structure of the stack?
Learn the basics of python interactive mode
How to verify successful installation of python
What are the required parameters of python
Logistic regression at the bottom of python
The usage of Ajax in Python3 crawler
Python solves the Tower of Hanoi game
Python string to judge the password strength
Solve the conflict of multiple versions of python
What is the scope of python variables
Python implements the sum of fractional sequences
Two days of learning the basics of Python
200 lines of Python code to achieve snake
What is the id function of python
Where is the pip path of python3
The essence of Python language: Itertools library
What are the advantages of python language
The specific method of python instantiation object
Python novice learns to use the library
python3 realizes the function of mask drawing
Introduction to the Ubuntu-based derivative distribution PopOS
How to learn the Python time module
What is the prospect of python development
What is the function body of python
The specific method of python import library
Solve the conflict of multiple versions of python
What is the function of adb in python
Detailed explanation of the principle of Python super() method
How to use the round function in python
The difference between the syntax of java and python
How to use the zip function in Python
Python realizes the development of student management system
Python uses the email module to send mail
Solve the problem of python running startup error
How to use the format function in python
Can the value of the python dictionary be modified?
Detailed explanation of the usage of Python decimal module
500 lines of python code to achieve aircraft war