难点就是在比较那边,对子, A顺判断
import random
import string
def chooseMax(list,type):
maxMark = []
max = 0
'''
surname 豹子
tierce 顺金
straight 顺子
double 对子
single 单牌
'''
if type == 'surname':
for i in list:
now = tempJudge[i[0][2:]]
if now==1:
maxMark.append(i)
break
else:
now = tempJudge[i[0][2:]]
if now>max:
max = now
maxMark = i
elif type=='double':
maxMark = getDoubleMax(list)
else:
for i in list:
now = getMax(i)
if now > max:
max = now
maxMark = i
print(maxMark, '最大')
def getDoubleMax(lists):
maxMark = []
unSame = []
max=0
for list in lists:
if list[0] == list[1] or list[0] == list[2]:
now = tempJudge[list[0][2:]]
unSame.append(list[2][2:])
else:
now = tempJudge[list[1][2:]]
unSame.append(list[0][2:])
if now > max:
max = now
maxMark= list
elif now==max:
p1 = tempJudge[unSame[0]]
p2 = tempJudge[unSame[1]]
if p1==p2:
continue
else :
if p2>p1:
maxMark=list
unSame.remove(unSame[0])
else:
if p2==1:
maxMark=list
unSame.remove(unSame[0])
continue
unSame.remove(unSame[1])
return maxMark
def check(card):
isStraight = False
com = []
com.append(tempJudge[card[0][2:]])
com.append(tempJudge[card[1][2:]])
com.append(tempJudge[card[2][2:]])
small = 0
middle = 0
max = 0
if (com[1] > com[0]):
if (com[1] > com[2]):
max = com[1]
else:
max = com[2]
else:
if (com[0] > com[2]):
max = com[0]
else:
max = com[2]
com.remove(max)
if (com[0] > com[1]):
middle = com[0]
small = com[1]
else:
middle = com[1]
small = com[0]
if (1 in com):
if (small + 1 == max - 1 == middle or small + middle == max == 13):
isStraight = True
else:
if (small + 1 == max - 1 == middle):
isStraight = True
return isStraight
def cards():
num = list(range(2, 11))
num.extend('JQKA')
color = ('梅花', '方块', '红桃', '黑桃')
card = [f'{m}{n}' for n in num for m in color]
# card=1
return card
f = [random.sample(cards(), 3) for m in range(176)]
def getMax(card):
com = []
com.append(tempJudge[card[0][2:]])
com.append(tempJudge[card[1][2:]])
com.append(tempJudge[card[2][2:]])
max = 0
if (com[1] > com[0]):
if (com[1] > com[2]):
max = com[1]
else:
max = com[2]
else:
if (com[0] > com[2]):
max = com[0]
else:
max = com[2]
return max
''''
surname 豹子
tierce 顺金
straight 顺子
double 对子
single 单牌
'''
cardMap = {
'surname': 5,
'tierce': 4,
'straight': 3,
'double': 2,
'single': 1
}
tempJudge = {
'A': 1,
'2': 2,
'3': 3,
'4': 4,
'5': 5,
'6': 6,
'7': 7,
'8': 8,
'9': 9,
'10': 10,
'J': 11,
'Q': 12,
'K': 13,
}
compareMap = {
'win': [],
'maxType': 'single',
'surname': [],
'tierce': [],
'straight': [],
'double': [],
'single': []
}
colorMap = {
'红桃':1,
'方块':1,
'梅花':2,
'黑桃':2
}
print('生成的牌', f)
# print(cardMap['single'])
if __name__ == '__main__':
for card in f:
max = cardMap[compareMap['maxType']]
if (card[0][2:] == card[1][2:]):
if (card[1][2:] == card[2][2:]):
'''豹子'''
compareMap['maxType'] = 'surname'
compareMap['surname'] = card
else:
# 对子
if (max > cardMap['double']):
continue
compareMap['double'].append(card)
compareMap['maxType'] = 'double'
else:
if (card[1][2:] == card[2][2:]):
# 对子
if (max > cardMap['double']):
continue
compareMap['double'].append(card)
compareMap['maxType'] = 'double'
else:
if (check(card)):
# 顺子
if (max > cardMap['straight']):
continue
compareMap['straight'].append(card)
compareMap['maxType'] = 'straight'
if (colorMap[card[0][:2]] == colorMap[card[1][:2]] and colorMap[card[1][:2]] == colorMap[card[2][:2]]):
# 同花顺
if (max > cardMap['tierce']):
continue
compareMap['tierce'].append(card)
compareMap['maxType'] = 'tierce'
else:
# 单
if (max > cardMap['single']):
continue
compareMap['single'].append(card)
print(compareMap)
type = compareMap['maxType']
re = compareMap[type]
if isinstance(re[0],str) :
print('最大牌', re)
else:
chooseMax(re,type)
代码仅供参考!