TopCoder problem "OpenBlackjack" used in TCCC '04 Wildcard (Division I Level Three)



Problem Statement

    

In your last few rounds of Blackjack, you have been losing horribly, but you think you may just have been unlucky. To calculate this, you want to figure out the maximum amount of money you could have made, had you played perfectly. You record all the cards that were dealt in the order they were dealt, and create a program to figure out the most money that could have been made from those cards, given your starting money and the amount you always bet.

The game of Blackjack is simple to learn, yet hard to master. Out of some combination of standard 52-card decks, you get two cards, and the dealer gets two cards. The dealer gives one card to you, then one to himself, then a second card to you, and finally a second card to himself. Cards with rank 2 through 10 are valued at their rank. Jacks, Queens and Kings are worth 10 points. Aces are worth 11 points, but if 11 points will put you over 21, the Ace's value reduces to 1 point. The goal is to beat the dealer's points and not go over 21. You must choose whether to "hit" (take another card from the deck to add to your score) before the dealer plays. You may hit as many times as you want while your score is under 21 (you cannot hit with a score of 21). If your score exceeds 21, you "bust", and the round is over. If you "stay" (stop hitting when your score is less than or equal to 21), the dealer plays his turn. The dealer plays according to a simple rule -- while his score is below 17, hit. If the dealer stops on a score less than your score or busts, you win. If you bust, or the dealer's score is greater than yours, you lose your bet. If you have the same score, it's called a "push" and you get back the money you bet for that round. Except for several special situations, when you win you receive the amount you bet. The special situations follow:

Being dealt an Ace and a card worth 10 points results in a starting score of 21, and is called Blackjack. If you or the dealer gets Blackjack on the first two cards dealt, no cards are allowed to be drawn. The scores are compared, and if you win, you receive one and a half times your bet. If you tie, it is a push. If you lose, you just lose the bet.

Doubling down is a way of increasing your winnings by increasing your bet once you see your first two cards. When you double down, you double the bet for this round, and you take exactly one more card from the deck. No matter what card that is, you cannot continue taking any more cards. You can only double down if:

  • You have not taken a hit.
  • Your first two cards total 10 or 11, or your first two cards are an Ace and a nine (in any order).
  • You have sufficient funds to double your bet.

Your method will be given a String cards that represents the cards in the order dealt, an int bet that represents the amount you bet for each round, and an int money that represents your starting money. Each character in cards will be one of '2'-'9', 'T' (Ten), 'J' (Jack), 'Q' (Queen), 'K' (King), or 'A' (Ace). You are to return an int representing the maximum amount of money you could end with after playing through all the rounds that are possible. You are not allowed to stop playing as long as there are more cards in the deck and you have enough money to keep playing. However, if you are playing a round and run out of cards, this is considered a push, and you keep your bet. See examples 4 and 5 for clarification. You must stop playing if you do not have enough money to make the bet for the next round.

 

Definition

    
Class:OpenBlackjack
Method:deckPotential
Parameters:String, int, int
Returns:int
Method signature:int deckPotential(String cards, int bet, int money)
(be sure your method is public)
    
 

Notes

-For those who know the standard rules of Blackjack, there is no insurance, no maximum number of cards, no splits, and the dealer stands on soft 17.
-It sometimes is more advantageous to make less money on a round than is possible. See examples 9 and 10.
 

Constraints

-cards will have between 4 and 50 characters, inclusive.
-Each character in cards will be either a digit '2' - '9', or one of 'AKQJT'.
-bet will be between 2 and 20, inclusive.
-bet will be an even number.
-money will be between bet and 100, inclusive.
-cards will be constructed such that there will not be a way to run out of cards on the first round.
 

Examples

0)
    
"AKQJ"
2
2
Returns: 5
On the first round, you bet 2 dollars. You get blackjack, and the dealer does not. Therefore, you win 1 and 1/2 times your 2 dollar bet, or 3 dollars, giving you a total of 5. At this point, there are no more cards to play.
1)
    
"KAQJKKK7"
2
3
Returns: 1
Dealer gets blackjack. Since there is no possible way to play the first round without losing, you will lose your 2 dollars. For the second round, you do not have enough money to make the bet, so you have to quit, even though you would have won for that round.
2)
    
"KKKKKAKKKAKKKAKKK"
4
8
Returns: 22
Here, you are dealt two kings, and so is the dealer. If you stay with your two kings, you will keep your 4 dollars. However, for the next two rounds, you will lose all your money because the dealer gets Blackjack twice. You don't even make it for the final round of cards. If you hit on your pair of kings at the beginning, you will bust, but it synchronizes the deck so that you win for the next three rounds with blackjack. The total money is 8 - 4 + 6 + 6 + 6 = 22 dollars.
3)
    
"9KA8A"
6
12
Returns: 24
A example of when doubling down in real life is a bad idea, but is a good idea for this problem. You have 20, the dealer has 18. If you simply stay or take the Ace, you win your 6 dollar bet. However, if you put up the other 6 dollars you have, and double down, you get 21 with the Ace, and win 12 more dollars total. Keep in mind that you need the 6 extra dollars to double down.
4)
    
"K8KK2K2K22222"
16
16
Returns: 32
If you hit on the first round, you bust, lose all your money and must quit. If you stay, you win 16 dollars. It is possible to play a second round such that you run out of cards. Therefore, you can push for that round and keep the 32 dollars you have so far.
5)
    
"AKKK87KK"
2
2
Returns: 7
Here, you get blackjack in the first round. For the second round, you have an opportunity to hit, but there are no cards left to play with. Therefore, since you ran out of cards, you could get a push. However, if you do NOT hit, the dealer takes no more cards and you win.
6)
    
"AKQJT98765432AKQJT98765432"
12
67
Returns: 121
7)
    
"TKTKATAJQK2"
10
50
Returns: 50
If you were allowed to hit on 21, you could walk away with 55 dollars instead of 50.
8)
    
"TA34JJ59289"
16
16
Returns: 32
9)
    
"565339AKKKKAKKKAKKKAKKKAKK"
2
5
Returns: 1
If you double down on the first round, you lose and end up with 1 dollar. Otherwise, if you take no hits, one hit, or two hits, you end up losing. After getting blackjack and losing twice to the dealer's blackjacks, you are left with no money.
10)
    
"5557AJKKKAKKKAKKKAAKKKKKKAKKKAKKKA"
2
5
Returns: 1
No matter what happens, the first round stops after the Jack, with you as the winner. If you double down, you will end the round with 9 dollars. You will then survive through three straight losses, and one winning blackjack, to get 6 dollars. Three more losses and you have 0. If you do not double down on the first round, you end the first round with 7 dollars. Three losses later, you walk away with 1 dollar.

Problem url:

http://www.topcoder.com/stat?c=problem_statement&pm=1974

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=5079&pm=1974

Writer:

schveiguy

Testers:

lbackstrom , vorthys

Problem categories:

Recursion, Simulation