本文最后更新于 2025-12-17,文章内容可能已经过时。

zjh游戏

# -*- coding = utf-8 -*-
# Author:YuanBai
# 作者:元白
# @Time : 2022/6/6 0004 22:15
# @USER : Administrator
# @File : OTATO-zjh.py
# @Software : PyCharm

from random import sample
from collections import Counter

#生成一付完整的扑克牌
def creat_poker_dic()->dict:
    # 准备扑克牌
    suit = ["♠", "♥", "♦", "♣"]
    num = []
    for i in range(2, 11):
        num.append(str(i))
    num += ["J", "Q", "K", "A"]
    poker_lst = {}  # 牌库
    for j in suit:
        count = 2
        for n in num:
            poker_lst[f"{j}{n}"] = count
            count += 1
    return poker_lst



#发给可选人数,每人3张牌,每个人的牌不会和自己及其他人重复。
def deal_card(pls:list,pks:list)->list:
    result = []
    for p in pls:
        pk = sample(pks, 3)
        #从牌库中移除已经发了的牌
        for _pk in pk:
            pks.remove(_pk)
        result.append({"user": p, "poker": pk})
    return result



def calculate(_poker_lst:dict, pk_lst:list)->tuple:  # 返回得分和牌型
    n_lst = list(map(lambda x: _poker_lst[x], pk_lst))  # 点数映射
    same_suit = len(set([pk[:1] 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))  # 重复情况
    sort = sorted(n_lst.copy())
    middle = sort[1]
    max_pk = max(sort)
    min_pk = min(sort)

    # 三张一样
    if check == 1:
        return n_lst[0]+5200, "豹子"
    #重复2张(21)
    elif 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*14 + single +1000, "对子"
    #同花顺()
    elif continuity and same_suit:
        if '14' in n_lst and '2' in n_lst:
            return 6*30+4000,"同花顺"
        else:
            return sum(n_lst) * 30 + 4000,"同花顺"
    #同花、不顺
    elif same_suit and not continuity:
        return max_pk * 15 + middle * 2+ min_pk + 3200, "同花"
    #不同花、顺子()
    elif continuity and not same_suit:
        if '14' in n_lst and '2' in n_lst:
            return 6*30+2000,"顺子"
        else:
            return sum(n_lst)*30+2000, "顺子"
    # 不同花、不顺、不重复()
    elif not same_suit and not continuity and check == 3:
        return max_pk * 15 + middle * 2 + min_pk, "单张"
    else:
        return 0,"?"



def check_max_type(_poker_lst:dict, pk_grp:list)->list:  # 比大小
    for p in pk_grp:
        p["score"], p["type"] = calculate(_poker_lst, p["poker"])
    for p in pk_grp:
        #pass
        print("玩家:{},手牌:{},牌型:{}".format(p['user'],p['poker'],p['type']))
    best = max(pk_grp, key=lambda x: x["score"])["user"]+','+max(pk_grp, key=lambda x: x["score"])['type']+"最大"
    print("赢家是:玩家{}".format(best))
    print("------"*8)
    return pk_grp

def timeshow(func):
    from time import time
    def newfunc(*arg, **kw):
        t1 = time()
        res = func(*arg, **kw)
        t2 = time()
        funtime = t2-t1
        print(f"{func.__name__}:{t2 - t1:.6f} sec")
        return res

    return newfunc

#开局
def begin(_poker_lst:dict, _players:list)->list:   # 开局
    #牌表
    pokers = list(_poker_lst.keys())
    #发牌情况表
    poker_grp = deal_card(_players, pokers)
    #展示发牌情况及赢家
    return check_max_type(_poker_lst, poker_grp)


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

@timeshow
def main():
    # 设定游戏人数
    players = [f"p{i}" for i in range(1, 6)]

    # 生成一副扑克
    poker_lst = creat_poker_dic()
    # 按照游戏人数发牌
    start_game(poker_lst, players, freq=10)

if __name__ == '__main__':
    main()

执行结果

玩家:p1,手牌:['♠10', '♦K', '♣2'],牌型:单张
玩家:p2,手牌:['♥3', '♦9', '♣4'],牌型:单张
玩家:p3,手牌:['♦Q', '♥K', '♣J'],牌型:顺子
玩家:p4,手牌:['♦5', '♠6', '♣A'],牌型:单张
玩家:p5,手牌:['♥A', '♣3', '♠5'],牌型:单张
赢家是:玩家p3,顺子最大
------------------------------------------------
玩家:p1,手牌:['♣8', '♠8', '♣7'],牌型:对子
玩家:p2,手牌:['♦A', '♦5', '♦K'],牌型:同花
玩家:p3,手牌:['♥4', '♠K', '♣K'],牌型:对子
玩家:p4,手牌:['♥6', '♦7', '♠Q'],牌型:单张
玩家:p5,手牌:['♦9', '♣5', '♣2'],牌型:单张
赢家是:玩家p2,同花最大
------------------------------------------------
玩家:p1,手牌:['♥3', '♣4', '♠2'],牌型:顺子
玩家:p2,手牌:['♦Q', '♠3', '♥7'],牌型:单张
玩家:p3,手牌:['♦2', '♣J', '♥A'],牌型:单张
玩家:p4,手牌:['♠K', '♣Q', '♥10'],牌型:单张
玩家:p5,手牌:['♦9', '♠9', '♠A'],牌型:对子
赢家是:玩家p1,顺子最大
------------------------------------------------
玩家:p1,手牌:['♦3', '♣9', '♣2'],牌型:单张
玩家:p2,手牌:['♥6', '♠2', '♥2'],牌型:对子
玩家:p3,手牌:['♠3', '♦2', '♠5'],牌型:单张
玩家:p4,手牌:['♣7', '♦A', '♠8'],牌型:单张
玩家:p5,手牌:['♥7', '♥9', '♥K'],牌型:同花
赢家是:玩家p5,同花最大
------------------------------------------------
玩家:p1,手牌:['♦2', '♣4', '♣6'],牌型:单张
玩家:p2,手牌:['♥8', '♦3', '♣2'],牌型:单张
玩家:p3,手牌:['♣9', '♠5', '♣7'],牌型:单张
玩家:p4,手牌:['♥K', '♠10', '♦Q'],牌型:单张
玩家:p5,手牌:['♦7', '♥7', '♥3'],牌型:对子
赢家是:玩家p5,对子最大
------------------------------------------------
玩家:p1,手牌:['♣6', '♠4', '♠6'],牌型:对子
玩家:p2,手牌:['♠8', '♠10', '♠A'],牌型:同花
玩家:p3,手牌:['♥9', '♠Q', '♥4'],牌型:单张
玩家:p4,手牌:['♥2', '♥6', '♥K'],牌型:同花
玩家:p5,手牌:['♣A', '♦7', '♥8'],牌型:单张
赢家是:玩家p2,同花最大
------------------------------------------------
玩家:p1,手牌:['♦10', '♦9', '♠J'],牌型:顺子
玩家:p2,手牌:['♥10', '♣4', '♥J'],牌型:单张
玩家:p3,手牌:['♥3', '♦5', '♦7'],牌型:单张
玩家:p4,手牌:['♣9', '♠A', '♦3'],牌型:单张
玩家:p5,手牌:['♦6', '♣J', '♣3'],牌型:单张
赢家是:玩家p1,顺子最大
------------------------------------------------
玩家:p1,手牌:['♠K', '♥3', '♥6'],牌型:单张
玩家:p2,手牌:['♥7', '♠9', '♦A'],牌型:单张
玩家:p3,手牌:['♥J', '♣6', '♠7'],牌型:单张
玩家:p4,手牌:['♣A', '♦8', '♠4'],牌型:单张
玩家:p5,手牌:['♦K', '♠8', '♦6'],牌型:单张
赢家是:玩家p2,单张最大
------------------------------------------------
玩家:p1,手牌:['♦3', '♥A', '♥3'],牌型:对子
玩家:p2,手牌:['♥Q', '♠6', '♥2'],牌型:单张
玩家:p3,手牌:['♠2', '♦6', '♠8'],牌型:单张
玩家:p4,手牌:['♦J', '♥8', '♦K'],牌型:单张
玩家:p5,手牌:['♦2', '♦5', '♦10'],牌型:同花
赢家是:玩家p5,同花最大
------------------------------------------------
玩家:p1,手牌:['♣5', '♣K', '♣3'],牌型:同花
玩家:p2,手牌:['♦J', '♣J', '♦10'],牌型:对子
玩家:p3,手牌:['♣4', '♠J', '♥3'],牌型:单张
玩家:p4,手牌:['♠2', '♣A', '♠3'],牌型:顺子
玩家:p5,手牌:['♦K', '♦A', '♦Q'],牌型:同花顺
赢家是:玩家p5,同花顺最大
------------------------------------------------

Counter({'单张': 31, '对子': 8, '同花': 6, '顺子': 4, '同花顺': 1})
单张频率:62.00%
顺子频率:8.00%
对子频率:16.00%
同花频率:12.00%
同花顺频率:2.00%
main:0.005779 sec