本文章在学习python中进行的练习小游戏,目的是为了让学习者熟悉python中的列表、字典、集合等数据操作。
游戏规则:
一付扑克牌,去掉大小王,每个玩家发3张牌,最后比大小,看谁赢。
有以下几种牌:
豹子:三张一样的牌,如3张6.
顺金:又称同花顺,即3张同样花色的顺子, 如红桃 5、6、7
顺子:又称拖拉机,花色不同,但是顺子,如红桃5、方片6、黑桃7,组成的顺子
对子:2张牌一样
单张:单张最大的是A
这几种牌的大小顺序为, 豹子>顺金>顺子>对子>单张
需程序实现的点:
先生成一付完整的扑克牌
给5个玩家随机发牌
统一开牌,比大小,输出赢家是谁
1.需要生成一副除去大小王的完整扑克牌
由于扑克牌中存在J、Q、K、A,为了方便进行大小比较,将对应的值存为11,12,13,14,这样玩家看到的值为JQKA,但实际后台运算时使用的是11,12,13,14。
2.根据玩家人数完成发牌
最少玩家为1名,最多玩家为14名,否则牌数不够。每个玩家分发三张牌
3.根据游戏规则比较玩家手中的牌面大小,牌面大的玩家获胜
此处采用权重的方式,将每种牌型赋予不同的权重,获得最后的牌数总点数进行比较,牌型及点数越大的总数和越大
如:
首先将玩家的三张牌根据点数大小升序排序,最大的点数10+中间的点数1+最小的点数*0.1,这样就能获得三张牌的基础分数score
然后判断玩家的牌型,当牌型为对子时,score = score * 100
当牌型为顺子时,score = score * 1000
…
以此类推,给不同的牌赋予不同的权重得到玩家最终的分数,最后根据分数排序,便能得到获胜的玩家
def generatePoker():
'''
:param :
:return: 生成完整地扑克牌
'''
color = ['♥','♠','♣','♦']
size = [[2,2], [3,3], [4,4], [5,5], [6,6], [7,7], [8,8], [9,9], [10,10], ['J',11], ['Q',12], ['K',13], ['A',14]]
poker = []
for poker_color in color:
for poker_size in size:
poker.append([f'{poker_color}{poker_size[0]}',poker_size[1]])
return poker
def distributePoker(poker, peopleList):
'''
:param poker: 一副不带王的完整地牌
:param num: 发牌人数
:return: 按照人数,每人发三张牌
'''
import random
p_cards = {}
if len(peopleList) > 14:
return '人数最多为14人'
if len(peopleList) < 1:
return '人数不符合规范'
for name in peopleList:
cards = random.sample(poker, 3)
p_cards[name] = cards
for Poker in cards:
poker.remove(Poker)
return p_cards
def calculationSingle(p_cards,score):
'''
计算单张牌点数大小
:param p_cards:
:param score:
:return:
'''
card_val = [card[1] for card in p_cards]
card_val.sort()
score += card_val[2] * 10 + card_val[1] * 1 + card_val[0] * 0.1
return score
def calculationDouble(p_cards, score):
'''
计算对子的点数大小
:param p_cards:
:param score:
:return:
'''
card_val = [card[1] for card in p_cards]
card_val.sort()
if len(set(card_val)) == 2:
score *= 100
return score
def calculationAlong(p_cards, score):
'''
计算顺子的点数大小
:param p_cards:
:param score:
:return:
'''
card_val = [card[1] for card in p_cards]
card_val.sort()
if card_val[0]+1 == card_val[1] and card_val[1]+1 == card_val[2]:
score *= 1000
return score
def calculationAlongSame(p_cards, score):
'''
计算同花顺的点数大小
:param p_cards:
:param score:
:return:
'''
color_val = [card[0][0] for card in p_cards]
if len(set(color_val)) == 1:
card_val = [card[1] for card in p_cards]
card_val.sort()
if card_val[0] + 1 == card_val[1] and card_val[1] + 1 == card_val[2]:
score *= 10000
return score
def calculationPanther(p_cards, score):
'''
计算豹子的点数大小
:param p_cards:
:param score:
:return:
'''
card_val = {card[1] for card in p_cards}
if len(card_val) == 1:
score *= 100000000
return score
def main(customer, calculation_poker_func=None):
'''
:param customer:
:param calculation_poker_func:
:return:
'''
if calculation_poker_func is None:
calculation_poker_func = [
calculationSingle,
calculationDouble,
calculationAlong,
calculationAlongSame,
calculationPanther
]
poker = generatePoker()
player_poker = distributePoker(poker=poker, peopleList=customer)
player_score = []
for name, cards in player_poker.items():
score = 0
for calc_func in calculation_poker_func:
score = calc_func(p_cards=cards, score=score)
player_score.append([name, score])
player_score.sort(key=lambda player_score: player_score[1])
winner = player_score[-1]
winner_poker = []
for poker in player_poker[winner[0]]:
winner_poker.append(poker[0])
print("玩家及所获取的牌如下:\n", player_poker)
print(f'恭喜玩家 {winner[0]} 获得胜利,他的牌是{winner_poker},他的分数为:{winner[1]}分')
if __name__ == '__main__':
''' '''
customer = ["王五","李三",'国王',"丞相","首辅"]
main(customer=customer)
运行结果如下: