【问题标题】:How to prevent repeated random values?如何防止重复的随机值?
【发布时间】:2017-12-25 04:11:30
【问题描述】:

CPU 和用户得到重复卡。我使用了 shuffle 功能,以及 pop。有没有办法防止用户和 CPU 获得重复卡。

这里是编译的程序示例

这是你的卡片: 1) 俱乐部 10 2)钻石4 3)钻石6 4) 俱乐部 7 5) 俱乐部 10 要玩牌,只需一次输入他们的第一名。完成后,输入空白 2 1 3 4

你玩过: 钻石之四 俱乐部 10 钻石6 俱乐部 7 人

CPU 播放: 黑桃杰克 黑桃J

如您所见,提示用户重复“随机”牌,CPU 播放重复的“随机”牌。

        import random
        import math
        print("Gui-less poker sucks, but it sure is addicting probably")
        if 1:
            hcardss = [""]
            hcardsc = [""]
            hcardsh = [""]
            ccardss = [""]
            ccardsc = [""]
            ccardsh = [""]
            ingame = "true"
            while (ingame == "true"):
                undone = 5
                while (undone > 0):
                    card = random.randint(1,52)
                    # print(card)
                    temp = card / 13
                    temp2 = card / 4
                    temp = math.floor(temp)
                    temp2 = math.floor(temp2)
                    temp = temp + 1
                    # temp2 = temp2 + 1
                    #print(temp)
                    #print(temp2)
                    # undone -= 1
                    hcardss.append(temp)
                    hcardsc.append(temp2)
                    if (temp == 1):
                        temp3 = " of Spades"
                    elif (temp == 2):
                        temp3 = " of Diamonds"
                    elif (temp == 3):
                        temp3 = " of Clubs"
                    else:
                        temp3 = " of Hearts"
                    if (temp2 == 10):
                        temp4 = "Jack"
                    elif (temp2 == 11):
                        temp4 = "Queen"
                    elif (temp2 == 12):
                        temp4 = "King"
                    elif (temp2 == 13):
                        temp4 = "Ace"
                    else:
                        temp4 = str(temp2 + 1)
                    # print("Your card was the " + temp4 + temp3)
                    hcardsh.append("The " + temp4 + temp3)
                    undone -= 1
                undone = 5
                while (undone > 0):
                    # THIS ONE IS THE COMPUTER
                    card = random.randint(1,52)
                    # print(card)
                    temp = card / 13
                    temp2 = card / 4
                    temp = math.floor(temp)
                    temp2 = math.floor(temp2)
                    temp = temp + 1
                    # temp2 = temp2 + 1
                    #print(temp)
                    #print(temp2)
                    # undone -= 1
                    ccardss.append(temp)
                    ccardsc.append(temp2)
                    if (temp == 1):
                        temp3 = " of Spades"
                    elif (temp == 2):
                        temp3 = " of Diamonds"
                    elif (temp == 3):
                        temp3 = " of Clubs"
                    else:
                        temp3 = " of Hearts"
                    if (temp2 == 10):
                        temp4 = "Jack"
                    elif (temp2 == 11):
                        temp4 = "Queen"
                    elif (temp2 == 12):
                        temp4 = "King"
                    elif (temp2 == 13):
                        temp4 = "Ace"
                        temp4 = str(temp2 + 1)
                    # print("Your card was the " + temp4 + temp3)
                    ccardsh.append("The " + temp4 + temp3)
                    undone -= 1
                print()
                print()
                print()
                print("Here are your cards:")
                print("1) " + hcardsh[1])
                print("2) " + hcardsh[2])
                print("3) " + hcardsh[3])
                print("4) " + hcardsh[4])
                print("5) " + hcardsh[5])
                print("To play cards, simply type their number one at a time. When done, input blank")
                doneinput = "false"
                hplay = [""]
                while (doneinput == "false"):
                    latestinput = input("> ")
                    if (latestinput == ""):
                        doneinput = "true"
                    else:
                        if (int(latestinput) in hplay):
                            print("You already picked that one!")
                        else:
                            hplay.append(int(latestinput))
                # print("The cards you played are " + str(hplay))
                doneinput = "false"
                cplay = [""]
                while (doneinput == "false"):
                    latestinput = random.randint(1,5)
                    if (latestinput == ""):
                        doneinput = "true"
                    else:
                        if (int(latestinput) in cplay):
                            doneinput = "true"
                        else:
                            cplay.append(int(latestinput))
                #print("So you played " + str(hplay))
                #print("And the cpu played " + str(cplay))
                #print("So you played the " + hcardsh[hplay[1]] + hcardsh[hplay[2]]
                times = len(hplay)
                # times = times - 1
                hplayh = [""]
                cplayh = [""]
                sub = 1
                print()
                print()
                print("You played:")
                while (sub < times):
                    hplayh.append(hcardsh[hplay[sub]])
                    print(hcardsh[hplay[sub]])
                    sub += 1
                sub = 1
                times = len(cplay)
                print()
                print()
                print("CPU played:")
                while (sub < times):
                    cplayh.append(ccardsh[cplay[sub]])
                    print(ccardsh[cplay[sub]])
                    sub += 1
                #print(str(hplayh))
                #print(str(cplayh))


                ingame = "false"

