Statistics

Problem Statement for "CardCounter"

Problem Statement

In the game of blackjack, the goal is to get a total as close to 21 as possible without going over, and to have a higher total than the dealer at the end of the game. An ace is worth either 1 or 11 (whichever is more advantageous), face cards are worth 10, and all other cards are worth their shown value. Play follows the following procedure:

  • Two cards each are dealt to the player and dealer. One of the dealer's cards is face-down, the other is face-up. The player can see both of his cards.
  • The player has the option to take a "hit" and be dealt another card. He may continue to take hits until he reaches 21, goes bust by exceeding 21, or decides to "stand" with the total currently held. (Once a player goes bust, he automatically loses.)
  • The dealer reveals his face-down card, and then takes a "hit" as long as his total is 16 or below. Once the dealer reaches 17 or above (possibly going bust), he stands. Note that the dealer stands if using an ace as 11 points gives him a total between 17 and 21. For example, if he has an ace and a 6, he stands with 17. A dealer holding an ace will count is as one point if counting it as 11 would cause him to exceed 21.
  • If the player has a total higher than the dealer, and has not gone bust, he wins. If the dealer has gone bust, but the player has not, the player wins regardless of his total.

Some very savvy players will attempt to mentally keep track of what cards have already been played, to determine when the chance of winning may be more or less favorable. In our case, the player has excellent memorization skills, and knows exactly how many of each value card remain in the deck. You are given the state of the deck, the value shown on the dealer's face-up card, and the two cards that have been dealt to the player.

The i-th element of deck contains the number of (i+1)-value cards remaining in the deck. A value of 1 is an ace, and all 10-value cards are considered equivalent in this game. (The unknown face-down card held by the dealer is one of the cards remaining in the deck.) dealer is the value of the face-up card shown by the dealer. player contains exactly two elements, and is the values of the two cards held by the player.

Assuming the player chooses to hit or stand optimally, return the probability that the player wins the hand.

Definition

Class:
CardCounter
Method:
winningChance
Parameters:
int[], int, int[]
Returns:
double
Method signature:
double winningChance(int[] deck, int dealer, int[] player)
(be sure your method is public)

Notes

  • In an actual casino setting, there may be other rules such as surrender, split, double-down, and dealer-peek, which don't apply for purposes of this problem. Only those rules explained in the problem here are used.
  • Also, specifically, a 2-card 21, also known as a blackjack, has no special meaning in this problem.
  • The constraints guarantee that there are enough cards left in the deck to play out the hand.

Constraints

  • deck will contain exactly 10 elements.
  • Each element of deck will be between 0 and 4, inclusive.
  • dealer will be between 1 and 10, inclusive.
  • player will contain exactly 2 elements.
  • Each element of player will be between 1 and 10, inclusive.
  • The total number of cards remaining in the deck will be no more than 16.
  • The total value of all cards showing and remaining in the deck will be at least 56.

