Java Programming: Write a command line game that plays a simple version of blackjack. The program should generate random numbers between 1 and 10 each time the player gets a card. It should keep a running total of the players cards, and ask the player whether or not it should deal another card. Sample output for the game is written below. Your program should produce the same output. The players get two cards to start with. Then they are asked if they want more cards. Players can continue to take as many cards as they like. The goal is to get close to 21 without going over. If the total is greater than 21 we say the player "busted".

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Java Programming:

Write a command line game that plays a simple version of blackjack. The program should generate random numbers between 1 and 10 each time the player gets a card. It should keep a running total of the players cards, and ask the player whether or not it should deal another card. Sample output for the game is written below. Your program should produce the same output.

The players get two cards to start with. Then they are asked if they want more cards. Players can continue to take as many cards as they like. The goal is to get close to 21 without going over. If the total is greater than 21 we say the player "busted".

 

Expert Solution
Step 1

Algorithm:

Step 1 Start.

Step 2 Import the Scanner and Random classes from the java.util package.

Step 3 Create an instance of the Scanner class to read user input from the console.

Step 4 Create an instance of the Random class to generate random numbers.

Step 5 Generate two random numbers between 1 and 10 (inclusive) to represent the first two cards dealt to the player.

Step 6 Calculate the total value of the two cards.

Step 7 Print the two cards and their total value.

Step 8 Initialize a boolean variable called "gameOver" to false to keep track of whether the game has ended.

Step 9 Enter a while loop that continues until the "gameOver" variable is set to true.

Step 10 Prompt the user to enter "y" or "n" to indicate whether they want another card.

Step 11 Read the user's input and convert it to lowercase.

Step 12 If the user entered "y", generate another random number between 1 and 10 (inclusive) to represent the next card.

Step 13 Add the value of the new card to the total value.

Step 14 Print the value of the new card and the updated total value.

Step 15 If the total value exceeds 21, print "Busted!" and set the "gameOver" variable to true.

Step 16 If the user entered "n", set the "gameOver" variable to true.

Step 17 If the user entered anything else, print an error message and prompt them to try again.

Step 18 Print "Game over" when the loop exits.

Step 19 End.

steps

Step by step

Solved in 4 steps with 2 images

Blurred answer
Knowledge Booster
Files and Directory
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education