-1

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.

Kevin
  • 1
  • 2
  • 1
    you are using random.shuffle ... and random.sample for some reason ... think of random shuffle as shuffling a deck and then you can just take x cards off the top normally ... in this you are shuffling the deck and then taking x cards from anywhere in the deck as chosen randomly – Joran Beasley Apr 29 '20 at 01:02
  • At a higher level, you don't need `shuffle()` at all, not anywhere. Build the deck just once and never change it. `random.sample(deck, 6)` is all you need to pick 6 cards "at random", and you can do that as often as you like. – Tim Peters Apr 29 '20 at 01:04

2 Answers2

0

I think you can take a simpler approach to the game mechanics. Use numpy to take six random integers from 1 to 52. Define aces as having a value of 1-4. So if the first element of the sorted array is < 5, then you've drawn an ace.

import numpy as np

# simulates six cards
draw = np.sort(np.random.randint(1, 52, 6)) 
if draw[0] < 5:
    print('drew an ace')
else:
    print('did not draw an ace')

You can repeat the draw in the loop whenever you need to do another round.

If you need to keep the card names, then build the deck once, and sample six items from the deck array on each iteration. There is no need to shuffle.

Eric Truett
  • 2,970
  • 1
  • 16
  • 21
  • Although I appreciate the feedback, I am relatively new to python. I was hoping to use random.shuffle as I'm pretty sure there are more effective ways of running this program. But I was wondering if perhaps I could still use random.shuffle in this program and how I would go away about it. Any help would be grateful. – Kevin Apr 29 '20 at 01:22
0

The sanest ;-) change would be to replace

    table = random.sample(shuffledDeck(),6)

with

    table = random.sample(d, 6)

There's no need to keep rebuilding the deck. If you're determined to use the more expensive .shuffle() instead, then, e.g., replace that line with

    random.shuffle(d)
    table = d[:6] # or d[-6:], or any other way of picking 6
Tim Peters
  • 67,464
  • 13
  • 126
  • 132
  • Thank you for the feedback, but I was wondering how I would go across fixing the {'int' object is not callable} problem now. – Kevin Apr 29 '20 at 01:35
  • You would have to show us the exact code you're running. The program ran fine for me after making either of the suggestions I posted here. – Tim Peters Apr 29 '20 at 01:38
  • At least show us the exact line of code triggering that error. The only calls I suggested were `random.sample(d, 6)` and `random.shuffle(d)`, which couldn't possibly trigger "int object is not callable" unless you made other changes that assigned an integer to `random.shuffle` or `random.sample`. – Tim Peters Apr 29 '20 at 01:43
  • Ah! Perhaps you copied from my answer when it said `random.shuffle(d, 6)`, which was a typo. _That_ could trigger an "int not callable" error. `random.shuffle(d)` was intended. – Tim Peters Apr 29 '20 at 01:46
  • I didn't change much, perhaps it was a typo. And it did work, thank you so much. Sorry for the inconvenience that I may of cause. Do you mind explaining what exactly is going on when table = d[:6]? – Kevin Apr 29 '20 at 02:00
  • `d[:6]` returns the first 6 elements of `d` as a new list of their own. Much more on "slice notation" here: https://stackoverflow.com/questions/509211/understanding-slice-notation – Tim Peters Apr 29 '20 at 02:07