mercredi 27 mars 2019

Player 1 always wins. Is this because random.shuffle isn' truely random or have I made a logic error?

The code always shows player 1 as the winner despite random.shuffle supposedly making the odds random every time. There isn't any straight forward pattern I have been able to see in how the cards are shuffled or how player 1 always wins the game, as they don't always win each round. Regardless they always win the whole game.

I want to know if this is a problem with me using sudo-random shuffling or have I made a logic error at some point that I failed to notice.

I have tried putting prints in at various points to try and see if I am getting unforeseen/undesired outcomes at different point but haven't noticed any weird outputs.

I tried double shuffling the values to see if that changed anything but the outcome was still always player 1.

Player 2 does win when I don't shuffle so I assume all the maths and checks are right.

#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: ")

I expect the output for winnerAbsolute to be sudo_random but the result is always player 1.




Aucun commentaire:

Enregistrer un commentaire