基于python设计一款棋牌游戏,其更具传统5十K为原型,通过随机发牌,用户坐庄出牌完成四人之间的游戏,但相对于传统5十K不同的是,这里没有对家只有上游,也无分数最高者,先出牌者胜,具体详情请看文章:
import random
face_size=('3','4','5','6','7','8','9','10','J','Q','K','A','2') #每张牌由小到大的排序规则确定牌面值的大小
def shuffle(hand_card): #洗牌
for role in hand_card.keys():
hand_card[role]=[] #每位玩家的牌置空
face={'A':4,'2':4,'3':4,'4':4,'5':4,'6':4,'7':4,'8':4,'9':4,'10':4,'J':4,'Q':4,'K':4} #牌的类型及其数量
face_choice=('A','2','3','4','5','6','7','8','9','10','J','Q','K')
card=[] #牌的排列顺序
for times in range(52):
sign=1
while(sign):
choice=random.randint(0,12)
if face[face_choice[choice]]!=0:
card.append(face_choice[choice])
face[face_choice[choice]]-=1
sign=0
return card
def send_card(card,hand_card,position): #把card按顺序发个四位玩家
for times in range(0,52,4): #52张牌每人13张
hand_card[position[0]].append(card[times])
times+=1
hand_card[position[1]].append(card[times])
times+=1
hand_card[position[2]].append(card[times])
times+=1
hand_card[position[3]].append(card[times])
def round_play(last_card,role,hand_card,banker): #role玩家回合
new_card=show_card(role,hand_card) #玩家出牌
if new_card=='pass':
if last_card[0]==-1:
print('您是庄家,不能过牌')
return round_play(last_card,role,hand_card,banker) #出牌不和规则,重新出牌
else:
role_card=(last_card,banker)
return role_card
else:
if last_card[0]==-1: #先手回合
for times in range(new_card[1]): #更新手牌
hand_card[role].remove(face_size[new_card[0]])
role_card=(new_card,role) #新牌更新为庄家牌和庄家
return role_card #返回庄家和庄家牌
else:
if new_card[1]<4 and last_card[1]!=new_card[1]:
print("您出的牌不能战胜对手,请您确认后重新决定出牌")
return round_play(last_card,role,hand_card,banker) #出牌不和规则,重新出牌
else:
if new_card[1]==4 and last_card[1]!=4: #炸弹大于所有的非炸弹
for times in range(new_card[1]): #更新手牌
hand_card[role].remove(face_size[new_card[0]])
role_card=(new_card,role) #新牌更新为庄家牌和庄家
return role_card #返回庄家和庄家牌
else:
if new_card[0]>last_card[0]: #新出的牌数值比对手的牌大
for times in range(new_card[1]): #更新手牌
hand_card[role].remove(face_size[new_card[0]])
role_card=(new_card,role) #新牌更新为庄家牌和庄家
return role_card #返回庄家和庄家牌
else:
print("您出的牌不能战胜对手,请您确认后重新决定出牌")
return round_play(last_card,role,hand_card,banker) #出牌不和规则,重新出牌
def show_card(role,hand_card): #玩家出牌
print(' '+role+" 玩家您好,您的手牌为:")
print(hand_card[role])
choice=input('请选择您要出的牌,选择您手牌中没有的单只将自动确认过牌:')
if '10' in choice: #两个字符的牌,单独考虑
if choice.count('10')==1: #单只
if choice in hand_card[role]:
choice=(face_size.index(choice),1)
return choice
else:
print("您已确认过牌")
return 'pass'
elif choice.count('10')==2: #对子
if hand_card[role].count(choice[0:2])>=2:
choice=(face_size.index(choice[0:2]),2)
return choice
else:
print("按规则重新出牌,单只、对子、三张、炸弹")
return show_card(role,hand_card)
elif choice.count('10')==3: #三张
if hand_card[role].count(choice[0:2])>=3:
choice=(face_size.index(choice[0:2]),3)
return choice
else:
print("按规则重新出牌,单只、对子、三张、炸弹")
return show_card(role,hand_card)
elif choice.count('10')==4: #炸弹
if hand_card[role].count(choice[0:2])==4:
choice=(face_size.index(choice[0:2]),4)
return choice
else:
print("按规则重新出牌,单只、对子、三张、炸弹")
return show_card(role,hand_card)
else:
print("按规则重新出牌,单只、对子、三张、炸弹")
return show_card(role,hand_card)
else:
if len(choice)==1: #单只
if choice in hand_card[role]:
choice=(face_size.index(choice),1)
return choice
else:
print("您已确认过牌")
return 'pass'
elif len(choice)==2: #对子
if choice[0]==choice[1] and hand_card[role].count(choice[0])>=2:
choice=(face_size.index(choice[0]),2)
return choice
else:
print("按规则重新出牌,单只、对子、三张、炸弹")
return show_card(role,hand_card)
elif len(choice)==3: #三张
if choice[0]==choice[1] and choice[0]==choice[2] and hand_card[role].count(choice[0])>=3:
choice=(face_size.index(choice[0]),3)
return choice
else:
print("按规则重新出牌,单只、对子、三张、炸弹")
return show_card(role,hand_card)
elif len(choice)==4: #炸弹
if choice[0]==choice[1] and choice[0]==choice[2] and choice[0]==choice[3] and hand_card[role].count(choice[0])==4:
choice=(face_size.index(choice[0]),4)
return choice
else:
print("按规则重新出牌,单只、对子、三张、炸弹")
return show_card(role,hand_card)
else:
print("按规则重新出牌,单只、对子、三张、炸弹")
return show_card(role,hand_card)
def frist(hand_card,role): #具有牌权的出牌
last_card=(-1,-1)
return round_play(last_card,role,hand_card,role)
def play(hand_card,position): #游戏开始,轮流出牌
banker_card=frist(hand_card,position[3]) #先手出牌玩家回合
last_card=banker_card[0]
banker=banker_card[1]
print("----------------------")
print("庄家:"+banker)
print("庄家牌:"+face_size[last_card[0]]*last_card[1])
print("----------------------")
sign=1
while sign: #当四个玩家中有一位玩家手牌为0时,游戏结束,该玩家获胜
for role in position:
if len(hand_card['A'])==0 or len(hand_card['B'])==0 or len(hand_card['C'])==0 or len(hand_card['D'])==0:
sign=0
break
if role==banker:
print("**********************")
print("玩家 "+banker+" 庄家继续坐庄,先手出牌")
print("**********************")
banker_card=frist(hand_card,role)
last_card=banker_card[0]
banker=banker_card[1]
print("----------------------")
print("庄家:"+banker)
print("庄家牌:"+face_size[last_card[0]]*last_card[1])
print("----------------------")
else:
banker_card=round_play(last_card,role,hand_card,banker) #role玩家回合
last_card=banker_card[0]
banker=banker_card[1]
print("----------------------")
print("庄家:"+banker)
print("庄家牌:"+face_size[last_card[0]]*last_card[1])
print("----------------------")
for role in hand_card:
if len(hand_card[role])==0:
print("**********************")
print("玩家: "+role+" WIN")
print("**********************")
break
def get_positino(): #玩家落座
position=input('游戏即将开始,请玩家落座(输入ABCD的位置,如ADBC):')
if position.count('A')!=1 or position.count('B')!=1 or position.count('C')!=1 or position.count('D')!=1:
return get_positino()
return tuple(position)
def main():
hand_card={'A':[],'B':[],'C':[],'D':[]} #A,B,C,D四个玩家持有的手牌
position=get_positino() #玩家落座
print('您的落座信息:'+str(position))
card=shuffle(hand_card) #洗牌
send_card(card,hand_card,position) #发牌
play(hand_card,position)
main()