diff --git a/z.py b/z.py new file mode 100644 index 0000000..5350cd3 --- /dev/null +++ b/z.py @@ -0,0 +1,53 @@ +import itertools +import random + +# 创建牌组 +suits = ['黑桃', '红桃', '梅花', '方块'] +ranks = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A'] +deck = list(itertools.product(suits, ranks)) + +# 定义检测牌型的函数 +def check(hand): + # 检测豹子 + if hand[0][1] == hand[1][1] == hand[2][1]: + return '豹子' + # 检测顺金 + sorted_hand = sorted(hand, key=lambda x: ranks.index(x[1])) + if all(sorted_hand[i][0] == sorted_hand[0][0] for i in range(3)): + if ranks.index(sorted_hand[2][1]) - ranks.index(sorted_hand[0][1]) == 2: + return '顺金' + # 检测金花 + if all(hand[i][0] == hand[0][0] for i in range(3)): + return '金花' + # 检测顺子 + if ranks.index(sorted_hand[2][1]) - ranks.index(sorted_hand[0][1]) == 2: + return '顺子' + # 检测对子 + if hand[0][1] == hand[1][1] or hand[1][1] == hand[2][1] or hand[0][1] == hand[2][1]: + return '对子' + # 检测特殊牌型235 + if sorted([card[1] for card in hand]) == ['2', '3', '5']: + return '特殊' + # 如果以上都不是,则为单张 + return '单张' + +# 模拟游戏 +def game(deck): + random.shuffle(deck) + player_hand = random.sample(deck, 3) + return check(player_hand) + +# 主函数 +def main(): + # 初始化牌型计数器 + types = {'豹子': 0, '顺金': 0, '金花': 0, '顺子': 0, '对子': 0, '单张': 0, '特殊': 0} + # 模拟10000次游戏 + for _ in range(10000): + type = game(deck) + types[type] += 1 + # 计算并打印概率 + for type, count in types.items(): + print(f"{type}: {count / 10000:.4f}") + +if __name__ == "__main__": + main()