Python implements the shuffling of the cards in Doudizhu

The examples in this article share with you the specific code of python to realize the shuffle of Doudizhu cards, for your reference, the specific content is as follows

1、 Create a sequence with 54 elements in total, representing a deck of playing cards, and there is an order in initialization.
2、 Write a piece of code to complete the shuffling function, that is, the sequence just now changes from an ordered arrangement to a random disorder.
3、 Write a piece of code to complete the card cutting function, that is, find a random number from 1-54, and start the sequence just after shuffling from this random number.
4、 Write a piece of code to complete the card-dealing function, and distribute the disordered sequence just now after the card cut into three sequences in order, and the remaining three hole cards (also a sequence).
5、 Write a piece of code to complete the card sorting function. First, sort each sequence after the card is divided (with its own function), and then display it in the order of the card type from largest to smallest.
6、 Complete the function of randomly calling the landlord, a landlord randomly appears, and three hole cards are assigned to the landlord to form a new sequence and arrange them in descending order.
7、 To display the name of the card, such as: Spade2 (spade 2), Heart2 (heart 2), Club2 (club 2), Diamond2 (square piece 2), etc., you can also use abbreviations: S2, H2, C2, D2, etc. . You can use the mapping (ie dictionary), or you can write your own display program.

code show as below

poker=[]   #Generate a deck of cards
pokers=[]
num=101    #Use 101 to refer to 1, 102 to spades, 1, 201 to spades 2 for length inrange(0,52):
poker.append(num)
num+=1if(length+1)%4==0:
num-=4
num+=10
poker.extend([230,240])print(poker)import random    #Shuffle odd numbers into a group and even numbers into a group
for leng inrange(0,8): 
poker1=[]
poker2=[]for length inrange(0,54):if length%2==0:
poker1.append(poker[length])else:
poker2.append(poker[length])
temp=random.randrange(0,2) #Add even and odd when it is 1, add even and odd when it is 2, a total of 2**8 results
if temp%2==0:
poker=poker1+poker2
else:
poker=poker2+poker1
print(poker)
number=int(input("Please enter the position of the cut card;")) #Cut cards
poker1=poker[0:number]
poker2=poker[number:54]
poker=poker2+poker1
print(poker)
player1=[]    #Split
player2=[]
player3=[]for length inrange(0,51):if length%3==0:
player1.append(poker[length])if length%3==1:
player2.append(poker[length])if length%3==2:
player3.append(poker[length])for length inrange(51,54):  #The remaining three cards (the landlord's card)
num=(poker[length]-90)//10
temp=num
if num==11:
temp='J'if num==12:
temp='Q'if num==13:
temp='K'if poker[length]%10==1:
name=str(temp)+'S'
pokers.append(name)
elif poker[length]%10==2:
name=str(temp)+'H'
pokers.append(name)
elif poker[length]%10==3:
name=str(temp)+'C'
pokers.append(name)
elif poker[length]%10==4:
name=str(temp)+'D'
pokers.append(name)
elif poker[length]==230:
name='Xiao Wang'
pokers.append(name)
elif poker[length]==240:
name='King'
pokers.append(name)print(pokers)
power=random.randrange(1,4)if power==1:
player1.extend(poker[51:54])if power==2:
player2.extend(poker[51:54])if power==3:
player3.extend(poker[51:54])
player1.sort()    #Descending
player2.sort()  
player3.sort()
player1=player1[::-1]
player2=player2[::-1]
player3=player3[::-1]
player1s=[]    #display
player2s=[]
player3s=[]for length inrange(0,len(player1)):  #Show player1
num=(player1[length]-90)//10
temp=num
if num==11:
temp='J'if num==12:
temp='Q'if num==13:
temp='K'if player1[length]%10==1:
name=str(temp)+'S'
player1s.append(name)
elif player1[length]%10==2:
name=str(temp)+'H'
player1s.append(name)
elif player1[length]%10==3:
name=str(temp)+'C'
player1s.append(name)
elif player1[length]%10==4:
name=str(temp)+'D'
player1s.append(name)
elif player1[length]==230:
name='Xiao Wang'
player1s.append(name)
elif player1[length]==240:
name='King'
player1s.append(name)for length inrange(0,len(player2)):  #Show player2
num=(player2[length]-90)//10
temp=num
if num==11:
temp='J'if num==12:
temp='Q'if num==13:
temp='K'if player2[length]%10==1:
name=str(temp)+'S'
player2s.append(name)
elif player2[length]%10==2:
name=str(temp)+'H'
player2s.append(name)
elif player2[length]%10==3:
name=str(temp)+'C'
player2s.append(name)
elif player2[length]%10==4:
name=str(temp)+'D'
player2s.append(name)
elif player2[length]==230:
name='Xiao Wang'
player2s.append(name)
elif player2[length]==240:
name='King'
player2s.append(name)for length inrange(0,len(player3)):  #Show player3
num=(player3[length]-90)//10
temp=num
if num==11:
temp='J'if num==12:
temp='Q'if num==13:
temp='K'if player3[length]%10==1:
name=str(temp)+'S'
player3s.append(name)
elif player3[length]%10==2:
name=str(temp)+'H'
player3s.append(name)
elif player3[length]%10==3:
name=str(temp)+'C'
player3s.append(name)
elif player3[length]%10==4:
name=str(temp)+'D'
player3s.append(name)
elif player3[length]==230:
name='Xiao Wang'
player3s.append(name)
elif player3[length]==240:
name='King'
player3s.append(name)iflen(player1s)==17:print('Farmer:',player1s)else:print('Landlord:',player1s)iflen(player2s)==17:print('Farmer:',player2s)else:print('Landlord:',player2s)iflen(player3s)==17:print('Farmer:',player3s)else:print('Landlord:',player3s)

