Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Black jack game simulation
#1
Hi everybody,
I have some questions regarding a code experiment I would like to adapt.

This code was done to stimulate a blackjack experiment. Basically, a deck of card was created and we calculated the distribution of the sum of cards chosen.
The code is done with remplacement of cards. I would like to do the same basically but with no replacement.

The first function calculates the distribution of probabilities for the sum of cards chosen. The second determines whether it is a winning game or not. And the last one determines at each stage of the game the probability to win depending on the number of cards that are still to be drawn.

This is the original code :

#Import useful packages
import numpy as np
import pandas as pd

def gen_distribution(thegame):
    nmax = thegame['nmax']
    xs, counts = np.unique(thegame['deck'], return_counts=True)
    ps = counts / sum(counts)
    allpsum = [pd.Series(ps, index=xs)]

    for n in range(2, nmax + 1):
        sums = np.arange(min(xs) * n, max(xs) * n + 1)
        psum = pd.Series(np.zeros(len(sums)), index=sums)
        psum0 = allpsum[n - 2]

        for kx, x in enumerate(xs):
            for x0 in psum0.index.values:
                psum[x + x0] += psum0[x0] * ps[kx]

        allpsum.append(psum)
    
    return allpsum

def dowin(cs, thegame):
    ub, lb = thegame['ublb']
    badnow = (sum(cs) >= ub) or (sum(cs) <= lb)
    outcome = 0
    
    if badnow:
        if thegame['isbust'] or (not thegame['isbust'] and len(cs) == thegame['nmax']):
            outcome = -1
    else:
        if len(cs) == thegame['nmax']:
            outcome = 1
    
    return outcome

def gen_pwin(thegame):
    nmax = thegame['nmax']
    ub, lb = thegame['ublb']
    isbust = thegame['isbust']

    xs, counts = np.unique(thegame['deck'], return_counts=True)
    ps = counts / sum(counts)
    allpsum = gen_distribution(thegame)

    allpwin = [[] for _ in range(nmax + 1)]
    nmore = 0
    N = nmax
    sums = np.arange(min(xs) * N, max(xs) * N + 1)
    pwin = [int(k < ub and k > lb) for k in sums]
    allpwin[nmore] = pd.Series(pwin, index=sums)
    
    for nmore in range(1, nmax + 1):
        N = nmax - nmore
        sums = np.arange(min(xs) * N, max(xs) * N + 1)
        pwin = []
        for s in sums:
            if isbust:
                if s >= ub or s <= lb:
                    pwin.append(0)
                    continue
                
                pw = 0
                for kx, xnext in enumerate(xs):
                    pw += ps[kx] * allpwin[nmore - 1][s + xnext]
                pwin.append(pw)
            else:
                psum = allpsum[nmore - 1]
                thisp = max(min(sum([psum[smr] for smr in psum.index.values if s + smr < ub and s + smr > lb]), 1), 0)
                pwin.append(thisp)
        
        allpwin[nmore] = pd.Series(pwin, index=sums)
    
    thegame['allpwin'] = allpwin
    return
Thank you in advance Shy
Roxane
Reply
#2
Quote: I would like to do the same basically but with no replacement.

Do you mean, cards taken from the pack should not be put back?

def draw_card(self):
        """
        Draws a card from the deck and removes it from the deck.

        Returns:
        - Card:
            The drawn card.
        """

        if len(self.cards) == 0:
            raise ValueError("The deck is empty.")

        return self.cards.pop()
Reply
#3
Instead of allowing cards to be picked again, you'll have to keep track of the cards that have already been used. You’ll need to update the functions to account for this change. First, you'll need to update the gen_distribution function to consider that once a card is drawn, it can't be picked again. This requires keeping track of the cards in the deck and updating the distribution of the remaining cards after each draw. Second, the dowin function, which determines whether it's a winning game, should remain mostly the same since it already evaluates the conditions based on the sum of cards. Third, the gen_pwin function will need adjustments to handle the changing probabilities as cards are used up from the deck. You’ll calculate the probabilities based on the remaining cards after each draw, ensuring no card is picked more than once.
Reply
#4
Rather than allowing cards to be picked multiple times, you'll need to track the cards that have already been drawn. This means updating the functions to reflect this change. First, modify the gen_distribution function so that once a card is drawn, it cannot be selected again. This involves keeping track of the remaining cards in the deck and adjusting the distribution after each draw. Second, the dowin function, which checks if the game is won based on the sum of the cards, will largely stay the same. Lastly, the gen_pwin function must be updated to account for changing probabilities as cards are used up. You'll calculate these probabilities based on the remaining cards, ensuring that no card is drawn more than once.
Here's they have explained about the game and how to play 21 game, which will give you exact idea.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Why it gives me a black image and libpng warning: iCCP rickyw2777 1 468 Feb-16-2025, 08:26 PM
Last Post: rickyw2777
  Bright Black Screen Issue in Tkinter GUI Application rommy 2 1,031 Nov-29-2024, 10:50 PM
Last Post: woooee
  remove all color but red, then replace it with black kucingkembar 14 11,823 Dec-29-2021, 07:50 PM
Last Post: deanhystad
  The jack output on Python Galtair 0 1,852 Nov-19-2020, 03:10 PM
Last Post: Galtair
  Checkbuttons always come up as black boxes regardless of the state kenwatts275 5 6,878 Jul-07-2020, 08:00 PM
Last Post: kenwatts275
  How to use nb-black python cde formatter ErnestTBass 3 8,623 Jun-04-2020, 03:51 PM
Last Post: ErnestTBass
  Finance: Black Scholes Model not working pwt 5 5,471 May-27-2020, 10:14 AM
Last Post: buran
  Because the emoji appears black and white at the exit ? nerd 3 6,720 Jan-28-2019, 11:34 PM
Last Post: nerd
  Help with simulation Geeseball 0 2,617 Oct-18-2018, 12:19 PM
Last Post: Geeseball
  Python interface only black and white........ Wilson 3 8,161 Jul-15-2017, 01:20 PM
Last Post: sparkz_alot

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020