Java Bootcamp – Workbooks and Challenges
)}

Blackjack

Blackjack is the most popular table game at casinos. You're going to build a simple version of Blackjack.

Part 1: Starting the game

• In Blackjack, the player plays against the dealer.

• The dealer gives the player two cards faceup.

• The dealer also starts with two cards. But, only one of the dealer's cards is faceup. The other is facedown.

Part 2: During the game

• 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 cards total up to 17. At 17 points or higher, the dealer must stay.

• 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.

Full output:

Launch the challenge

From `Java Bootcamp Resources` -> `Module 1` -> `5. Loops`, open `Challenge 5`.

Define the `drawRandomCard` function.

``````    /**
* Function name -- drawRandomCard
* @return (int)
*
* Inside the function:
*   1. Gets a random number between 1 and 13.
*   2. Returns a card.
*/
``````

Make a function that receives a card number and returns the `String` drawing. You can grab the cards from `cards.txt`. Use the doc comment inside the workbook as a guide.

Write code that waits for the user to press enter.

When the game starts, the dealer will give you two cards faceup. Get the code to deal you two random cards and print them:

``````-- Get two random cards.
-- Print them: (﻿new line﻿) You get a (﻿new line﻿) <random card> and a <random card>``````

After you print your cards, print the hand value. The hand value is the sum of your card values.

``your total is: <hand value>``

In blackjack, the face cards (Jack, Queen, and King) have values of 10.

Hint: look into `Math.min`.

Example output:

The dealer gets two cards as well. Print the dealer's first card, and print the next one facedown. You can find the `String` for a facedown card being returned from a function called: `faceDown()`.

``````1. print:

(﻿new line﻿) The dealer shows (﻿new line﻿) <random card> and
has a card facing down (﻿new line﻿) <facedown card>﻿;

2. print:
(﻿new line﻿) The dealer's total is hidden``````

That's all for part 1. Your final output should appear as follows:

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.