mardi 28 avril 2020

How to change this program to use random.shuffle instead of random.sample?

I was wondering if there was anyone who was willing to help me out, when it came to this program. Backstory is to create a card game that when you draw 6 random cards, if one card is an ace, the player wins a dollar if not they lose a dollar. This goes on till the player either doubles their money or loses it all. The game is ran 1000 times, and starts with an initial of 10 dollars. Here is my code:

import random

faceValues = ['ace', '2', '3', '4', '5', '6',
              '7', '8', '9', '10', 'jack',
              'queen', 'king']
suits = ['clubs', 'diamonds', 'hearts',
         'spades']
def shuffledDeck():
    deck = []
    for faceValue in faceValues:
        for suit in suits:
            deck.append(faceValue + ' of ' + suit)
    random.shuffle(deck)
    return deck

def faceValueOf(card):
    return card.split()[0]

def suitOf(card):
    return card.split()[2]

d = shuffledDeck()

def game(initial):
    counts = 0
    bankroll = initial
    while 0 < bankroll < 2*initial:
        ace = 0
        table = random.sample(shuffledDeck(),6)
        counts += 1
        for cards in table:
            if faceValueOf (cards) == 'ace':
                ace += 1
        if ace >= 1:
            bankroll += 1
        else:
            bankroll -= 1

    return counts


initial = float(input('Enter initials: '))
totalcounts = 0
for x in range(1000):
    totalcounts += game(initial)

print('Average number of rounds: ', totalcounts/1000)

I was wondering how I would be able to alter this program, so instead of using random.sample, I could use random.shuffle. I tried many ways in changing this program, but whenever I do, my end results are not the same. I'm just trying to change the section where I have to draw cards. When running this program starting at an initial 10, the answer is roundabout 45 to 50 in terms of rounds played. Edit: Trying to keep the program relatively the same, only want to change what I really need to.




Aucun commentaire:

Enregistrer un commentaire