PROBLEM STATEMENT:
As a person with a severe gambling addiction, you are trying to find the
strategy of play that maximizes your potential earnings. To help with the
discovery of the strategy you need a program to simulate the play of Black Jack.
Assume Black Jack is played as follows:
* There are N players, numbered 0 through N - 1, and a dealer.
* At the beginning of each hand, each player puts a bet on the table.
* A first card is dealt to player 0, then 1, ... then N-1, and finally the
dealer.
* A second card is dealt to everyone in the same order.
* All players who get Black Jack (have exactly 21 after the first two cards)
get 5:2 and are removed from the game.
* Starting with player 0, if the player is still in the game, he "hits"
(requests
more cards from the dealer) until either he "stays" (has at least his hit-stay
threshold points) or "busts" (has more than 21 points). If the player busts,
he loses his bet and is removed from the game. The player is dealt as many
cards as he needs to reach his hit-stay threshold or bust before the next
player goes.
* The dealer then hits until he has at least 17 points.
* Finally, earnings are determined for all players that are STILL IN the game:
1. If the dealer busts (has more than 21 points), all players get 2:1.
2. If the dealer did not bust:
2.1. Players who end with more points than the dealer and did not
bust get 2:1.
2.2. Players who end with the same number of points as the dealer
get 1:1.
2.3. Players who end with fewer points than the dealer lose their bet.
The deck will contain the strings "2", "3", "4", ... "10", "J", "Q", "K", "A"
where number values represent a card with that value, "J" is a Jack, "Q" is a
Queen, "K" is a King, and "A" is an ace.
Points are calculated as follows:
1. Cards 2 - 10, inclusive, are worth their face value points.
2. Jacks, Queens, Kings are worth 10 points.
3. Aces can be worth either 1 or 11 points. A hand containing aces is worth
the largest possible value less than 22. If there is no possible value
less than 22 the player busts.
For example, if a hand is A, 9: the hand is worth 11 + 9 = 20 points.
if a hand is A, 9, 3: the hand is worth 1 + 9 + 3 = 13
points because 11 + 9 + 3 > 21.
if a hand is A, A: the hand is worth 11 + 1 = 12 points.
if a hand is 12 aces, the hand is worth 12 points.
Implement a class BlackJack which contains a method simulatePlay(). The method
takes as parameters:
* String[] deck: representing the cards in the deck where element 0 of deck is
the card on top.
* int N: the number of players.
* int[] hitStayThresholds: The values at which each player stops hitting.
If a player has less than their threshold, the
player hits.
* int[] bets: The amount each player bets on the game.
The method should simulate one hand of the game and return an int[] which is
the amount each player ends up getting on the hand.
DEFINITION:
Class: BlackJack
Method: simulatePlay
Parameters: String[], int, int[], int[]
Returns: int[]
Method Signature (be sure your method is public): int[] simulatePlay(String[]
deck, int N, int[] hitStayThresholds, int[] bets);
The deck will contain the strings "2", "3", "4", ... "10", "J", "Q", "K", "A"
where number values represent a card with that value, "J" is a Jack, "Q" is a
Queen, "K" is a King, and "A" is an ace.
hitStayThresholds[i] is the hit-stay threshold for player i.
bets[i] is the bet for player i.
TOPCODER WILL ENSURE:
* deck has between 1 and 50 cards, inclusive.
* deck contains only Strings "2" through "10", inclusive, and the uppercase
letters
"J", "Q", "K", "A".
* N is between 1 and 6, inclusive.
* hitStayThresholds and bets have N elements.
* Values of hitStayThresholds are between 2 and 21, inclusive.
* Values of bets are between 2 and 1000, inclusive, and are even.
NOTE:
* If there are not enough cards to simulate the simulation, return an empty
int[] (An instantiated int[] with 0 elements).
* Suites do not matter and are not included.
* Unlike real life, cards may occur any number of times in the deck.
* Getting X:1 means for every 1 the player bets, he gets X.
* Losing the bet is equivalent to getting 0:1.
* In determining if a player is at their hit-stay threshold, if there are aces
in the player's hand, use the largest possible point value less than 22. (If
there is no possible point value less than 22, the player busted). See the
examples under "Points are calculated as follows:" above and the last example
below.
* If there are enough cards to complete the simulation, the returned int[]
should have N elements where the 0th element is how much the 0th player gets.
All elements should be greater than or equal to 0, where a value of 0
corresponds to losing the bet.
* Even if all players bust or are dealt 21, the dealer must finish his hand.
* Cards are always dealt from the top of the deck. First deck[0] is dealt,
then deck[1], etc...
EXAMPLES:
If deck = {5, 8, K, Q, 3, A, A, 3, 5, 2, 7, 4},
N = 2,
hitStayThresholds = {15, 17},
bets = {10, 20},
First, two cards are dealt to each player:
Player 0: 5 Q
Player 1: 8 3
Dealer: K A
Then, Player 0 has the opportunity to hit or stay. Q + 5 = 15, which
is the player's threshold, so he stays.
Then, Player 1 has the opportunity to hit or stay. 8 + 3 = 11, which is
less than the player's threshold, so he hits:
Player 1: 8 3 A
The Ace must be worth 1 point, since 11 would cause him to bust. 12
points is still below his threshold so he hits again:
Player 1: 8 3 A 3
15 points is still below the threshold, so he hits again:
Player 1: 8 3 A 3 5
20 is above his threshold so he stays.
The Dealer is above his threshold (always 17), so he stays.
Now winnings are calculated. Both Player 0 and 1 have less than the dealer's
21, so they get 0.
The method returns {0, 0}.
If deck = {5, 8, 3, Q, 3, A, 2, 3, 9, 6, 7, 4, 10, 2, J, J},
N = 3,
hitStayThresholds = {17, 19, 5},
bets = {4, 300, 500}.
First two cards are dealt to each player:
Player 0: 5 3
Player 1: 8 A
Player 2: 3 2
Dealer: Q 3
Player 0 has below 17, so he hits, bringing him to his threshold.
Player 0: 5 3 9 = 17.
Player 1 has 19 (Assume A is 11 because it keeps him under 22), so he
is at his threshold.
Player 1: 8 A = 19.
Player 2 has 5, so he is at his threshold and stays."
Player 2: 3 2 = 5
The dealer is below 17, so he hits bringing him to 19:
Dealer: Q 3 6 = 19.
Now winnings are calculated.
Player 0 lost.
Player 1 tied, so gets 1:1 = 300.
Player 2 lost.
The method returns {0, 300, 0}.
If deck = {A, 7, 10, 10, Q, 8, 10, 5, 10, 8},
N = 3,
hitStayThresholds = {20, 17, 17},
bets = {4, 300, 500}
The first cards are dealt
Player 0: A Q
Player 1: 7 8
Player 2: 10 10
Dealer: 10 5
Player 0 has 21 immediately, so gets 5:2 = 10 and is removed from the game.
Player 1 hits, and busts, so gets 0 and is removed from the game.
Player 2 stays.
The dealer hits, and busts.
Player 2 gets 2:1 = 1000. He is the only remaining player.
The method returns {10, 0, 1000}.
If the deck in the last example was {A, 7, 10, 10, Q, 8, 10, 5, 10},
There would not be enough cards to complete the simulation and the method would
return {}.
If deck = {10, 10, 10, 5, 10},
N = 1,
hitStayThresholds = {21},
bets = {100},
The Player 0 busts, but there are not enough cards to complete the dealer's
hand so the method returns {}.
If hitStayThresholds = {20} instead, the method should return {200}.
If deck = {A, 2, A, A, 3, 4, K, 9, A, 10, 5, 5, 10},
N = 2,
hitStayThresholds = {17, 17},
bets = {100, 200}.
After dealing:
Player 0: A A
Player 1: 2 3
Dealer: A 4
Player 0 hits because he has 12 points. (The first A can be 11 and
the second can be 1, keeping him under 22 points.)
Player 0: A A K.
The first player hits again because now both A are 1.
Player 0: A A K 9.
And 21.
Player 1 hits:
Player 1: 2 3 A
He hits again.
Player 1: 2 3 A 10
He hits again
Player 1: 2 3 A 10 5
Finally the dealer hits:
Dealer: A 4 5
Both players beat the dealer so the method returns {200, 400}.
If deck = {2, 10, A, 9, A, 10, 6, 10, A},
N = 1,
hitStayThresholds = {17},
bets = {10}
After dealing:
Player 0: 2 A
Dealer: 10 9
Player 0's is worth at most 13 points, which is less than his
threshold so he hits.
Player 0: 2 A A
He is still below his threshold because the largest possible
value of his hand less than 22 is 14, so he hits.
Player 0: 2 A A 10
He hits again because his hand is worth 2 + 1 + 1 + 10 = 14
(note the changing ace value)
Player 0: 2 A A 10 6
Now he stays, with a hand worth 20 points.
The dealer is above his threshold, so he stays.
Player 0 wins, so the method returns {20}.