Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

blackjack simulator WIP (for card counters)

richodude

New Coder
I have no clue what I'm doing but this code gets the job done. In its current form, this program allows you to calculate the house edge for currently 1 ruleset (6d H17 DAS split up to 2 hands no surrender) by simulating x-millions of hands playing basic strategy. You are able to sim about 625k hands per second which is way better than I thought I could get. My goal is to not pay for CVCX, a $90 program that is much fancier and sims a couple of million hands per second. The calculated house edge is off by about 1.5x so idk if my basic strategy logic is wrong (I don't think so) or I messed something up with splitting (more likely but I debugged that pretty hard). Newer coder here so lemme know if there's any formatting or other stuff that makes you cringe.


Code:
answer = int(input("how much is your bankroll?"))
init_bankroll = answer
answer = int(input("what is your minimum betting unit?"))
bet = answer
answer = int(input("how many rounds would you like to simulate in millions? (1.6sec per mill)"))
multi = answer


"""
answer = input("does the dealer hit on a soft 17? (y/n/def1ult)")
if answer.lower().strip() == "default":
    answer = input("simulate with 23 deviations?")
    if answer.lower().strip() == "y":
        answer = input("wong out at 1 true count? (-1,0,1,x)")
        if answer.lower().strip() == "-1":
            answer = input("how many units will you bet on 1 true 1?")
            true1 = answer.lower().strip()
            answer = input("true 2? (x if the last true count was your max bet)")
            # if answer.lower().strip() == "x":

            true2 = answer.lower().strip()
            answer = input("true 3?")
            # if answer.lower().strip() == "x":

            true3 = answer.lower().strip()
            answer = input("true 4?")
            # if answer.lower().strip() == "x":

            true4 = answer.lower().strip()
            answer = input("true 5?")
            # if answer.lower().strip() == "x":

            true5 = answer.lower().strip()
"""
import random

deck = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11, 2, 3, 4, 5, 6, 7, 8,
        9, 10, 10, 10, 10, 11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11,
        2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11, 2, 3, 4, 5, 6, 7, 8, 9,
        10, 10, 10, 10, 11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11, 2,
        3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11, 2, 3, 4, 5, 6, 7, 8, 9, 10,
        10, 10, 10, 11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11, 2, 3, 4,
        5, 6, 7, 8, 9, 10, 10, 10, 10, 11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10,
        10, 10, 11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11, 2, 3, 4, 5,
        6, 7, 8, 9, 10, 10, 10, 10, 11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10,
        10, 11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]
random.shuffle(deck)
import sys

sys.setrecursionlimit(1000000)

""" EDIT THIS NUMBER TO SET THE AMOUNT OF ROUNDS/HAND SIMULATED """
rounds_simmed = 1000000 * multi
bankroll = init_bankroll
d_hand = 0
p_hand = 0
p_hand2 = 0
p_hand3 = 0
p_hand4 = 0
card = 4
rounds = 0
shoes = 0
change = 0
dub = 1
dub2 = 1


def deal():
    global p_hand
    global d_hand
    p_hand = deck[0] + deck[1]
    d_hand = deck[2] + deck[3]
    # print("player's", deck[0], deck[1], "vs dealer's", deck[2])
    if d_hand == 21:
        compare()
        return
    if deck[2] == 2:
        d2()
    if deck[2] == 3:
        d3()
    if deck[2] == 4:
        d4()
    if deck[2] == 5:
        d5()
    if deck[2] == 6:
        d6()
    if deck[2] == 7:
        d7()
    if deck[2] == 8:
        d8()
    if deck[2] == 9:
        d9()
    if deck[2] == 10:
        d10()
    if deck[2] == 11:
        d11()


