Implement a “Hand” class to keep track of a hand of cards in Blackjack. This Hand class will rely on the Card class you have already created (as a Hand is composed of individual cards). The Hand will simulate the actions of a Blackjack player given a hand of cards.

The Hand class will incorporate the following functionality:

  • It will request an initial deal. This will result in two cards being requested from the console.
  • It will keep track of the number of cards and the value of the Blackjack hand. Keep in mind that if there are one or more aces in the hand, the hand can have a primary and alternate value. The class must keep track of both values (see the below Rules).
  • The class constructor should accept a “stay” threshold value. If the value of the hand is at or above this value, the class will stay (e.g., ask for no additional hits).
  • Based on the value of the hand, the class will either accept additional “hits” (after the initial deal) or elect to stay with the hand it currently has. This decision will be based on the “stay” value discussed above.
  • After the initial deal and each subsequent hit, the class will print out the values (both primary and alternate of the hand).
  • If a hit results in an unavoidable bust (e.g,, both primary and alternate hand values exceed 21), then the hand will indicate bust and report its final value.
  • For now, you can process each card one at a time. There is no need to keep track of all the individual cards in a hand, only the cumulative values of the hand[1]. You will need to keep track of the presence of some cards (like those needed to declare Blackjack).
  • After a hand is over, it will be capable or resetting the hand instance if another hand is desired.

Note that once initialized, the Hand is autonomous; that is, it decides what it wants to do based on the cards it is dealt. The user is responsible for entering the individual cards using the card format used in Project #4.

Rules:

  • Print out hand value after the initial (2 card) hand is input.
  • Update (and output) hand value after each subsequent card is input.
  • Note that Ace card can have a value of 1 or 11—as such, the user has a choice of two different values. If the value of the hand exceeds 21 with Ace=11, then the lower value (Ace = 1) is chosen. If two hand values, choose the greater one if a further hit is declined.
  • If the value of the hand is at or above the stay value (without a bust), then print out the value the hand and indicate that the player elects to stay.
  • Blackjack occurs when the initial deal results in a jack and an ace.
  • If Blackjack or 21 is achieved, then print out value and halt hand.
  • If value of hand exceeds 21, then print out value and indicate BUST.
  • After each hand ends, determine if user wants to quit or play again.
  • Output an error message if an entry is invalid.

Sample Program Run (for 3 consecutive hands):

>Welcome to Blackjack!

>You have elected a stay value of 18

>Do you want to play a hand? [Y]

>Please input Card #1: [J-H]

>Please input Card #2: [A-S]

>Congratulations--you have Blackjack!

>Hand is over

>Do you want to play another hand? [Y]

>Please input Card #1: [3-D]

>Please input Card #2: [A-S]

>The hand is worth 4 or 14

>The hand elects a hit

>Please input Card #3: [A-D]

>The hand is worth 5 or 15

>The hand elects a hit

>Please input Card #4: [8-C]

>The hand is worth 13

>The hand elects a hit

>Please input Card #5: [10-H]

>The hand is worth 23

>The hand has busted

>Do you want to play another hand? [Y]

>Please input Card #1: [10-D]

>Please input Card #2: [5-*]

>Invalid card suit – try again

>Please input Card #2: [5-S]

>The hand is worth 15

>The hand elects a hit

>Please input Card #3: [3-C]

>The hand is worth 18

>The hand elects to stay

>Do you want to play another hand? [N]

>Thanks for playing Blackjack! Come again.

Class Testing

This program will have three classes: HandTest, Hand, and Card (from #4). The HandTest class should be little more than a main() method that creates a Hand class instance and starts the game. It should also prompt the user if they want to play again.

To develop this program, you do not need a complete understanding of Blackjack; however, one can be found here. Be sure you thoroughly test your program to make sure it works on all legal combinations of cards that might be in a hand. If you have any questions about this assignment, please contact me immediately.

Pseudocode[2]:

booleancontinue_game, continue_hand, card_valid;

inthand_value, alt_hand_value, stay_value;

public static final intbust_limit = 21;

-- Initialize all values

while (continue_game)

{

-- Reset hand values;

while (continue_hand)

{

-- Prompt for the next card

-- Get next card input

-- Validate card input

if (card_valid)

{

-- Compute value of hand (primary & alternate)

-- Report hand value

if (getMinHandValue() > bust_limit) then

{

-- Report Bust

-- continue_hand = false;

}

else

{

-- Determine if a “hit” is desired

-- Set the continue_hand variable accordingly

}

}

else

{

-- Report input error

}

}

-- Determine if player wants to continue the game

-- Set the continue_game variable

}

[1] For project #6, the Hand class will be updates to use arrays (Chapter 6). For that project, you will need to list the rank and suit of every card in the hand at any given time.

[2] The below pseudocode is independent of how functionality is partitioned among classes.