Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Blackjack error
#1
When it gets to damage calculation in my blackjack rpg thing, 2d2 + 3d6 somehow does 146 damage. Help!

#This is blackjack adventure
import random

global cards
global corpoint

strd2 = "d2"
strd4 = "d4"
strd6 = "d6"
strd8 = "d8"
strd10 = "d10"
strd12 = "d12"

cards = ["junk", "ace", "two", "three" , "four", "five", "six", "seven", "eight", "nine", "ten", "jack", "queen", "king"]
corpoint = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]
#correlating point value^
def deckreset() :
    global deck
    deck = []
    deckset = 0
    while deckset < 4 :
        deck.append("ace")
        deck.append("two")
        deck.append("three")
        deck.append("four")
        deck.append("five")
        deck.append("six")
        deck.append("seven")
        deck.append("eight")
        deck.append("nine")
        deck.append("ten")
        deck.append("jack")
        deck.append("queen")
        deck.append("king")
        deckset += 1
#reset the deck^

def draw() :
    global carddrawn
    drawn = random.randint(1, 13)
    carddrawn = cards[drawn]
    value = corpoint[drawn]
    deck.remove(carddrawn)
    return value
#draw a card^

def battle(enemy, enemyhealth, conf) :
    
    ehealth = enemyhealth
    global etotal
    global epassed
    global elose
    global edead
    epassed = False
    elose = False

    global phealth
    global ppassed
    global plose
    global ptotal
    global pdead
    ppassed = False
    plose = False
    phealth = 200
    phit = False

    bidded = False

    while phealth >= 0 and ehealth >= 0 :
        diepool = ["d2", "d2"]
        lastbid = ["d2"]
        deckreset()
        ptotal = draw() + draw()
        etotal = draw() + draw()
        edead = False
        pdead = False
        
        while elose == False and plose == False and pdead == False and edead == False :
            print("HP: " + str(phealth))
            print("Enemy HP: " + str(ehealth))
            if ppassed == False or epassed == False :
                while ppassed == False and phit == False :
                    print("Your total is " + str(ptotal) + ". Would you like to pass or hit?")
                    answer = input()
                    if answer == "pass" :
                        ppassed = True
                    if answer == "hit" :
                        while bidded == False :
                            print("Last wager was " + str(lastbid) + ".")
                            print("Would you like to raise or match?")
                            raiseormatch = input()
                            if raiseormatch == "raise" :
                                print("What die type would you like to wager (ex. d12)?")
                                dtype = input()
                                print("How many dice would you like to roll?")
                                numrolled = int(input())
                                if numrolled <= 0 :
                                    print("You have to wager.")
                                elif dtype != "d2" and dtype != "d4" and dtype != "d6" and dtype != "d8" and dtype != "d10" and dtype != "d12" :
                                    print("That is not an available type.")
                                else :
                                    while numrolled > 0 :
                                        diepool.append(dtype)
                                        lastbid.append(dtype)
                                        numrolled -= 1
                                    more = input("Would you like to wager more?\n")
                                    if more == "no" :
                                        bidded = True
                                    elif more == "yes" :
                                        print("")
                                    else :
                                        print("I'll take that as a no.")
                                        bidded = True
                            if raiseormatch == "match" :
                                diepool.append(lastbid)
                                bidded = True
                        pointcheck = draw()
                        if carddrawn == "ace" or carddrawn == "eight" :
                            print("You drew an " + carddrawn + ".")
                        else :
                            print("You drew a " + carddrawn + ".")
                            ptotal += pointcheck
                        if carddrawn == "ace" :
                            pointcheck = int(input("Is the ace a 1 or an 11?"))
                            if pointcheck == 11 or pointcheck == 1 :
                                ptotal += pointcheck
                            else :
                                print("I'll take that as a 1.")
                                ptotal += 1
                        if carddrawn == "eight" :
                            ptotal += pointcheck
                        if ptotal == 21 :
                            print("Your total is 21.")
                            ppassed = True
                        if ptotal > 21 :
                            print("You lose.")
                            ppassed = True
                            plose = True
                        else :
                            print("Your total is " + str(ptotal) + ".")
                        phit = True
                    else :
                        print("That wasn't an option.")
                if epassed == False and elose == False and plose == False:
                        ehit = False
                        if etotal < conf :
                            print(enemy + " hits.")
                            etotal += draw()
                            diepool += lastbid
                            ehit = True
                        if etotal >= conf and ehit == False and etotal <= 21 :
                            print(enemy + " passes.")
                            epassed = True
                        if etotal > 21 :
                            print(enemy + " loses.")
                            elose = True
                phit = False
                bidded = False

            if elose == False and plose == False :
                if etotal < ptotal :
                    elose = True
                if ptotal <= etotal :
                    plose = True
                
            if elose == True :
                damage = 0
                for strd2 in diepool :
                    damage += d2()
                for strd4 in diepool :
                    damage += d4()
                for strd6 in diepool :
                    damage += d6()
                for strd8 in diepool :
                    damage += d8()
                for strd10 in diepool :
                    damage += d10()
                for strd12 in diepool :
                    damage += d12()
                print("damage = " + str(damage))
                ehealth -= damage
                if ehealth <= 0 :
                    edead = True

            if plose == True :
                damage = 0
                for strd2 in diepool :
                    damage += d2()
                for strd4 in diepool :
                    damage += d4()
                for strd6 in diepool :
                    damage += d6()
                for strd8 in diepool :
                    damage += d8()
                for strd10 in diepool :
                    damage += d10()
                for strd12 in diepool :
                    damage += d12()
                print(damage)
                phealth -= damage
                if phealth <= 0 :
                    pdead = True
        plose = False
        ppassed = False
        elose = False
        epassed = False
            
            
