PROBLEM STATEMENT
You are playing blackjack at a casino. By counting cards, you can increase
your chances of winning. You are to write a method that, given a String of the
unplayed cards, and a String representing the dealer's "upcard", returns the
probability the dealer will bust, rounded to two decimal places and multiplied
by 100 (round up on 5's), i.e. return the percent chance the dealer has of
busting (see examples).
Now, in blackjack the rules are as follows:
- number cards are worth their number: a '2' is worth 2, a '3' is worth 3,...,
a 'T' is worth 10.
- face cards 'J', 'Q', 'K' are all worth 10.
- aces are worth 1 or 11. To simplify things, an ace is worth 1 if the total
value of the cards is greater than 21, otherwise it is worth 11. See examples
of hands below to clarify this.
- all suits are treated as equal - so in blackjack a 5 of diamonds is exactly
the same as a 5 of spades.
- if the dealer has 16 or less total, he will take another card ("hit"). Since
the deck is shuffled, the card he gets is random, i.e. he has the same
probability of getting each card.
- if the dealer has between 17 and 21 (inclusive), he will not take another
card ("stand").
- if the dealer gets over 21, he loses ("busts").
- the dealer must follow these rules, he cannot make his own decisions.
- each card in the deck has an equal chance of being picked by the dealer.
Once the dealer picks the card, it is removed from the deck.
- in blackjack the dealer has an "upcard" (the card showing), and a card not
showing. For the purposes of this problem assume he has nothing but an "upcard".
Here are examples of a few hands - pay particular attention to the way aces
(A's) work:
1. Dealer's upcard is "7".
Total = 7, so dealer must hit.
Dealer gets a '5'. Total = 12, so dealer must hit.
Dealer gets a '7'. Total = 19, so dealer stands on 19 (he does not bust).
2. Dealer's upcard is "A".
Total = 11, so dealer must hit.
Dealer gets a '5'. Total = 16 so dealer must hit.
Dealer gets a 'T'. Total would be 26 if 'A' is 11 - so 'A' becomes 1. Total
= 16 so dealer must hit.
Dealer gets a '5'. Total = 21, so dealer stands on 21 (he does not bust).
3. Dealer's upcard is "A".
Total = 11, so dealer must hit.
Dealer gets a 'A'. Total would be 22 if 'A' is 11 - so an 'A' becomes 1.
Total = 12 so dealer must hit.
Dealer gets a 'Q'. Total would be 22 if 'A' is 11 - so the other 'A' becomes
1. Total = 12 so dealer must hit.
Dealer gets a 'J'. Total = 22. Each ace is already worth 1 so dealer busts.
4. Dealer's upcard is "6".
Total = 6, so dealer must hit.
Dealer gets a 'A'. Total = 17, so dealer stands (no bust).
DEFINITION
Class Name: Blackjack
Method Name: bustPct
Parameters: String, String
Returns: int
Method signature (be sure your method is public): int bustPct (String deck,
String upcard);
NOTES
* Do not assume that only one deck is being used. The casino uses multiple
decks, but at most 50 cards are left on the deck. So for example, it is
possible to have deck = "33333333333333333", even though there are only four
"3"s in a standard deck of cards.
TopCoder will ensure that:
- deck will contain characters representing cards from standard decks. Each
character will be either an 'A' (ace), '2' (two), '3' (three), '4' (four), '5'
(five), '6' (six), '7' (seven), '8' (eight), '9' (nine), 'T' (ten), 'J' (jack),
'Q' (queen), 'K' (king). No other characters will appear in the String.
- deck will be sorted according to the order listed above
(A,2,3,4,5,6,7,8,9,T,J,Q,K). For example if two 2's, one 4 and an ace were in
the deck, deck would be "A224". See examples below - all decks in the examples
are sorted.
- There will be at most 50 unplayed cards in the deck (the String's length will
be at most 50).
- upcard will be either a "A", "2", "3", "4", "5", "6", "7", "8", "9", "T",
"J", "Q", or "K".
- there are always enough cards for the dealer to follow the rules (it will
never happen that the dealer has 15 where he should "hit", but the deck is out
of cards).
EXAMPLES
* You can see a clearer illustration of the first 2 examples at:
http://www.topcoder.com/contest/problem/blackjack/blackjack.html
1. if deck = "35TJ" and upcard = "7"
The possibility tree is as follows:
*let t=x mean that x represents the dealer's total so far
*note the tree is not "in order" because it is more balanced and easier to draw
out of order
7
t=7,hit
/ / \ \
3 J T 5
t=10,hit t=17,STAND t=17,STAND t=12,hit
/ / | | \ \
5 T J T J 3
t=15,hit t=20,STAND t=20,STAND t=22,BUST t=22,BUST
t=15,hit
/ \ /
\
T J T
J
t=25,BUST t=25,BUST
t=25,BUST t=25,BUST
These are the only ways he will bust:
(i) if he gets a 5 first, he will bust no matter what - p = (1/4).
(ii) if he gets a 3 first, and a 5 second - p = (1 / 4) * (1 / 3) = (1 / 12).
The dealers total probability of busting is (1/4) + (1/12) = (1/3) = .3333...
so the dealer has a 33% chance of busting (rounded to the nearest percent).
Your method should return 33.
2. if deck = "38Q" and upcard = "A"
A
/ | \
3 8 Q
t=14,hit t=19,STAND t=21,STAND
/ |
8 Q (the ace starts out as an 11,
but if the dealer
t=12,hit t=14,hit goes over 21, make it a 1)
| |
Q 8
t=22,BUST t=22,BUST
The dealer will bust if and only if he gets a 3 first. 33% chance (rounded to
the nearest percent).
Your method should return 33.
3. deck = "AAA222333444555666777888999TTTJJJQQQKKK" (3 of each card)
if upcard = "2" your method should return 35.
if upcard = "9" your method should return 23.
if upcard = "A" your method should return 12.