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