编程语言
首页 > 编程语言> > python——德州扑克

python——德州扑克

作者:互联网

  德州扑克

  4种花色 每个花色的牌从A-K

  功能如下:

1.洗牌:将52张牌打乱

2.发牌:洗牌结束后,发给玩家五张牌

3.验证玩家的牌型

 牌型分以下几种

 A.同花顺

 B.同花

 C.顺子

 D.四条(7777x)

 E.三条(777xx)

 F.一对(77xxx)

 G.两对(7788x)

 H.满堂红(77788)三条带一对子

 I.正常牌(24567)

  1 import random
  2 import time
  3 
  4 # 创建一副牌
  5 card_list = []
  6 for i in ['A', 2, 3, 4, 5, 6, 7, 8, 9, 10, 'J', 'Q', 'K']:
  7     for j in ['黑桃', '红桃', '方块', '梅花']:
  8         card = j + str(i)
  9         card_list.append(card)
 10 
 11 # 加载进度动画
 12 fmt = '{:3d} [{:<20}]'.format
 13 
 14 
 15 def progressbar():
 16     for n in range(21):
 17         time.sleep(0.1)
 18         print('\r', fmt(n * 5, '=' * n), end='')
 19 
 20 
 21 # 洗牌
 22 def shuffle():
 23     random.shuffle(card_list)
 24     progressbar()
 25     print('\n' + '洗牌完成')
 26     print(card_list)
 27 
 28 
 29 # 发牌
 30 def deal():
 31     print('发给你的牌为:')
 32     time.sleep(1)
 33     print(card_list[0:5])
 34     time.sleep(1)
 35 
 36 
 37 # 判断牌
 38 def showdown():
 39     color = []
 40     size = []
 41     # 提取五张牌的花色
 42     for k in range(1, 6):
 43         color.append(str(card_list[k - 1][0:2]))
 44     # 提取五张牌的大小
 45     for h in range(1, 6):
 46         size5 = str(card_list[h - 1][2:len(card_list[h - 1])])
 47         if size5 == "J":
 48             size5 = 11
 49         elif size5 == "Q":
 50             size5 = 12
 51         elif size5 == "K":
 52             size5 = 13
 53         elif size5 == "A":
 54             size5 = 1
 55         size.append(int(size5))
 56 
 57     size_set = list(set(size))
 58 
 59     # 判断牌
 60     while color[0] == color[1] == color[2] == color[3] == color[4]:
 61         if max(size) - min(size) == 4:
 62             print("同花顺")
 63             break
 64         print('同花')
 65         break
 66     else:
 67         if size == list(set(size)) and max(size) - min(size) == 4:
 68             print('顺子')
 69         elif len(size) - 1 == len(size_set):
 70             print('一对')
 71         elif len(size) - 2 == len(size_set):
 72             for a in range(0, 5):
 73                 for b in range(0, 3):
 74                     if size[a] == size_set[b]:
 75                         size[a] = 0
 76                         size_set[b] = 0
 77             last = [x for x in size if x != 0]
 78             if last[0] == last[1]:
 79                 print('三条')
 80             else:
 81                 print('两对')
 82         elif len(size) - 3 == len(size_set):
 83             for a in range(0, 5):
 84                 for b in range(0, 2):
 85                     if size[a] == size_set[b]:
 86                         size[a] = 0
 87                         size_set[b] = 0
 88             last = [x for x in size if x != 0]
 89             if last[0] == last[1] == last[2]:
 90                 print('四条')
 91             else:
 92                 print('满堂红')
 93         else:
 94             print('正常牌')
 95 
 96 
 97 if __name__ == "__main__":
 98     # 准备
 99     print('这儿有一副牌:')
100     time.sleep(1)
101     print(card_list)
102     time.sleep(1)
103     print('开始洗牌')
104 
105     # 洗牌
106     shuffle()
107 
108     # 发牌
109     deal()
110 
111     # 亮牌
112     showdown()

 

标签:10,python,list,牌型,import,德州,扑克,card
来源: https://www.cnblogs.com/yzy1314/p/12001203.html