炸金花小游戏
-要求: 编写炸金花游戏程序
自己写一个程序,实现发牌、比大小判断输赢。
游戏规则:
一付扑克牌,去掉大小王,每个玩家发3张牌,最后比大小,看谁赢。 有以下几种牌: 豹子:三张一样的牌,如3张6. 顺金:又称同花顺,即3张同样花色的顺子, 如红桃 5、6、7 顺子:又称拖拉机,花色不同,但是顺子,如红桃5、方片6、黑桃7,组成的顺子 对子:2张牌一样 单张:单张最大的是A 这几种牌的大小顺序为, 豹子>顺金>顺子>对子>单张 需程序实现的点:
1. 先生成一付完整的扑克牌
2. 给5个玩家随机发牌
3. 统一开牌,比大小,输出赢家是谁
本人是python小白,有不对地方多多指正
整体代码
import random
'''具体比较函数'''
def calculate(player_poker_values,pokers_map):
score_list=list(map(lambda x:pokers_map[x],player_poker_values))
same_colors=len(set([color[:2] for color in player_poker_values]))==1
continutiy=sorted(score_list)==[num for num in range(min(score_list),max(score_list))] or set(score_list)==[14,2,3]
repeat=len(set(score_list))
if not continutiy and not same_colors and repeat==3:
return sum(score_list),'单张'
elif not continutiy and not same_colors and repeat==2:
return sum(score_list)*2,'对子'
elif not same_colors and repeat==3 and continutiy:
return sum(score_list)*3,'顺子'
elif not continutiy and repeat==3 and same_colors:
return sum(score_list)*4,'金花'
elif repeat==3 and same_colors and continutiy:
return sum(score_list)*5,'同花顺'
elif repeat==1 and not same_colors and not continutiy:
return sum(score_list)*6,'飞机'
'''将每个人的牌型进行展示并比较'''
def show_and_compare(play_poker_list,pokers_map):
for player in play_poker_list:
player['总分值'], player['牌型'] = calculate(player['手牌'], pokers_map)
print(player)
winner=max(play_poker_list,key=lambda x:x['总分值'])
print('-'*80)
print('大赢家',winner['Poker'])
'''
获取每个抓的牌返回列表
'''
def poker_grp(players, pokers):
players_poker_list = []
for player in players:
pks = random.sample(pokers, 3)
for pk in pks:
pokers.remove(pk)
players_poker_list.append({'Poker':player,'手牌':pks})
return players_poker_list
'''
开始游戏
'''
def start_game(players,pokers_map):
pokers=list(pokers_map.keys())
player_poker_list=poker_grp(players,pokers)
show_and_compare(player_poker_list,pokers_map)
if __name__ == '__main__':
colors=["黑桃", "红心", "方块", "梅花"]
num=[str(i) for i in range(2,11)]+['J','Q','K','A']
pokers_map={}
for i in colors:
count=2
for j in num:
pokers_map[i+j]=count
count+=1
players=[f'Player{i}' for i in range(1,6)]
start_game(players,pokers_map)
|