【问题讨论】:

  • 为了使用random.shuffle(),您应该生成一个包含 52 张卡片的列表,然后对该列表应用洗牌功能。然后您使用.pop() 函数挑选卡片。

标签: python random shuffle poker playing-cards


【解决方案1】:

根本不要使用 randint() 来挑选卡片。构建一个“deck”,它只是一个卡片列表,使用 shuffle() 将其随机化,并使用 pop() 处理一张卡片。另外,我建议用数字而不是字符串来表示卡片。弦乐是给人类的。数字将使您的其余代码更简单、更快。只需在需要时为用户转换为字符串。

类似:

theDeck = [];

def shuffle():
    theDeck = range(52)
    random.shuffle(theDeck)

def dealCard():
    return theDeck.pop()

def nameOfCard(c):
    return [ "Deuce", "Trey", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King" ][c >> 2] + \
        " of " + [ "Clubs", "Diamonds", "Hearts", "Spades" ][c & 3];

【讨论】:

    【解决方案2】:

    将打出的牌添加到列表中。检查新的随机卡是否已经在此列表中,如果是则重新生成新卡,直到它不在列表中。不要忘记检查列表中的所有卡片以避免无限循环。 附言不要使用这种方法,一次生成所有列表并打乱它更有效。

    【讨论】:

    • 效率极低。更好地打乱列表并从中进行选择。
    【解决方案3】:

    您必须保留所有已在游戏中的牌的列表,然后检查最近生成的牌是否在该列表中,如果是,则重新生成它。

    generated_cards = []
    ...
    card = random.randint(1,52)
    while card in generated_cards:
        card = random.randint(1,52)
    generated_cards.append(card)  # once the card is a new, unique one,
                                  # add it to the generated_cards list 
    

    一种更简单的处理方法可能是生成卡片列表,将它们洗牌,然后一张一张地从列表中弹出。

    deck_of_cards = [x for x in range(0, 52)]
    random.shuffle(deck_of_cards)
    ...
    card = deck_of_cards.pop()  # this removes the item from the list 
                                # and puts it in the card variable
    

    【讨论】:

    • 第一种方法效率极低。从打乱列表中挑选更简单、更快捷。
    • 我知道这可能是一个无限长的操作,但它是对所提问题的答案。处理所有这些的正确方法是 OOP,但在我看来,这似乎超出了问题的范围。
    • 我尽量不要对 OP 的确切问题过于直白。毕竟,如果他在寻求帮助,我为什么会认为他确切地知道要问什么问题呢?我更倾向于试图弄清楚他想要解决什么问题。在您的回答中,您都做到了。此外,您的想法还有一个更好的版本,称为 Floyd 算法,它是 O(log n) 并且保证有限。
    • 这很有意义。感谢您的(暗示)建议,仍然让我作为问题回答者得到支持! This弗洛伊德算法?我知道你也可以只增加 randint % 52 直到你得到一张独特的卡片,但是 O(log n) 的解决方案听起来真的很有趣。
    猜你喜欢
    • 1970-01-01
    • 2021-04-22
    • 1970-01-01
    • 1970-01-01
    • 2017-05-20
    • 2021-11-03
    • 1970-01-01
    • 2014-01-01
    • 1970-01-01
    相关资源
    最近更新 更多