From ea48e401a93ffbf1607f4a0fd5efcdbbf5ae2385 Mon Sep 17 00:00:00 2001 From: plhp7qn34 <22643952@qq.com> Date: Sat, 1 Jun 2024 16:20:02 +0800 Subject: [PATCH] ADD file via upload --- 扑克牌.py | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 扑克牌.py diff --git a/扑克牌.py b/扑克牌.py new file mode 100644 index 0000000..334be5d --- /dev/null +++ b/扑克牌.py @@ -0,0 +1,85 @@ +import random +from collections import Counter + +# 初始化扑克牌,生成52张牌,不包括大小王 +suits = ['H', 'D', 'S', 'C'] # Hearts, Diamonds, Spades, Clubs +values = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A'] +deck = [v + s for s in suits for v in values] + + +# 牌型判断函数 +def is_bomb(cards): + # 判断是否为豹子(炸弹),三张点相同的牌 + return cards[0][:-1] == cards[1][:-1] == cards[2][:-1] + + +def is_straight_flush(cards): + # 判断是否为顺金(同花顺),花色相同的顺子 + return is_flush(cards) and is_straight(cards) + + +def is_flush(cards): + # 判断是否为金花(色皮),花色相同,非顺子 + return cards[0][-1] == cards[1][-1] == cards[2][-1] + + +def is_straight(cards): + # 判断是否为顺子(拖拉机),花色不同的顺子 + values = sorted(['23456789TJQKA'.index(c[:-1]) if c[:-1] != '10' else 8 for c in cards]) + return values == list(range(values[0], values[0] + 3)) + + +def is_pair(cards): + # 判断是否为对子,两张点数相同的牌 + values = [c[:-1] for c in cards] + return len(set(values)) == 2 + + +def is_special(cards): + # 判断是否为特殊牌型,花色不同的235 + values = sorted(['23456789TJQKA'.index(c[:-1]) if c[:-1] != '10' else 8 for c in cards]) + suits = set(c[-1] for c in cards) + return values == [0, 1, 2] and len(suits) == 3 + + +def get_hand_type(cards): + # 获取牌型的类型 + if is_special(cards): + return '特殊' + if is_bomb(cards): + return '豹子' + if is_straight_flush(cards): + return '顺金' + if is_flush(cards): + return '金花' + if is_straight(cards): + return '顺子' + if is_pair(cards): + return '对子' + return '单张' # 三张牌不组成任何类型的牌,单张 + + +# 模拟游戏并统计结果 +results = Counter() + +# 模拟10000次游戏 +for _ in range(10000): + # 洗牌 + random.shuffle(deck) + # 发牌,每位玩家3张牌 + player1 = deck[:3] + player2 = deck[3:6] + + # 统计每位玩家的牌型 + results[get_hand_type(player1)] += 1 + results[get_hand_type(player2)] += 1 + +# 计算概率 +total_hands = 20000 # 两个玩家,每人10000次 +probabilities = {hand: count / total_hands for hand, count in results.items()} + +# 输出结果 +for hand, prob in probabilities.items(): + print(f"{hand}: {prob:.4f}") + +