#生成一副牌
""" 编写炸金花游戏程序
自己写一个程序,实现发牌、比大小判断输赢。
游戏规则:
一付扑克牌,去掉大小王,每个玩家发3张牌,最后比大小,看谁赢。有以下几种牌:
豹子:三张一样的牌,如3张6.
顺金:又称同花顺,即3张同样花色的顺子, 如红桃 5、6、7
顺子:又称拖拉机,花色不同,但是顺子,如红桃5、方片6、黑桃7,组成的顺子对子:2张牌一样单张:单张最大的是A
这几种牌的大小顺序为, 豹子>顺金>同花>顺子>对子>单张
需程序实现的点:
1.先生成一付完整的扑克牌
2.给5个玩家随机发牌 AZH
<1)
0 在这里输入你要搜索的内容"""
import random
def Alex():
lista = ["♣", "♦", "♠", "♥"]
listb = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"]
listc = []
for a in lista:
count = 2
for b in listb:
card=[f"{a}{b}",count]
listc.append(card)
count+=1
return listc
pokelist=Alex()#整幅牌
#设置玩家开始玩
plays = ["大佬", "阿斯顿马丁", "小刘", "小武", "小王"]
def wanjia(piatersdemo,pokelistdemo,pokenum):
dict={}
for names in piatersdemo:
p_cards=random.sample(pokelistdemo,pokenum)#给我每位玩家随机发3张牌;从这副牌牌中随机那3个
for card in p_cards:
pokelistdemo.remove(card)#发完牌以后;避免有一张牌出现2次,所以吧发过的牌从整幅牌中删除
dict[names]=p_cards
print(f"为玩家{names},发的牌是{p_cards}")
return dict
playerDic=wanjia(plays,pokelist,3)#玩家手中的牌
#冒泡排序
def sortlist(dadatlist):
lenght = len(dadatlist)
for i in range(0,lenght-1):
for j in range(0,lenght-1-i):
if dadatlist[j]>dadatlist[j+1]:
dadatlist[j],dadatlist[j+1]=dadatlist[j+1],dadatlist[j]
return dadatlist
#单排
def danpai(p_cards,score):
p_cards=sortlist(p_cards)#把玩家手中的牌从小到大依次排列
#初始得分
score=0
wegiht_val=[0.1,1,10]
count=0
for card in p_cards:
x=card[1]*wegiht_val[count]
score=score+x
count+=1
print(f"计算单牌的得分是{score}")
return score
#对子
def dueizi(p_cards,score):
p_cards = sortlist(p_cards) # 把玩家手中的牌从小到大依次排列
card_val=[i[1] for i in p_cards]
print()
if len(set(card_val))==2:
if card_val[0]==card_val[1]: #aab形式的对子
score=(card_val[0]+card_val[1])*50+card_val[2]
else:
score=(card_val[1]+card_val[2])*50+card_val[0]
print(f"计算对子的得分是{score}")
return score
#顺子
def shunzi(p_cards,score):
p_cards=sortlist(p_cards)
card_val=[i[1] for i in p_cards]
a,s,d,=card_val
if s-a==1 and d-s==1:
score*=100
print(f"计算顺子的得分为{score}")
return score
#同花
def tonghua(p_cards,score):
color_vla=[i[0][0] for i in p_cards]
if len(set(color_vla))==1:
score*=1000
print(f"计算同花的得分为{score}")
return score
#同花顺
def tonghuashun(p_cards,score):
#先判断是不是同花
color_vla = [i[0][0] for i in p_cards]
if len(set(color_vla)) == 1:
#在判断是不是同花顺
p_cards = sortlist(p_cards)
card_val = [i[1] for i in p_cards]
a, s, d, = card_val
if s - a == 1 and d - s == 1:
score *= 10000
print(f"计算同花顺的得分为{score}")
return score
#豹子
def baozi(p_cards,score):
card_val=[i[1] for i in p_cards]
if len(set(card_val))==1:
score*=10000000000
print(f"计算豹子的得分为{score}")
return score
#比较大小
qwe=[
danpai,
dueizi,
shunzi,
tonghua,
tonghuashun,
baozi
]
playerscore=[]
for p_name,p_cards in playerDic.items():
print(f"开始计算玩家{p_name}的牌:{p_cards}")
score=0
for asd in qwe:
score=asd(p_cards,score)
playerscore.append([p_name,score])
print(playerscore)
#使float全部变为int
listc=[]
for i in playerscore:#因为大小中存在float;sored和冒泡排列都是针对int的;所以把他全部变为int(作业做要权重比例变大)
c=i[0]
a=i[-1]
b=int(a)
listc.append([b,c])#使用append只能在列表中使用;并且只能加入一个参(如果要加2个参,使用[]把参数全部写进[]里)
winner=sortlist(listc)[-1]#因为这时候给冒泡的全部是整数;所以冒泡可以处理了(冒泡只能处理int)
print(f"最后赢的玩家是:{winner[1]},得分为:{winner[0]}")
#winner=sortlist(playerscore)
#print(f"最后赢的玩家是:{winner[0]},得分为:{winner[1]}")