def d2() :
    return random.randint(1, 2)
def d4() :
    return random.randint(1, 4)
def d6() :
    return random.randint(1, 6)
def d8() :
    return random.randint(1, 8)
def d10() :
    return random.randint(1, 10)
def d12() :
    return random.randint(1, 12)
#Dice(this is not going to go too well.)


print("A kobold poker challenges you!")
battle("kobold poker", 100, 21)
if edead == True :
    print("You won!")
if pdead == True :
    print("You died.")
    
if pdead == False :
    print("A gamblin challenges you!")
    battle("gamblin", 120, 19)
    if edead == True :
        print("You won!")
    if pdead == True :
        print("You died.")

if pdead == False :
    print("The Deckromancer challenges you!")
    battle("The Deckromancer", 200, 16)
    if edead == True :
        print("You won!")
    if pdead == True :
        print("You died.")

if pdead == False:
    print("「ZA HANDO」 challenges you!")
    battle("「ZA HANDO」", 500, 18)
    if edead == True :
        print("You won!")
    if pdead == True :
        print("You died.")

if pdead == False :
    print("RNG Dragon challenges you!")
    battle("RNG Dragon", 1000, 15)
    if edead == True :
        print("You won! You beat the game!")
    if pdead == True :
        print("You died. So close.")
Reply
#2
Please be more specific, show line numbers of suspected problem area.
Reply
#3
strd2 starts as 'd2'. You have this bit of code (lines 168-169):

                for strd2 in diepool :
                    damage += d2()
This does not go through diepool and each time there is a 'd2' add d2() to the damage. It goes through each item in die pool, changes strd2 to that item, and then adds d2() to the damage. So for each die in your diepool, you are adding up a d2, a d4, a d6, a d8, a d10, and a d12. That's why your damage is so huge.

You need to think of it this way:

damage = 0
for die in diepool:
    if die == strd2:     # is this really easier than die == 'd2'?
        damage += d2()
    elif die == strd4:
        damage += d4()
    ...
But this leads to a big if/elif chain. There is a simpler way to do this. When you make diepool, instead of appending the string 'd2' to it, you could append the function d2 to it, without calling the function. Then you could do this:

damage = 0
for die in diepool:
    damage += die()
or even simpler: damage = sum(die() for die in diepool).

If that's too confusing, you could just have one die function that takes an integer parameter, and instead of appending 'd2' to diepool, you could append the integer 2 to die pool:

def die(sides):
    return random.randint(sides)

...

damage = 0
for sides in diepool:
    damage += die(sides)
Also note that you can simplify this:

elif dtype != "d2" and dtype != "d4" and dtype != "d6" and dtype != "d8" and dtype != "d10" and dtype != "d12" :
to this:

elif dtype not in ('d2', 'd4', 'd6', 'd8', 'd10', 'd12'):
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#4
ichabod801 Thank you! I'm only a student so this is super helpful.
Reply


Forum Jump:

User Panel Messages

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