import random
type = ['红桃', '黑桃', '方块', '梅花']
num = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K']
play_card = [[type_one, num_one] for type_one in type for num_one in num]
card_type = ['单张', '对子', '顺子', '顺金', '豹子']
class man():
def __init__(self, name, card, type):
self.name = name
self.card = card
self.type = type
# 发牌
def rand_card():
return random.sample(play_card, 3)
# 判断牌
def panduan(one):
index_list = sorted(one, key=lambda x: num.index(x[1]))
# 判断豹子
if index_list[0][1] == index_list[1][1] == index_list[2][1]:
return 4
# 判断顺金
if (num.index(index_list[0][1]) + 1 == num.index(index_list[1][1])) and (
num.index(index_list[0][1]) + 2 == num.index(index_list[2][1])) and (
num.index(index_list[1][1]) + 1 == num.index(index_list[2][1])):
if index_list[0][0] == index_list[1][0] == index_list[2][0]:
# 顺金
return 3
else:
# 顺子
return 2
# 判断对子
if (index_list[0][1] == index_list[1][1]) or (index_list[0][1] == index_list[2][1]) or (
index_list[2][1] == index_list[1][1]):
return 1
# 单张
return 0
print('开始发牌:')
print('++++++++++++++++++++++++++++++++')
man_list = []
for i in range(5):
card_list = rand_card()
m = man('牌友{}'.format(i + 1), card_list, panduan(card_list))
man_list.append(m)
print("牌友{}的牌是{}".format(i + 1, card_list))
print('++++++++++++++++++++++++++++++++')
man_list.sort(key=lambda x: x.type, reverse=True)
type_num = [one.type for one in man_list]
type_count = type_num.count(max(type_num))
print('开牌结果:')
if type_count == 5:
print("平局!")
else:
print("牌最大的有{}位!".format(type_count))
for i in range(type_count):
print("名字是:{},他的牌是:{}".format(man_list[i].name, card_type[man_list[i].type]))
Python---炸金花小游戏制作
最新推荐文章于 2024-02-01 11:06:38 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)