def compare():
    global deck
    global p_hand
    global p_hand2
    global p_hand3
    global p_hand4
    global d_hand
    global card
    global bankroll
    global rounds
    global shoes
    global rounds_simmed
    global change
    global dub
    global dub2
    global bet
    rounds += 1
    # print("Dealer turns up a", deck[3])
    if deck[2] == 11 or deck[3] == 11:
        while d_hand < 18:
            d_hand = d_hand + deck[card]
            # print("dealer hits a", deck[card])
            card += 1
        if d_hand > 21:
            d_hand -= 10
    if d_hand > 7 and deck[card] == 11:
        while d_hand < 18:
            d_hand = d_hand + deck[card]
            # print("dealer hits a", deck[card])
            card += 1
        if d_hand > 21:
            d_hand -= 10
    while d_hand < 17:
        d_hand = d_hand + deck[card]
        # print("dealer hits a", deck[card])
        if d_hand > 21 and deck[card] == 11:
            d_hand -= 10
            card += 1
    # print("player: ", p_hand)
    # print("dealer: ", d_hand)
    if d_hand < p_hand < 22 or d_hand > 21 >= p_hand:
        # print("you win")
        bankroll += bet * dub
        dub = 1
    elif d_hand > p_hand or p_hand > 21:
        # print("you lose")
        bankroll -= bet * dub
        dub = 1
    else:
        # print("push")
        dub = 1
    if p_hand2 > 0:
        # print("player's hand 2: ", p_hand2)
        # print("dealer: ", d_hand)
        if d_hand < p_hand2 < 22 or d_hand > 21 >= p_hand2:
            # print("you win hand 2")
            bankroll += bet * dub2
            dub2 = 1
            p_hand2 = 0
        elif d_hand > p_hand2 or p_hand2 > 21:
            # print("you lost hand 2")
            bankroll -= bet * dub2
            dub2 = 1
            p_hand2 = 0
        else:
            # print("push on hand 2")
            dub2 = 1
            p_hand2 = 0
    del deck[0:card]
    card = 4
    rounds += 1
    if len(deck) < 52:
        shoes += 1
        deck = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11, 2, 3, 4, 5,
                6, 7, 8, 9, 10, 10, 10, 10, 11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11, 2, 3, 4, 5, 6, 7, 8, 9, 10,
                10, 10, 10, 11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11,
                2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11, 2, 3, 4, 5, 6,
                7, 8, 9, 10, 10, 10, 10, 11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10,
                10, 10, 11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11, 2,
                3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11, 2, 3, 4, 5, 6, 7,
                8, 9, 10, 10, 10, 10, 11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10,
                10, 10, 11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11, 2,
                3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11, 2, 3, 4, 5, 6, 7,
                8, 9, 10, 10, 10, 10]
        random.shuffle(deck)
        if shoes % 10000 == 0:
            print("Current bankroll after", rounds, "rounds and", shoes, "shoes: $", bankroll)
    if rounds == rounds_simmed:
        change = bankroll - init_bankroll
        print("simulation complete after", rounds_simmed, "rounds. Ending bankroll is", bankroll,
          "which is a change of $", change, ", which equates to a house edge of", (100 / bet) * change / rounds_simmed, "% (-0.67% excpected)")
        return


def blackjack():
    global bankroll
    global bet
    bankroll += bet/2
    compare()


def pdouble():
    global p_hand
    global card
    global dub
    dub = 2
    p_hand = p_hand + deck[card]
    # print("Doubling down...")
    # print("Player hits a", deck[card])
    if p_hand > 21 and deck[card] == 11:
        p_hand -= 10
    card += 1


def psoftdouble():
    global p_hand
    global card
    global dub
    dub = 2
    p_hand = p_hand + deck[card]
    # print("Doubling down...")
    # print("Player hits a", deck[card])
    if p_hand > 21:
        p_hand -= 10
    card += 1


def pdouble2():
    global p_hand2
    global card
    global dub2
    dub2 = 2
    p_hand2 = p_hand2 + deck[card]
    # print("Doubling down on hand 2...")
    # print("Player's hand 2 hits a", deck[card])
    if p_hand2 > 21 and deck[card] == 11:
        p_hand2 -= 10
    card += 1


