Work goof series | Python command-line development Landlords

Foreword

Good weekend - we recently updated analysis of the data content is not too much know you have not fatigue, brought to you today is how to work how senior goof: development using a command-line Python Landlords game , using the time to run the program Landlords would not be a very happy, the key is no time limit, you can not only play with the boss can call on at any breakpoint play

doudizhu.mov

Then simply talk about how to use python Landlords develop such a game, let's think about the process of a game Landlords experienced: first generate 54 poker, then these 54 poker players were randomly distributed to three, each player 17 cards and keep three cards, then starting from the first player to grab the landlord asked whether, if it becomes the landlord were given three cards, so in turn the cycle until the landlord appears, is based on the cards after the completion of a series of rules action cards, the first card of a complete victory and who accumulate points (joy beans) in accordance with the scoring rules.

Because only a specified number of their own recreational use so we do not do UI interface, without logging no registration, no sub-client, server-side complex content and some messaging, etc., in short, only need to complete the licensing shuffling the cards and generate rules the robot can accompany play. Therefore, we use only pure python does not depend on any third-party libraries to develop.

Shuffle licensing and implementation

Shuffle and licensing is better to get, basically using python standard library can be random, such as creating library

import random
def get_Card():
    card_type = ['黑桃','红桃','方片','梅花']
    card_values= ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K']
    n=1
    cards = ['JOKER','joker']
    for i in card_type:
        for j in card_values:
            cards.append((i+j))
            n += 1
    return cards

Shuffle and Licensing

def send_Card(cards):
    print('开始洗牌')
    wash_card = random.shuffle(cards) #使用shuffle打乱
    print('开始发牌')
 
    print('底牌为 : %s' %cards[0:3])
 
    player1 = cards[3:20]
    player2 = cards[20:37]
    player3 = cards[37:54]
 
    print ('player1 : '+ str(player1))
    print ('player2 : '+ str(player2))
    print ('player3 : '+ str(player3))

 

Implementation and use other functions

 

Then the cards and play cards in the process of decision rules would be difficult to get, so they can only help GitHub great God, and unfortunately most of the projects is quite large Landlords interactive game development, does not meet the needs of our simple version, after Find some modifications eventually put together a program to achieve Landlords command before our proposed line (not dependent on any third-party libraries), take a look at some code, for example, determined that the two players the cards

def legal(cards1,cards2):
  type1,number1 = cardtype(cards1)
  type2,number2 = cardtype(cards2)
  if type2 == "rocket": #火箭
    return True
  if type2 == "bomb":
    if type1 == "bomb":
      if number2[0] > number1[0]:
        return True
      else:
        return False
    else:
      return True
  if type1 == "single": #单牌
    if type2 == "single":
      if number2 > number1:
        return True
      else:
        return False
    else:
      return False
  if type1 == "pair": #对子    if type2 == "pair":
      if number2[0] > number1[0]:
        return True
      else:
        return False
    else:
      return False
  if type1 == "3+1": #三带一
    if type2 == "3+1":
      if number2[0] > number1[0]:
        return True
      else:
        return False
    else:
      return False
  if type1 == "4+2": #4带2
    if type2 == "4+2":
      if number2[0] > number1[0]:
        return True
      else:
        return False
    else:
      return False
  if type1 == "straight": #顺子
    if type2 == "straight":
      if number2[1] == number1[1]:
        if number2[0] > number1[0]:
          return True
        else:
          return False
      else:
        return False
    else:
      return False
######################其他规则

AI is that we generate in accordance with what rules the cards? (You can modify ai.py part of the code adjustment rules)

Analysis of hand

先看是不是王炸
看是不是炸弹,如果是对手的炸弹,能打就打
再看是不是大于K,如果是队友出的,就过牌,否则就试着打掉,用炸弹
如果对手手牌少于9张出大于K或者出的大于8张的飞机/顺子/连对有王炸/炸弹就用掉
否则无脑跟 不用炸弹和火箭
如果跟对方牌并且对方小于9张,没有1,2张的牌就拆2,3张的牌打
自己出牌lastcard应该是[]
若自己手牌小于5张,先出王炸,炸弹,最大牌
再看下家手牌数量和阵营 同阵营出最小单/对 不同阵营出比他多的牌或者大牌

 

Due to space reasons do not show all the code. In the "early python" No public reply back Landlords can download all the source code.

Use is also very simple, enter doudizhu file folder from the command line execution python main.pycan be. I remember the sound off or remove the source code bgm relevant code when fishing in troubled waters. Of course, I do not get up early to say bgm Landlords Landlords are soulless, Note: If for Landlords work was dismissed, shall not be responsible for this public number. I wish you all a good weekend, bye ~

Published 38 original articles · won praise 229 · views 30000 +

Guess you like

Origin blog.csdn.net/weixin_41846769/article/details/104856348