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".
Java
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".
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.
Step by step
Solved in 4 steps with 2 images