def psoftdouble2():
    global p_hand2
    global card
    global dub2
    dub2 = 2
    p_hand2 = p_hand2 + deck[card]
    # print("Doubling down on hand 2...")
    # print("Player's hand 2 hits a", deck[card])
    if p_hand2 > 21:
        p_hand2 -= 10
    card += 1


def split_aces():
    global p_hand
    global p_hand2
    global card
    # print("Player hits a", deck[card])
    p_hand = 11 + deck[card]
    card += 1
    # print("Player's second hand hits a", deck[card])
    p_hand2 = 11 + deck[card]
    card += 1


def split():
    global p_hand
    global p_hand2
    p_hand = deck[0]
    p_hand2 = p_hand
    # print("splitting...")


def hit():
    global p_hand
    global card
    p_hand = p_hand + deck[card]
    # print("Player hits a", deck[card])
    if p_hand > 21 and deck[card] == 11:
        p_hand -= 10
    card += 1


def hit2():
    global p_hand2
    global card
    p_hand2 = p_hand2 + deck[card]
    # print("Player's second hand hits a", deck[card])
    if p_hand2 > 21 and deck[card] == 11:
        p_hand2 -= 10
    card += 1


def hit13():
    global p_hand
    global deck
    if deck[0] == 11 or deck[1] == 11:
        while p_hand < 18:
            hit()
    if p_hand > 21:
        p_hand -= 10
    while p_hand < 13:
        hit()


def hit132():
    global p_hand2
    global deck
    if deck[card - 1] == 11:
        while p_hand2 < 18:
            hit2()
    if p_hand2 > 21:
        p_hand2 -= 10
    while p_hand2 < 13:
        hit2()


def hit12():
    global p_hand
    global deck
    if deck[0] == 11 or deck[1] == 11:
        while p_hand < 18:
            hit()
    if p_hand > 21:
        p_hand -= 10
    while p_hand < 12:
        hit()


def hit122():
    global p_hand2
    global deck
    if deck[card - 1] == 11:
        while p_hand2 < 18:
            hit2()
    if p_hand2 > 21:
        p_hand2 -= 10
    while p_hand2 < 12:
        hit2()


def hit17_78():
    global p_hand
    global deck
    if deck[0] == 11 or deck[1] == 11:
        while p_hand < 18:
            hit()
    if p_hand > 21:
        p_hand -= 10
    while p_hand < 12:
        hit()


def hit172_78():
    global p_hand2
    global deck
    if deck[card - 1] == 11:
        while p_hand2 < 18:
            hit2()
    if p_hand2 > 21:
        p_hand2 -= 10
    while p_hand2 < 12:
        hit2()


def hit17():
    global p_hand
    global deck
    if deck[0] == 11 or deck[1] == 11:
        while p_hand < 19:
            hit()
    if p_hand > 21:
        p_hand -= 10
    while p_hand < 12:
        hit()


def hit172():
    global p_hand2
    global deck
    if deck[card - 1] == 11:
        while p_hand2 < 19:
            hit2()
    if p_hand2 > 21:
        p_hand2 -= 10
    while p_hand2 < 12:
        hit2()


def d2():
    global p_hand
    global p_hand2
    global p_hand3
    global p_hand4
    global card
    if p_hand == 21:
        blackjack()
    if p_hand == 10 or p_hand == 11:
        pdouble()
        compare()
        return
    if deck[0] == deck[1]:
        if p_hand == 22:
            split_aces()
        if deck[0] == 2 or deck[0] == 3 or deck[0] == 6 or deck[0] == 7 or deck[0] == 8 or deck[0] == 9:
            split()
            hit()
            if p_hand == 10 or p_hand == 11:
                pdouble()
                hit2()
            hit13()
            hit2()
            if p_hand2 == 18 and deck[card - 1] == 11:
                pdouble2()
            if p_hand2 == 10 or p_hand2 == 11:
                pdouble2()
            hit132()
            compare()
            return
    if deck[0] == 11 or deck[1] == 11:
        if p_hand == 18:
            psoftdouble()
            compare()
            return
    hit13()
    compare()


