‘’’
编写程序,设计单张扑克牌类Card,具有花色,牌面与具体值。
同时设计整副扑克牌类Cards,具有52张牌。
红桃、黑桃、方片、草花 2345678910JQKA
♥♠♦♣
设计一个发牌的函数,可以任意发出三张牌。
对任意三张牌断定牌的类型。
类型包括:
三条:三张牌value一样
一对:两张value一样
顺子:三张牌挨着
同花:三张牌type一样
同花顺:挨着,类型一样
其余都是散牌
‘’’
import random
class Card():
def
init
(self, type, value, real_value):
self.type = type
self.value = value
self.real_value = real_value
def __str__(self):
return '{}{}'.format(self.type, self.value)
class Cards():
def
init
(self):
# 存放52张牌
self.all_cards = []
# 存放3张牌
self.three_pai = []
# 初始化52张牌
types = ‘♥♠♦♣’
# 获取13种牌面
values = list(range(2, 11)) + list(‘JQKA’)
# 遍历牌面类型
for t in types:
# 遍历牌面值
for i, v in enumerate(values):
# 创建牌
c = Card(t, v, i + 2)
# 将牌放入集合all_cards
self.all_cards.append©
# 设计一个发牌的函数,可以任意发出三张牌。
def fa_pai(self):
# 循环生成3张牌
for i in range(3):
index = random.randint(0, len(self.all_cards) - 1)
self.three_pai.append(self.all_cards.pop(index))
# 判断规则
def pan_duan(self):
'''
类型包括:
三条:三张牌value一样
一对:两张value一样
顺子:三张牌挨着
同花:三张牌type一样
同花顺:挨着,类型一样
其余都是散牌
'''
self.three_pai.sort(key=lambda x: x.real_value)
c1 = self.three_pai[0]
c2 = self.three_pai[1]
c3 = self.three_pai[2]
if c1.value == c2.value == c3.value:
print('三条')
elif c1.type == c2.type == c3.type:
print('同花')
elif c1.value == c2.value or c1.value == c3.value or c2.value == c3.value:
print('一对')
elif (c1.type == c2.type == c3.type) and (
c1.real_value + 1 == c2.real_value and c2.real_value + 1 == c3.real_value):
print('同花顺')
elif c1.real_value + 1 == c2.real_value and c2.real_value + 1 == c3.real_value:
print('顺子')
else:
print('散牌')
def show_all(self):
#self.three_pai.sort(key=lambda x: x.real_value)
for c in self.three_pai:
print(c)
cards = Cards()
cards.fa_pai()
cards.show_all()
cards.pan_duan()