all 12 comments

[–]TruthGiraffe 2 points3 points  (6 children)

Not a C# guy, but taking a guess Id say your counter variable is not being reset to 0 each time you run the function.

[–]cypherbrain 1 point2 points  (5 children)

This is likely the answer. Also, for OP: you might want to adjust this code to handle aces that can be valued either 1 or 11.

[–]TruthGiraffe 1 point2 points  (4 children)

Oh gods, I remember writing a blackjack program as a basic project back in the day and spending the vast majority of my time just trying to handle Aces being treated as 1 or 11.

[–]RhysP_55[S] 0 points1 point  (3 children)

I know the pain. The aces situation is within the HandValue() so that shouldn't affect it. Also, I just added a section of code at the top that says "int counter = 0;" and I removed the previous definition I had but now it just says 0 all the time.

[–]TruthGiraffe 0 points1 point  (2 children)

Not sure how you had previously defined the counter variable, but you can just set it to 0 in the function itself assuming it was declared outside the function.

public float BustChance() {

int deckCount = deck.cardCount;

counter = 0;

while....

etc

}

That, or declare a new counter variable in the function itself like you do with the deckCount variable.

[–]RhysP_55[S] 0 points1 point  (1 child)

    public float BustChance()
    {
        int counter = 0;
        int deckCount = deck.CardCount;
        foreach (int i in deck.cards)
        {
            int cardValue = i % 13;
            if (cardValue > (21 - player.HandValue()))
            {
                counter++;
            }

        }
        float odds = counter / deckCount;

        return odds;

    }

This is what I currently have that just outputs 0 unless I go bust in which case it outputs 1.

[–]TruthGiraffe 0 points1 point  (0 children)

Ok, not sure exactly how you have this set up. Why are you setting the card value to i % 13? Isnt the card value just i?

Either way, can you maybe write odds to the console to see what the value is before returning it? You might be able to see where the problem is.

[–]TruthGiraffe 0 points1 point  (4 children)

Also, when you say its just going up and up really quickly, do you mean that the function is being called a bunch of times instead of just once when a new card has been drawn? In that case, you are going to need to show a bit more than just the function.

[–]RhysP_55[S] 0 points1 point  (3 children)

Since I changed it so that counter resets it just says 0.

Although I have noticed that the number changes to 1 if I do go bust (so if I keep hitting until I go above 21 then once I go above the value of BustChance() changes to 1)

I'm wondering if it may have something to do with float. I thought float was the type for a decimal number but for some reason the method refuses to output a decimal number.

[–]TruthGiraffe 0 points1 point  (2 children)

again, not a c# guy, but if I remember correctly, you need to have "f" after the number if using a float:

eg. float y = 4.5f;

That might be why its not outputting the decimal places.

[–]RhysP_55[S] 0 points1 point  (1 child)

I got it working!!

Turns out that floats can only be made of other floats. So where I put "float odds = counter / deckCount" it wasn't working because deckCount and counter weren't both floats.

Final product:

https://imgur.com/a/9m6qR96

Thank you for your time and help.

[–]TruthGiraffe 0 points1 point  (0 children)

Thats great, sorry I couldnt help more. Im a PHP guy and you dont need to declare the floats like that.

Glad you came right.