def d3():
    global p_hand
    global p_hand2
    global p_hand3
    global p_hand4
    global card
    if p_hand == 21:
        blackjack()
    if p_hand == 9 or p_hand == 10 or p_hand == 11:
        pdouble()
    if deck[0] == deck[1]:
        if p_hand == 22:
            split_aces()
        if deck[0] == 2 or deck[0] == 3 or deck[0] == 6 or deck[0] == 7 or deck[0] == 8 or deck[0] == 9:
            split()
            hit()
            if p_hand == 9 or p_hand == 10 or p_hand == 11:
                pdouble()
                hit2()
            hit13()
            hit2()
            if 16 < p_hand2 < 19 and deck[card - 1] == 11:
                pdouble2()
            if p_hand2 == 9 or p_hand2 == 10 or p_hand2 == 11:
                pdouble2()
            hit132()
            compare()
            return
    if deck[0] == 11 or deck[1] == 11:
        if 16 < p_hand < 19:
            psoftdouble()
            compare()
            return
    hit13()
    compare()


def d4():
    global p_hand
    global p_hand2
    global p_hand3
    global p_hand4
    global card
    if p_hand == 21:
        blackjack()
    if p_hand == 9 or p_hand == 10 or p_hand == 11:
        pdouble()
    if deck[0] == deck[1]:
        if p_hand == 22:
            split_aces()
        if deck[0] == 2 or deck[0] == 3 or deck[0] == 6 or deck[0] == 7 or deck[0] == 8 or deck[0] == 9:
            split()
            hit()
            if p_hand == 9 or p_hand == 10 or p_hand == 11:
                pdouble()
                hit2()
            hit12()
            hit2()
            if 14 < p_hand2 < 19 and deck[card - 1] == 11:
                pdouble2()
            if p_hand2 == 9 or p_hand2 == 10 or p_hand2 == 11:
                pdouble2()
            hit122()
            compare()
            return
    if deck[0] == 11 or deck[1] == 11:
        if 14 < p_hand < 19:
            psoftdouble()
            compare()
            return
    hit12()
    compare()


def d5():
    global p_hand
    global p_hand2
    global p_hand3
    global p_hand4
    global card
    if p_hand == 21:
        blackjack()
    if p_hand == 9 or p_hand == 10 or p_hand == 11:
        pdouble()
    if deck[0] == deck[1]:
        if p_hand == 22:
            split_aces()
        if deck[0] == 2 or deck[0] == 3 or deck[0] == 4 or deck[0] == 6 or deck[0] == 7 or deck[0] == 8 or deck[0] == 9:
            split()
            hit()
            if p_hand == 9 or p_hand == 10 or p_hand == 11:
                pdouble()
                hit2()
            hit12()
            hit2()
            if 12 < p_hand2 < 19 and deck[card - 1] == 11:
                pdouble2()
            if p_hand2 == 9 or p_hand2 == 10 or p_hand2 == 11:
                pdouble2()
            hit122()
            compare()
            return
    if deck[0] == 11 or deck[1] == 11:
        if 12 < p_hand < 19:
            psoftdouble()
            compare()
            return
    hit12()
    compare()


