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

问题描述 投票:0回答:2

代码总是将玩家1显示为赢家,尽管random.shuffle据说每次都会随机输入赔率。没有任何直接的模式,我已经看到了如何洗牌或玩家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']

#cardShuffler
def cardShuffler(cards):
    from random import shuffle
    shuffle(cards)
    print(cards)
    return(cards)


#cardArrayDeciphers
#colour
def cardColour(selectedCard):
    colour = selectedCard[0] #takes the letter on the card intidacting colour
    return colour
#number
def cardNum(selectedCard):
    number = int(selectedCard[1]) #takes number in a card and convert str to int
    return number

#winnerCalc
def 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

#gameVals
searchVal = -1
p1WinCards = []
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)
    else:
        p2WinCards.append(p1Card)
        p2WinCards.append(p2Card)
    print("p1 " +str(len(p1WinCards))) #debug
    print("p2 " +str(len(p2WinCards))) #debug
if len(p1WinCards) > len(p2WinCards): #finding the final winner by the length of win cards
     winnerAbsolute = p1WinCards
     winnerName = input("Player 1 won! Please enter your name: ")
else:
    winnerAbsolute = p2WinCards
    winnerName = input("Player 2 won! Please enter your name: ")

我希望winnerAbsolute的输出为sudo_random,但结果始终是player 1。

python python-3.x random
2个回答
1
投票

你的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

Player1赢了,因为没有检查玩家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,然后重构为以下代码行:

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

0
投票

首先,玩你的游戏很有趣。其次,正如我所怀疑的那样,错误始终存在于多个if语句中,因为您认为自己是彻底的并且在内心它咬你的时候特别棘手!

所以要清楚,让我们举一个例子:

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。避免这种情况的多种方法,但这应该留给你弄清楚:)

© www.soinside.com 2019 - 2024. All rights reserved.