SoFunction
Updated on 2024-11-12

Python simulates the landlord's card dealing

In this article, we share the example of python simulation of the landlord to deal the specific code for your reference, the details are as follows

Title:The Landlord of the Funny Hundred Questions

Poker is a very popular game and there are many games related to poker in computers. For example, Solitaire, Battle of Hearts, etc., which comes with the Windows operating system. In poker games, it is often necessary to perform a shuffle operation, which is to completely disrupt a deck of cards so that their arrangement is not regular.

Request:

1. 54 playing cards are dealt to 3 players, 17 to the farmer and 20 to the landlord.
2、Automatically generate a poker deck; shuffle the cards; deal the cards to the players; organize the poker cards in the players' hands according to the size of the suits. **

'''
Fun Hundred Questions-Dealing Poker-Fighting Landlord
Please program to deal poker cards

Version: 0.1
Author: jasn
Date: 2020-01-01

Main Knowledge Points: Usage of random, class definition and invocation.
The main function points: using Python classes to realize the initialization, shuffling, dealing, grabbing the landlord, card management, suit correspondence of the landlord. The code is as follows
'''
import random

class doudizhu:
 #define 54 cards
 def __init__(self):
  =[]
  for i in range(54):
   (i)
 #Shuffle
 def xipai(self):
  () #Shuffle
  n = (1, 54)
  b = [:n] # Cut the cards from n's position
  c = [n:]
   = b + c

  #Dealing
 def fapai(self):
  self.user1=[0:-3:3] #Player 1, gets cards in the order 0, 3, 6, 9...
  self.user2=[1:-3:3] #Player 2, gets cards in the order 1, 4, 7, 10...
  self.user3=[2:-3:3] #Player 3, gets cards in the order of 2, 5, 8, 11...
  self.user4=[-3:] #Bottom cards. Order is 51, 52, 53.
 #Grab the landlord
 def qiangdizhu(self):
  i=(1,3)
  =i #Define an instance of a landlord
  if i == 1:
   self.user1+=self.user4
  if i == 2:
   self.user2+=self.user4
  if i == 3:
   self.user3+=self.user4
 #code plate
 def mapai(self):
  self.(reverse=True) ##Small to large yardage tags
  self.(reverse=True)
  self.(reverse=True)
 # The order of the cards corresponds to the suit.
 def yingshe(self):
  huase = [(0, 'Square Piece 3'), (1, 'Plum Blossom 3'), (2, '3 of hearts'), (3, '3 of Spades'),
     (4, 'Square Piece 4'), (5, 'Plum Blossom 4'), (6, '4 of hearts'), (7, '4 of Spades'),
     (8, 'Square Piece 5'), (9, 'Plum Blossom 5'), (10, '5 of hearts'), (11, '5 of Spades'),
     (12, 'Square Piece 6'), (13, 'Plum Blossom 6'), (14, '6 of Hearts'), (15, 'Six of Spades'),
     (16, 'Square Piece 7'), (17, 'Plum Blossom 7'), (18, '7 of Hearts'), (19, '7 of Spades'),
     (20, 'Square Piece 8'), (21, 'Plum Blossom 8'), (22, '8 of Hearts'), (23, '8 of Spades'),
     (24, 'Square Piece 9'), (25, 'Plum 9'), (26, '9 of hearts'), (27, '9 of Spades'),
     (28, 'Square Piece 10'), (29, 'Plum 10'), (30, '10 of Hearts'), (31, '10 of Spades'),
     (32, 'Square J'), (33, 'Plum Blossom J'), (34, 'J of Hearts'), (35, 'Jack of Spades'),
     (36, 'Square Piece Q'), (37, 'Plum Q'), (38, 'Q of Hearts'), (39, 'Q of Spades'),
     (40, 'Square K'), (41, 'Plum K'), (42, 'K of Hearts'), (43, 'K of Spades'),
     (44, 'Square Piece A'), (45, 'Plum A'), (46, 'Ace of Hearts'), (47, 'Ace of Spades'),
     (48, 'Square Piece 2'), (49, 'Plum Blossom 2'), (50, '2 of hearts'), (51, '2 of Spades'),
     (52, 'BlackJoker'), (53, 'RedJoker')]
  zdpai = dict(huase)
  paiuser1=''
  for i in range(len(self.user1)):
   paiuser1+=zdpai[self.user1[i]]+' ' # Storing cards as strings
  paiuser2 = ''
  for i in range(len(self.user2)):
   paiuser2 += zdpai[self.user2[i]] + ' '
  paiuser3 = ''
  for i in range(len(self.user3)):
   paiuser3 += zdpai[self.user3[i]] + ' '
  paiuser4 = ''
  for i in range(len(self.user4)):
   paiuser4 += zdpai[self.user4[i]] + ' '

  self.user1 = paiuser1 # Reassign the sequence of cards corresponding to good suits to the instance attributes of the three players
  self.user2 = paiuser2
  self.user3 = paiuser3
  self.user4 = paiuser4


if __name__ == '__main__':
 Player=doudizhu() #Assist the class to the playes for easy calling.
 ()
 ()
 ()
 ()
 ()

print('The landlord of the game is:player (of computer games){}'.format())
print('Undercard:',Player.user4)
print('Player One:',Player.user1)
print('Player Two:',Player.user2)
print('Player Three:',Player.user3)

This is the whole content of this article.