Examples

  1. {4,4,4,0,0,0,0,0,0,4}

    1

    {1,1}

    Returns: 0.4367347731633445

  2. {4,4,1,1,1,1,1,1,1,1}

    1

    {1,1}

    Returns: 0.5120718730123492

  3. {0,0,0,0,0,0,0,4,0,4}

    9

    {9,9}

    Returns: 0.5

    Here, the player already has 18. Knowing that only 8s and 10s are left, there would be no reason to take a hit. Instead, we hope for the best. If the dealer's down-card is an 8, we will win (18 beats 17), but if it's a 10, we will lose (18 loses to 19). There's an equal probability of either.

  4. {0,0,0,0,0,0,0,4,0,4}

    10

    {5,5}

    Returns: 0.2857142857142857

    Note that the dealer here can't possibly go bust based on the remaining cards, so you're only chance to win is to try to win by getting a good total hand. You need to draw one of the 10s (4/8 chance), and the dealer's face-down card needs to be one of the 8s (4/7 chance). So, your chances of winning are 4/8 * 4/7 = 2/7.

  5. {0,0,0,4,0,0,0,0,0,4}

    10

    {10,7}

    Returns: 0.5

    You have some choice here whether to hit or stand. If you take a hit, you have a 0.5 chance of getting a 4, leaving you with a 21 which will definitely win. If you stand, you need to have the dealer's down-card be a 4 (0.5), making a total of 14, which requires him to hit. He then needs to draw a 10 (4/7) to go bust in order for you to win, so standing only gives you a 2/7 chance of winning. The peculiar set of cards remaining in the deck makes hitting the far better choice.

  6. {0,0,2,2,2,2,2,2,2,2}

    6

    {10,10}

    Returns: 0.7348901098901098

    The player has 20, and the dealer is holding a 6, which is a terrible position for the dealer.

  7. {0,0,0,0,0,0,0,4,4,4}

    6

    {10,2}

    Returns: 1.0

    The dealer will go bust no matter what happens, so just stand, even with only 12.

  8. {1,0,0,0,0,2,1,0,0,2}

    10

    {9,1}

    Returns: 0.5

  9. {1,0,0,0,0,0,0,1,1,1}

    10

    {10,10}

    Returns: 0.5

    If the player hits, he has a 0.25 chance of getting the ace and reaching 21, after which the dealer will have 18, 19, or 20 and lose. If the player stands, the dealer has an equal chance of 21(10+A), 18, 19, or 20, with the player winning in 2 of the 4 cases. The player is best to stand, and wins the hand with a 0.5 chance.

  10. {3, 2, 1, 1, 1, 1, 1, 1, 2, 3}

    2

    {2, 2}

    Returns: 0.39036586016496727

    Added by espr1t.

  11. {4, 3, 2, 1, 0, 0, 0, 1, 2, 3}

    1

    {1, 1}

    Returns: 0.36341708522958516

    Added by espr1t.

  12. {4, 3, 2, 1, 0, 0, 0, 1, 2, 3}

    2

    {1, 1}

    Returns: 0.5362425117871547

    Added by espr1t.

  13. {4, 3, 2, 1, 0, 0, 0, 1, 2, 3}

    2

    {2, 2}

    Returns: 0.44142605575641286

    Added by espr1t.

  14. {0, 0, 0, 0, 0, 0, 0, 0, 4, 4}

    10

    {9, 9}

    Returns: 0.0

    Added by espr1t.

  15. {2,2,1,2,1,2,1,2,1,2}

    8

    {5,7}

    Returns: 0.4159676434676435

  16. {1,2,2,2,2,0,1,2,2,2}

    9

    {10,5}

    Returns: 0.2825292762792762

  17. {2,0,1,3,3,3,1,1,1,1}

    6

    {7,8}

    Returns: 0.4839375901875902

  18. {0,0,1,4,1,0,1,2,4,0}

    4

    {1,4}

    Returns: 0.45615773115773117

  19. {0,2,2,2,1,2,1,3,2,1}

    9

    {3,2}

    Returns: 0.42779220779220783

  20. {1,2,1,2,3,2,0,3,1,1}

    4

    {6,6}

    Returns: 0.433052664927665

  21. {2,3,0,4,1,1,2,1,1,1}

    2

    {10,4}

    Returns: 0.4076102138602139

  22. {1,4,1,1,1,1,0,2,1,4}

    2

    {7,4}

    Returns: 0.5655915149665149

  23. {2,1,1,1,0,0,1,2,4,3}

    2

    {6,9}

    Returns: 0.3356238206238206

  24. {2,1,2,1,0,3,2,1,3,1}

    5

    {7,5}

    Returns: 0.49840395715395713

  25. {0,3,4,2,0,1,1,1,0,3}

    8

    {4,7}

    Returns: 0.5061930661930663

  26. {1,4,2,1,1,1,0,1,1,2}

    6

    {6,7}

    Returns: 0.3574389235103521

  27. {2,1,1,3,4,0,2,1,1,1}

    8

    {5,6}

    Returns: 0.477584151959152

  28. {0,3,1,4,3,2,0,0,2,1}

    8

    {1,2}

    Returns: 0.6057542457542457

  29. {4,1,2,0,3,1,2,2,0,1}

    8

    {7,8}

    Returns: 0.3342707986457986

  30. {0,2,0,3,2,1,3,0,3,2}

    10

    {2,10}

    Returns: 0.41832264957264953

  31. {2,1,0,0,4,1,2,2,2,1}

    3

    {6,7}

    Returns: 0.40682021682021674

  32. {0,3,1,1,3,2,2,2,2,0}

    7

    {7,3}

    Returns: 0.40981768231768234

  33. {2,0,3,1,4,1,1,0,2,0}

    9

    {5,6}

    Returns: 0.4353341103341103

  34. {4,1,0,2,0,1,1,2,2,3}

    2

    {2,6}

    Returns: 0.4046716593591594

  35. {3,0,1,2,2,3,0,1,1,3}

    3

    {1,5}

    Returns: 0.5041865599678099

  36. {4,1,4,3,1,0,0,0,1,1}

    1

    {10,6}

    Returns: 0.49742294742294746

  37. {2, 2, 2, 2, 2, 2, 2, 2, 0, 0 }

    3

    {2, 2 }

    Returns: 0.46003436708793843

  38. {2, 0, 2, 0, 2, 1, 2, 2, 2, 1 }

    1

    {1, 3 }

    Returns: 0.43372652479795337

  39. {0, 0, 0, 0, 0, 0, 0, 1, 4, 0 }

    1

    {10, 9 }

    Returns: 0.0

  40. {4, 4, 1, 1, 1, 1, 1, 1, 1, 1 }

    2

    {2, 2 }

    Returns: 0.4595101733583877

  41. {3, 2, 1, 1, 1, 1, 1, 4, 1, 1 }

    1

    {1, 2 }

    Returns: 0.4967371564172159


This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2024, TopCoder, Inc. All rights reserved.
This problem was used for: