- Home /
Help with Blackjack Probability Concept
Hi, I have created a functioning Blackjack game and want to add a unique feature so that there is some text on the screen that shows the user the probability of the next card making them bust if they click hit. However, I don't know a good way of doing this in unity. I would have to look through the deck and their values and then work out how many cards would make the users hand value higher than 21 and then divide that by the number of cards remaining in the deck, I think. Any help is much appreciated. Thanks
Answer by surfuay · Apr 09, 2019 at 06:03 PM
this is not meant to sound mean so sorry
just look up on google the base math equation that does just that.
then adapt it to code.
to get it to show in your screen you'll need 2 things
public float bustChance
and
public text bustText
public void update () { Equation() bustText.text = "" + bustChance;
}
public void Equation() { bustChance = equation
the equation may be multiple lines }
Answer by azilvl · Apr 09, 2019 at 06:18 PM
something like this would approximately calculate the odds of getting over 21
float odds = Mathf.Max(0, 13 - (21 - playersHandValue)) * 4 / 52;
// Can be simplified to:
float odds = Mathf.Max(0, playersHandValue - 8) * 4 / 52;
keep in mind this is just an approximation and doesn't take into account that some of the cards have been drawn. if you truly want to get the exact odds you'd have to do what you already know.
int losingCardsCount = GetNumberOfRemainingCardsWithValueOverInt(21 - playersHandValue);
float odds = (float)losingCardsCount / (float)totalRemainingCardsInDeck;
int GetNumberOfCardsWithValueOverInt(int num)
{
//go through each card with a value bigger than num and count how many of them are left
//if you have a list of cards you can do this:
//List<Card> remainingCards = new list<Card>();
//remainingCards = allCardsList.FindAll(x => x.value > num && x.hasNotBeenDrawn);
//return remainingCards.count;
}
Your answer
Follow this Question
Related Questions
cpu logic in cards game 0 Answers
What's the best way to couple logic and rendering in a card game? 1 Answer
Is deviceUniqueIdentifier actually unique? 0 Answers
Ride across a tree? 1 Answer
Mecanim Trigger Stays too long true 1 Answer