zl程序教程

您现在的位置是:首页 >  后端

当前栏目

一小时就做好的python游戏实战:打牌小游戏,好玩归好玩,注意别玩上瘾了哟~~

Python游戏 实战 注意 小时 做好 小游戏 好玩
2023-09-27 14:27:49 时间

导语:

快点吧我等到花儿都谢了”这句话应该耳熟能详吧。嘿嘿,要不要小编用python帮你们提前复习一下呢?也不至于到时候没有心理准备。

​遇见你如果要花光我所有运气!那我请你离我远点,我过年可是要打牌的人。

在牌桌上,自己是自己的主任,自己的命运真正地掌握在自己手中,通过打牌,感到自己真实地存

在。

正文:python打牌小游戏

打牌人特质:常常站在高手后面点头微笑,学习自己想要的东西,说明善于学习别人的长处。

打牌人特质:手气差的时候洗个手,昂,运气就来了😏

🛒代码:

from random import sample
from collections import Counter

def get_pk_lst(pls, pks):  # 发牌
    result = []
    for p in pls:
        pk = sample(pks, 3)
        for _pk in pk:
            pks.remove(_pk)
        result.append({"name": p, "poker": pk})
    return result

def calculate(_score_map, pk_lst):  # 返回得分和牌型
    n_lst = list(map(lambda x: _score_map[x], pk_lst))  # 点数映射
    same_suit = len(set([pk[:2] for pk in pk_lst])) == 1  # 是否同花色
    continuity = sorted(n_lst) == [i for i in range(min(n_lst), max(n_lst) + 1)] or set(n_lst) == {14, 2, 3}  # 是否连续
    check = len(set(n_lst))  # 重复情况
    if not same_suit and not continuity and check == 3:
        return sum(n_lst), "单张"
    if not same_suit and check == 2:
        w = [i for i in n_lst if n_lst.count(i) == 2][0]
        single = [i for i in n_lst if i != w][0]
        return w*2*2 + single, "对子"
    if same_suit and not continuity:
        return sum(n_lst)*9, "金花"
    if continuity and not same_suit:
        return sum(n_lst)*81, "顺子"
    if check == 1:
        return sum(n_lst)*666, "豹子"
    if continuity and same_suit:
        return sum(n_lst)*999, "同花顺"

def compare(_score_map, pk_grp):  # 比大小
    for p in pk_grp:
        p["score"], p["type"] = calculate(_score_map, p["poker"])
    print("开牌结果------")
    for p in pk_grp:
        print(p)
    print("赢家是------")
    best = max(pk_grp, key=lambda x: x["score"])["name"]
    print(best)
    return pk_grp

def show(_score_map, _players):   # 开局
    pokers = list(_score_map.keys())
    poker_grp = get_pk_lst(_players, pokers)
    return compare(_score_map, poker_grp)

def start_game(_score_map, _players, freq=1):   # 游戏和统计
    type_lst = []
    for i in range(freq):
        grp = show(_score_map, _players)
        type_lst = type_lst + [t["type"] for t in grp]
    c = Counter(type_lst)
    print(c)
    total = sum(c.values())
    for item in c.items():
        print(f"{item[0]}频率:{item[1]/total:.2%}")

if __name__ == '__main__':
    # 准备扑克牌
    suit = ["黑桃", "红心", "方块", "梅花"]
    num = [str(i) for i in range(2, 11)] + ["J", "Q", "K", "A"]
    score_map = {}  # 单张点数映射表
    for s in suit:
        count = 2
        for n in num:
            score_map[f"{s}{n}"] = count
            count += 1
    # 5个玩家入场
    players = [f"p{i}" for i in range(1, 6)]
    # 开始游戏
    start_game(score_map, players, freq=100000) ​​
复制代码

结尾:

【打牌要诀】:打牌已经成为大家所喜爱的娱乐项目,给大家总结了一点小的要诀,祝大家多赢钱哈!

好啦文章就分享到这里就结束啦
给大家分享了一份全套的 Python 学习资料,给那些想学习 Python 的小伙伴们!

一、Python所有方向的学习路线
Python所有方向路线就是把Python常用的技术点做整理,形成各个领域的知识点汇总,它的用处就在于,你可以按照上面的知识点去找对应的学习资源,保证自己学得较为全面。

二、学习软件
工欲善其事必先利其器。学习Python常用的开发软件都在这里了,给大家节省了很多时间。

三、全套PDF电子书
书籍的好处就在于权威和体系健全,刚开始学习的时候你可以只看视频或者听某个人讲课,但等你学完之后,你觉得你掌握了,这时候建议还是得去看一下书籍,看权威技术书籍也是每个程序员必经之路。

四、入门学习视频
我们在看视频学习的时候,不能光动眼动脑不动手,比较科学的学习方法是在理解之后运用它们,这时候练手项目就很适合了。

四、实战案例
光学理论是没用的,要学会跟着一起敲,要动手实操,才能将自己的所学运用到实际当中去,这时候可以搞点实战案例来学习。

五、面试资料
我们学习Python必然是为了找到高薪的工作,下面这些面试题是来自阿里、腾讯、字节等一线互联网大厂最新的面试资料,并且有阿里大佬给出了权威的解答,刷完这一套面试资料相信大家都能找到满意的工作。


这份完整版的Python全套学习资料已经上传CSDN,朋友们如果需要可以微信扫描下方CSDN官方认证二维码免费领取【保证100%免费】