(The display can be compiled as a function first, and quoted when used, which can reduce the number of lines of code)
(This method does not use python built-in functions, if you find it troublesome, you can optimize it again on this basis)

More interesting classic mini game implementation topics, share with you:

C++ classic games summary

Python classic games summary

python tetris game collection

JavaScript classic games are constantly playing

JavaScript classic games summary

The above is the whole content of this article, I hope it will be helpful to everyone's study.

Recommended Posts

Python implements the shuffling of the cards in Doudizhu
The usage of tuples in python
Understanding the meaning of rb in python
The usage of Ajax in Python3 crawler
What is the function of adb in python
The meaning and usage of lists in python
Python implements the source code of the snake game
The consequences of uninstalling python in ubuntu, very
How to understand the introduction of packages in Python
Python simulation to realize the distribution of playing cards
Consolidate the foundation of Python (4)
Consolidate the foundation of Python(7)
Subscripts of tuples in Python
Consolidate the foundation of Python(6)
Consolidate the foundation of Python(5)
Consolidate the foundation of Python (3)
Python implements the brick-and-mortar game
How to find the area of a circle in python
Learn the hard core operation of Python in one minute
Use of Pandas in Python development
How Python implements the mail function
Python simply implements the snake game
Python handles the 4 wheels of Chinese
Python3 implements the singleton design pattern
Use of numpy in Python development
Python simulation of the landlord deal
What is the use of Python
Detailed usage of dictionary in Python
Usage of os package in python
​What are the numbers in Python?
Python implements the steepest descent method
Python implements the actual banking system
Talking about the modules in Python
The premise of Python string pooling
Secrets of the new features of Python 3.8
How Python implements the timer function
Python implements the aircraft war project
The father of Python joins Microsoft
Python implements horizontal stitching of pictures
The operation of python access hdfs
End the method of running python
Description of in parameterization in python mysql
Can Python implement the structure of the stack?
Learn the basics of python interactive mode
What are the required parameters of python
Python implements alternate execution of two threads
Implementation of JWT user authentication in python
Python solves the Tower of Hanoi game
Detailed explanation of the use of pip in Python | summary of third-party library installation
What is the scope of python variables
Two days of learning the basics of Python
What is the id function of python
Analysis of glob in python standard library
Where is the pip path of python3
Method of installing django module in python
The essence of Python language: Itertools library
python implements the gradient method python the fastest descent method
What are the advantages of python language
The specific method of python instantiation object
python3 realizes the function of mask drawing
What is the prospect of python development