#题目要求
游戏规则:
一副扑克牌,去掉大小王,每个玩家发3张牌,最后比较大小,看谁赢。
有以下几种牌:
豹子:三张一样的牌,如3张6
同花顺:即3张同样花色的顺子,如红桃5,6,7
顺子:又称拖拉机,花色不同,但是顺子,如 红桃5,方块6 ,黑桃7 ,组成的顺子(据网上所说 `A 2 3` 和 `Q K A` 都为顺子 所以将 `A 2 3`设置为最小的顺子 `Q K A`设置为最大的顺子 )
对子:2张牌一样
单张:单张最大的是A
这几张牌的大小顺序为 豹子>同花顺>同花>顺子>对子>单张
需程序实现的点:
1.先生成一副完整的牌
2.给5个玩家随机发牌
3.统一开牌,比大小,输出赢家是谁
## 下方是参考代码
#coding=utf-8
#@Time : 2023/12/21 17:14
#@Author : ZHL
#@File : 炸金花.py
import random
# Level对应的值
# 豹子 三张一样的 6
# 同花顺 三张同色顺子 5
# 同花 三张一样的牌 4
# 顺子 顺子 花色不同 3
# 对子 对子 2
# 单张 比较最大的一张 1
def findMax(num1,num2):
maxNum = num1
if num1 < num2:
maxNum =num2
return maxNum
def function01(): #返回一个字典 对应牌的值
listOne = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'k', 'A']
listTwo = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
my_dict = dict(zip(listOne, listTwo)) # 使用zip()函数组合两个列表成字典
return my_dict
def function02(CharList): # 传入的列表为 单字符 列表 如果是顺子子返回true 并且返回该列表元素对应的最大的数值
my_dict = function01()
listReturn = [] # 存放转换后的数字列表
for i in CharList:
listReturn.append(my_dict[i])
listReturn.sort() # 将转换后的数字列表进行排序
myBoolean = True
for i in range(0, len(listReturn)-1):
if listReturn[i]+1 != listReturn[i+1]: # 依次判断前后两个差值是否为1 如果为前后两个差值不为1 则返回Flase
myBoolean = False
if listReturn == [1,2,13]: #======== 如果为[A, 2, 3] 则返回True 和 3
return True, 3
return myBoolean, listReturn[len(listReturn) - 1] # 返会返回知否为顺子以及最大值
class Card: #单张牌 有数字和花色两个属性
#red1 red2 black1 black2 四种花色
def __init__(self,a,b):
self.number = a
self.color = b
def show(self):
if self.color == "red1":
return "红桃"
elif self.color == "red2":
return "方框"
elif self.color == "black1":
return "梅花"
else:
return "黑桃"
def show02(self):
return self.show()+self.number
def __repr__(self):
return self.show()+self.number
class Cards: #储存三张牌 方便后面比较
def __init__(self,name):
self.cards = []
self.name = name
def __repr__(self):
str = self.name + "的牌是:"
for i in self.cards:
str += i.show02()+","
return str
def cardAppend(self,appendCard):
self.cards.append(appendCard)
def Level(self): #先分等级 等级高的肯定大于等级低的
numberList = []
colorList = []
this_dict = function01()
for i in self.cards:
numberList.append(i.number)
colorList.append(i.color) #findall(prttern,txt)
numSame = 0
colorSame = 0
for i in colorList:
colorSame = findMax(colorSame,colorList.count(i)) # 3张牌中相同的花色牌的个数
for i in numberList:
numSame = findMax(numSame,numberList.count(i)) #是相同的牌的个数
thisBoolean,maxNum = function02(numberList)
if numSame == 3:
return 6, maxNum #豹子
elif thisBoolean == True:
if colorSame == 3:
return 5, maxNum #同花顺
else:
return 3, maxNum #顺子
elif colorSame == 3:
return 4,maxNum #同花
elif numSame == 2:
for i in numberList:
# if (len(re.findall(i,numberList)) == 2): #如果一个数出现两次
if (numberList.count(i) == 2):
minNum = this_dict[i]
return 2, maxNum, minNum #对子
else:
my_dict = function01()
listReturn = [] # 存放转换后的数字列表
for i in numberList:
listReturn.append(my_dict[i])
listReturn.sort() # 将转换后的数字列表进行排序
midNum = listReturn[1]
minNum = listReturn[0]
return 1, maxNum,midNum,minNum #单牌
class puke(): #生成一副扑克牌
number = ['A','2','3','4','5','6','7','8','9','10','J','Q','k']
color = ["red1","red2","black1","black2"]
cards = []
def __init__(self):
for i in self.number:
for j in self.color:
card = Card(i,j)
self.cards.append(card)
def shuffle(self):
random.shuffle(self.cards)
def start01(self,peopleNum):
if peopleNum > 17 | peopleNum < 0:
print("输入人数不合法!!!")
return
self.shuffle() #打乱牌堆
CardsList = []
for i in range(0, peopleNum):
name = f"people{i+1}"
middle = Cards(name)
for j in range(0, 3):
middle.cardAppend(self.cards[i*3+j])
CardsList.append(middle)
# 测试使用
# one = Card('A', "red1")
# two = Card('2', 'red2')
# three = Card('3', "black1")
#
# CardsTest = Cards("people1")
# CardsTest.cardAppend(one)
# CardsTest.cardAppend(two)
# CardsTest.cardAppend(three)
# CardsList[0] = CardsTest
# print(CardsList)
for i in CardsList:
print(i)
def compare(CardsOne, CardsTwo):
t1, t2, *t3, = CardsOne.Level()
s1, s2, *s3, = CardsTwo.Level()
if (t1 != s1):
return CardsOne if t1 > s1 else CardsTwo
elif (t1 == 1 & s1 == 1):
if (t2 > s2):
return CardsOne
elif (t2 < s2):
return CardsTwo
else:
if (t3[0] > s3[0]):
return CardsOne
elif (t3[0] < s3[0]):
return CardsTwo
else:
return CardsOne if t3[1] > s3[1] else CardsTwo
elif (t1 == 2 & s1 == 2):
if (t3 > s3):
return CardsOne
elif (t3 < s3):
return CardsTwo
else:
return CardsOne if t2 > s2 else CardsTwo
else:
return CardsOne if t2 > s2 else CardsTwo
MaxCards = CardsList[0]
for i in CardsList:
MaxCards = compare(MaxCards,i)
print(f"最后的赢家是{MaxCards.name}")
puke = puke()
peopleNumber = 5 #可以指定参与游戏人数
ok = puke.start01(peopleNumber)