用Python实现炸金花,叫小伙伴一起玩

您所在的位置:网站首页 腾讯有没有炸金花游戏 用Python实现炸金花,叫小伙伴一起玩

用Python实现炸金花,叫小伙伴一起玩

2024-04-18 13:48| 来源: 网络整理| 查看: 265

来源:步步年华

链接:用Python实现炸金花,叫小伙伴一起玩

有时候突然想和小伙伴玩炸金花了,但是又没有扑克牌,那我们今天就用Python来实现玩炸金花吧。

《诈金花》又叫三张牌,是在全国广泛流传的一种民间多人纸牌游戏。比如JJ比赛中的诈金花(赢三张),具有独特的比牌规则。

游戏过程中需要考验玩家的胆略和智慧。

游戏使用一副除去大小王的扑克牌,共4个花色52张牌。

1、豹子(AAA最大,222最小)。

2、同花顺(AKQ最大,A23最小)。

3、同花(AKQ最大,352最小)。

4、顺子(AKQ最大,A23最小)。

5、对子(AAK最大,223最小)。

6、单张(AKJ最大,352最小)。

玩“诈金花”可能牌小诈走牌大,是实力、勇气和智谋的较量,是冒险家的游戏。

豹子:三张点相同的牌,AAA、222。

同花顺:花色相同的顺子,黑桃456、红桃789。

金花:花色相同,非顺子,黑桃368,方片945。

顺子:花色不同的顺子,黑桃5红桃6方片7。

对子:两张点相同的牌,223,334。

散牌:三张牌不组成任何类型的牌。

特殊:花色不同的352(部分地区略有不同,不作详细介绍)。

不废话了直接上手玩:

道理很简单,p1到p5为五个小伙伴,后面是小伙伴的牌,每次从52张牌中随机获得,分配点数,通过点数比较大小,最后计算出谁是赢家。还能计算各种牌型出现的频率。

最后直接上源代码:

# -*- coding: utf-8 -*-"""function discreption:Created on Mon Oct 18 11:16:34 2021To: One is never too old to learn!@author: puyuanwei""" # 炸金花 from random import samplefrom 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=1)

喜欢的小伙伴赶紧去试试吧。



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3