Java Bootcamp – Workbooks and Challenges
)}

# Blackjack (part 2)

In part 1, you wrote the logic for starting the game.

In part 2, you will write the logic for playing the game. Recall:

• First, it's the player's turn. The player can decide to hit or stay.

• hit: draw another card.

• stay: do nothing.

• If the player decides to hit, and their hand value exceeds 21, they go bust (lose).

• Once the player decides to stay, the dealer reveals the hidden card.

• Then, the dealer must hit until their total gets to 17. At 17 points or higher, the dealer's turn ends.

• You win if your hand value is higher than the dealer's hand.

• You win if the dealer goes bust (exceeds 21)

• You lose if the dealer's hand value is higher than yours.

Final output:

Make a function that asks the user to hit or stay.

/** * Function name -- hitOrStay * @return (String) * * Inside the function: * 1. Asks the user to hit or stay. * * 2. If the user doesn't enter "hit" or "stay" * Run a while loop * During each run, println: Please write 'hit' or 'stay' * * 3. Returns the user's option */

If the user doesn't enter "hit" or "stay", keep asking them to try again by printing:

The condition for this while loop is kind of tricky. I recommend using a while loop that runs forever and breaking it when it's appropriate. Test your function by calling it from main().

In Task 8, you will keep asking the player to hit or stay. In other words, make a while loop that runs forever. Then, call your function inside the while loop. Every time the player hits, draw a new card and calculate their new total. Then, print:

• (new line) You get a (new line) <show new card>

• your new total is <total>

• Once the player 'stays', break the loop.

While the player is hitting, if they go bust (total exceeds 21) print: "Bust! Player loses". Then, shut down the game with System.exit(0) (System.exit(0) terminates execution).

After the player chooses to stay, it becomes the dealer's turn. First, reveal the dealer's hidden card.

Print:

• (new line) Dealer's turn

• (new line) The dealer's cards are (new line) <dealer card 1> (new line) and a (new line) <dealer card 2>

The dealer must keep "hitting" until their total gets to 17. Every time the dealer hits, print:

• (new line) Dealer gets a (new line) <show new card>

• (new line) Dealer's total is <dealer's total>

At 17 points or higher, end the dealer's turn.

If the dealer's total is higher than 21, print: "Bust! Dealer loses" and end the game with System.exit(0).

If at this point, the program didn't terminate, compare the dealer and player's hand values.

• If the player's hand value is higher, print: Player wins!

• Otherwise, print: Dealer wins!.

Final output:

## Good luck!

##### Related Course: The Complete Spring Boot Development Bootcamp – Become a Java Web Developer

Feedback Summary
4.7
43 students
5

91%
4

2%
3

0%
2

0%
1

7%
Written Reviews
There are no written reviews yet.