任务描述
1.定义一个单张扑克类(考虑需要哪些属性),定义一个一副扑克牌类,该类包含一个单张扑克对象的数组(不考虑大小王)。实现一个模拟扑克发牌洗牌的算法; 2.电脑随机发出5张牌,判断是以下哪种牌型?(提示,利用各种集合的特性可以简化判断)
输出
import random
colorLst = ['红桃', '方片', '黑桃', '草花']
class Card:
def __init__(self, color, value):
self.value = value
self.color = color
def __str__(self):
strvalue = str(self.value)
if self.value == 11:
strvalue = 'J'
elif self.value == 12:
strvalue = 'Q'
elif self.value == 13:
strvalue = 'K'
elif self.value == 1:
strvalue = 'A'
return self.color + str(strvalue)
class Poke:
def __init__(self):
self.cards = []
for color in colorLst:
for value in range(1, 14):
card = Card(color, value)
self.cards.append(card)
def output(self):
index = 1
for card in self.cards:
print(card, end='\t')
if index % 13 == 0:
print()
index += 1
poke = Poke()
poke.output()
random.shuffle(poke.cards)
print('洗牌之后')
poke.output()
hands = poke.cards[:5]
print('分到的一手牌是')
for card in hands:
print(card, end='\t')
print('\n牌型是:', end='')
bSameColor = False
bShunZi = False
cardColors = [card.color for card in hands]
cardValues = [card.value for card in hands]
cardValuesSet = set(cardValues)
cardColorsSet = set(cardColors)
if len(cardColorsSet) == 1:
bSameColor = True
cardValuesSorted = sorted(cardValues)
if cardValuesSorted[4] - cardValuesSorted[0] == 4 and len(cardValuesSet) == 5:
bShunZi = True
if bSameColor and bShunZi:
print('同花顺')
exit(0)
if bSameColor:
print('同花')
exit(0)
if bShunZi:
print('顺子')
exit(0)
if len(cardValuesSet) == 4:
print('一对')
if len(cardValuesSet) == 5:
print('杂牌')
if len(cardValuesSet) == 2:
if cardValues.count(cardValues[0]) == 1 or cardValues.count(cardValues[0]) == 4:
print('四带一')
else:
print('三带二')
bisThreeOneOne = False
if len(cardValuesSet) == 3:
for value in cardValues:
if cardValues.count(value) == 3:
bisThreeOneOne = True
break
if bisThreeOneOne == False:
print('221')
else:
print('311')
请先登陆 或 注册