def d6():
    global p_hand
    global p_hand2
    global p_hand3
    global p_hand4
    global card
    if p_hand == 21:
        blackjack()
    if p_hand == 9 or p_hand == 10 or p_hand == 11:
        pdouble()
    if deck[0] == deck[1]:
        if p_hand == 22:
            split_aces()
        if deck[0] == 2 or deck[0] == 3 or deck[0] == 4 or deck[0] == 6 or deck[0] == 7 or deck[0] == 8 or deck[0] == 9:
            split()
            hit()
            if p_hand == 9 or p_hand == 10 or p_hand == 11:
                pdouble()
                hit2()
            hit12()
            hit2()
            if 12 < p_hand2 < 20 and deck[card - 1] == 11:
                pdouble2()
            if p_hand2 == 9 or p_hand2 == 10 or p_hand2 == 11:
                pdouble2()
            hit122()
            compare()
            return
    if deck[0] == 11 or deck[1] == 11:
        if 12 < p_hand < 20:
            psoftdouble()
            compare()
            return
    hit12()
    compare()


def d7():
    global p_hand
    global p_hand2
    global p_hand3
    global p_hand4
    global card
    if p_hand == 21:
        blackjack()
    if p_hand == 10 or p_hand == 11:
        pdouble()
        compare()
        return
    if deck[0] == deck[1]:
        if p_hand == 22:
            split_aces()
        if deck[0] == 2 or deck[0] == 3 or deck[0] == 7 or deck[0] == 8:
            split()
            hit()
            if p_hand == 10 or p_hand == 11:
                pdouble()
                hit2()
            hit17_78()
            hit2()
            if p_hand2 == 10 or p_hand2 == 11:
                pdouble2()
            hit172_78()
            compare()
            return
    hit17_78()
    compare()


def d8():
    global p_hand
    global p_hand2
    global p_hand3
    global p_hand4
    global card
    if p_hand == 21:
        blackjack()
    if p_hand == 10 or p_hand == 11:
        pdouble()
        compare()
        return
    if deck[0] == deck[1]:
        if p_hand == 22:
            split_aces()
        if deck[0] == 8 or deck[0] == 9:
            split()
            hit()
            if p_hand == 10 or p_hand == 11:
                pdouble()
                hit2()
            hit17_78()
            hit2()
            if p_hand2 == 10 or p_hand2 == 11:
                pdouble2()
            hit172_78()
            compare()
            return
    hit17_78()
    compare()


def d9():
    global p_hand
    global p_hand2
    global p_hand3
    global p_hand4
    global card
    if p_hand == 21:
        blackjack()
    if p_hand == 10 or p_hand == 11:
        pdouble()
        compare()
        return
    if deck[0] == deck[1]:
        if p_hand == 22:
            split_aces()
        if deck[0] == 8 or deck[0] == 9:
            split()
            hit()
            if p_hand == 10 or p_hand == 11:
                pdouble()
                hit2()
            hit17()
            hit2()
            if p_hand2 == 10 or p_hand2 == 11:
                pdouble2()
            hit172()
            compare()
            return
    hit17()
    compare()


def d10():
    global p_hand
    global p_hand2
    global p_hand3
    global p_hand4
    global card
    if p_hand == 21:
        blackjack()
    if p_hand == 11:
        pdouble()
        compare()
        return
    if deck[0] == deck[1]:
        if p_hand == 22:
            split_aces()
        if deck[0] == 8:
            split()
            hit()
            if p_hand == 11:
                pdouble()
                hit2()
            hit17()
            hit2()
            if p_hand2 == 11:
                pdouble2()
            hit172()
            compare()
            return
    hit17()
    compare()


def d11():
    global p_hand
    global p_hand2
    global p_hand3
    global p_hand4
    global card
    if p_hand == 21:
        blackjack()
    if p_hand == 11:
        pdouble()
        compare()
        return
    if deck[0] == deck[1]:
        if p_hand == 22:
            split_aces()
        if deck[0] == 8:
            split()
            hit()
            if p_hand == 11:
                pdouble()
                hit2()
            hit17()
            hit2()
            if p_hand2 == 11:
                pdouble2()
            hit172()
            compare()
            return
    hit17()
    compare()


while rounds < rounds_simmed:
    deal()
 
Back
Top Bottom