以下是一种可能的生成代码:

import random

nums = [401, 402, 403, 404, 405, 406, 407, 408, 409, 410, 411, 412, 413,
        301, 302, 303, 304, 305, 306, 307, 308, 309, 310, 311, 312, 313,
        201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213,
        101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113]

def generate_hand():
    hand = []
    colors = []
    while len(hand) < 5:
        num = random.choice(nums)
        if num in hand:
            continue
        color = num // 100
        if color in colors:
            continue
        colors.append(color)
        hand.append(num)
        if len(hand) >= 2 and abs(hand[-1] - hand[-2]) != 1:
            colors = []
            hand = []
    return hand

print(generate_hand())

这个代码中,我们首先定义了一个包含所有扑克牌数字的列表 nums。然后我们定义了一个函数 generate_hand,该函数用于生成一手牌。我们在函数中定义了一个空列表 hand 和一个空列表 colors,分别用于存储当前已经选出的牌和已经选出的颜色。然后我们进入一个循环,直到 hand 中有 5 张牌为止。在循环中,我们从 nums 中随机选出一个数字 num,如果 num 已经在 hand 中,则跳过本次循环;否则,我们提取 num 的百位作为颜色,并将其存入 colors 中。如果 colors 中已经有了这个颜色,则跳过本次循环;否则,我们将 num 存入 hand 中。如果此时 hand 中有至少 2 张牌,并且最后两张牌的差不为 1,则说明这不是一手连续的牌,我们需要重新开始,清空 colorshand。最终,我们返回生成的一手牌。

最后,我们在主程序中调用 generate_hand 函数,并输出结果。注意,由于随机性质,每次运行结果可能不同。

Python 随机生成扑克牌手牌:连续且至少两种颜色

原文地址: https://www.cveoy.top/t/topic/ncIv 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录