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) 

结尾

【打牌要诀】:打牌已经成为大家所喜爱的娱乐项目,给大家总结了一点小的要诀,祝大家多赢哈!
在这里插入图片描述
好啦文章就分享到这里就结束啦~

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值