import random pokers1 = ['2','3','4','5','6','7','8','9','10','J','Q','K','A']
pokers2 = ['大王','小王'] pokers_color = ['红桃','黑桃','梅花','方块'] pokers =
['%s%s'%(i,j) for i in pokers_color for j in pokers1] + pokers2 # 生成一副54张的整牌
random.shuffle(pokers) #洗牌

robot1 = [] robot2 = [] player = [] #玩家的牌 while len(pokers) > 3:#留3张底牌 i =
pokers.pop() robot1.append(i) j = pokers.pop() robot2.append(j) k =
pokers.pop() player.append(k)

def poker_sort(a):#对牌排序函数 a1,a2 = [],[] for i in range(len(a)):#将数字型和非数字型牌挑出来
str_list = list(a[i]) if len(str_list) == 2 or str_list[-1] in
['J','Q','K','A']:#挑出非数字型牌 a2.append(a[i]) else: a1.append(a[i]) #print(a1)
#print(a2) i = 0 while 1:# 先对数字型牌使用冒泡排序法 for j in range(len(a1)-1-i): str_list
= list(a1[j]) str_list1 = list(a1[j+1]) if len(str_list) == 4:# 牌值为10的转换成列表长度为4
str_list[2] = '10' if len(str_list1) == 4: str_list1[2] = '10' if
int(str_list[2]) > int(str_list1[2]): t =a1[j+1] a1[j+1] = a1[j] a1[j] = t i +=
1 if len(a1)-1-i == 1: break #print(a1) i = 0 while 1:# 对非数字型牌使用冒泡排序法 for j in
range(len(a2)-1-i): str_list = list(a2[j]) str_list1 = list(a2[j+1]) if
str_list[-1] == 'J': str_list[-1] = '11' elif str_list[-1] == 'Q': str_list[-1]
= '12' elif str_list[-1] == 'K': str_list[-1] = '13' else: str_list[-1] = '14'
if str_list1[-1] == 'J': str_list1[-1] = '11' elif str_list1[-1] == 'Q':
str_list1[-1] = '12' elif str_list1[-1] == 'K': str_list1[-1] = '13' else:
str_list1[-1] = '14' if str_list[0] == '小' : str_list[-1] = '50'#小王用50代替 if
str_list1[0] == '小' : str_list1[-1] = '50' if str_list[0] == '大': str_list[-1]
= '100'#大王用100代替 if str_list1[0] == '大': str_list1[-1] = '100' if
int(str_list[-1]) > int(str_list1[-1]): t =a2[j+1] a2[j+1] = a2[j] a2[j] = t i
+= 1 if len(a2)-1-i == 1: break #print(a2) a = a1 + a2 #print(a) return a

name = input('请输入玩家名：\n')# 游戏角色为一个玩家加两个机器人 print('欢迎玩家'+name+'!') # 叫地主 key =
input('请问玩家'+name+'叫地主吗（y/n）：\n') if key =='y': player = player + pokers player
= poker_sort(player)#对牌排序函数 从2到A、小王、大王 print(name+'现在是地主，牌为：\n',player) else:
robot1 = robot1 + pokers robot1 = poker_sort(robot1) print('robot1现在是地主！')