为了账号安全,请及时绑定邮箱和手机立即绑定

玩家 1 总是赢。这是因为 random.shuffle 不是真正随机的还是我犯了逻辑错误?

玩家 1 总是赢。这是因为 random.shuffle 不是真正随机的还是我犯了逻辑错误?

墨色风雨 2021-12-17 16:59:03
尽管 random.shuffle 每次都会使赔率随机化,但代码始终显示玩家 1 为赢家。在如何洗牌或玩家 1 如何总是赢得比赛方面,我没有任何直接的模式,因为他们并不总是每轮都赢。不管怎样,他们总是赢得整场比赛。我想知道这是否是我使用 sudo-random shuffling 的问题,或者我是否在某个时候犯了一个我没有注意到的逻辑错误。我曾尝试在不同的点放置打印件,以尝试查看我是否在不同的点得到了无法预料/不希望的结果,但没有注意到任何奇怪的输出。我尝试对值进行双重洗牌,看看是否有任何改变,但结果仍然总是玩家 1。当我不洗牌时,玩家 2 确实赢了,所以我假设所有的数学和检查都是正确的。cards = ['r1','r2','r3','r4','r5','r6','r7','r8','r9','r10','b1','b2','b3','b4','b5','b6','b7','b8','b9','b10','y1','y2','y3','y4','y5','y6','y7','y8','y9','y10']#cardShufflerdef cardShuffler(cards):    from random import shuffle    shuffle(cards)    print(cards)    return(cards)#cardArrayDeciphers#colourdef cardColour(selectedCard):    colour = selectedCard[0] #takes the letter on the card intidacting colour    return colour#numberdef cardNum(selectedCard):    number = int(selectedCard[1]) #takes number in a card and convert str to int    return number#winnerCalcdef winnerCalc(p1Card, p2Card):    colour1 = cardColour(p1Card)    colour2 = cardColour(p2Card)    num1 = cardNum(p1Card)    num2 = cardNum(p2Card)    #pattern should go red > black > yellow > red    if(colour1 == "r")and(colour2 == "b"):        winner = ("p1")    elif(colour1 == "b")and(colour2 == "y"):        winner = ("p1")    elif(colour1 == "y")and(colour2 == "r"):        winner = ("p1")    elif num1 > num2:        winner = ("p1")    else:        winner = ("p2")    print("winner "+str(winner)) #debug    return winner#gameValssearchVal = -1p1WinCards = []p2WinCards = []cards = cardShuffler(cards) #function = cardShuffle#gameLoop [player 1 always wins!]while (searchVal != 29):    p1Given = searchVal + 1 #where the code looks for p1 card    p2Given = searchVal + 2    p1Card = cards[p1Given]    p2Card = cards[p2Given]    searchVal = p2Given    winner = winnerCalc(p1Card, p2Card) #function = winnerCalc    if winner == "p1": #giving the winner all the cards        p1WinCards.append(p1Card)        p1WinCards.append(p2Card)我希望winnerAbsolute 的输出是sudo_random,但结果总是玩家1。
查看完整描述

2 回答

?
达令说

TA贡献1821条经验 获得超6个赞

您的 if 语句非常有利于 p1 获胜。我希望 'p2' 获胜的示例代码,因为 p2 具有优越的颜色:


colour1 = 'b'

colour2 = 'r'

num1 = 2

num2 = 1


if(colour1 == "r")and(colour2 == "b"):

    print("p1")

elif(colour1 == "b")and(colour2 == "y"):

    print("p1")

elif(colour1 == "y")and(colour2 == "r"):

    print("p1")

elif num1 > num2:

    print("p1")

else:

    print("p2")


>>> p1

玩家 1 在这里获胜,因为没有检查玩家 2 是否具有更好的颜色。要正确检查正确的获胜者,您可以将 if 语句修改为以下逻辑:


colour1 = 'b'

colour2 = 'r'

num1 = 2

num2 = 1


if (colour1 == "r" and colour2 == "b") or (colour1 == "b" and colour2 == "y") or (colour1 == "y" and colour2 == "r"):

    print("p1")

elif (colour2 == "r" and colour1 == "b") or (colour2 == "b" and colour1 == "y") or (colour2 == "y" and colour1 == "r"):

    print("p2")

elif num1 > num2:

    print("p1")

else:

    print("p2")


>>> p2

您可以在其中将最后一个 elif 和 else 重构为以下代码行:


colour1 = 'b'

colour2 = 'r'

num1 = 2

num2 = 1


if (colour1 == "r" and colour2 == "b") or (colour1 == "b" and colour2 == "y") or (colour1 == "y" and colour2 == "r"):

    print("p1")

elif (colour2 == "r" and colour1 == "b") or (colour2 == "b" and colour1 == "y") or (colour2 == "y" and colour1 == "r"):

    print("p2")

else:

    print("p1" if num1 > num2 else "p2")


>>> p2


查看完整回答
反对 回复 2021-12-17
?
慕工程0101907

TA贡献1887条经验 获得超5个赞

首先,玩你的游戏很有趣。其次,正如我所怀疑的,错误总是出在多个 if 语句中,这特别棘手,因为您认为自己是彻底的,并且在 ned 中它会咬你!


为了清楚起见,让我们举个例子:


p1Card = y8 ,p2Card = b1

让我们运行它:


if(colour1 == "r")and(colour2 == "b") # False

    winner = ("p1")

elif(colour1 == "b")and(colour2 == "y")# False

    winner = ("p1")

elif(colour1 == "y")and(colour2 == "r")# False

    winner = ("p1")

elif num1 > num2: # True

    winner = ("p1")

else:

    winner = ("p2")

因此,赢家 P1 而它应该是 P2。有多种方法可以避免这种情况,但这应该留给您自己解决:)


查看完整回答
反对 回复 2021-12-17
  • 2 回答
  • 0 关注
  • 136 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信