Two people play the game of Count 21 by taking turns entering a 1, 2, or 3, which is added to a running total. The player who adds the value that makes the total reach or exceed 21 loses the game. Create a game of Count 21 in which a player competes against the computer, and program a strategy that always allows the computer to win. On any turn, if the player enters a value other than 1, 2, or 3, force the player to reenter the value. Save the game as Count21.java.

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

I have an assignment that needs to be turn in before monday, here's the question:

 

Two people play the game of Count 21 by taking turns entering a 1, 2, or 3, which is added to a running total. The player who adds the value that makes the total reach or exceed 21 loses the game. Create a game of Count 21 in which a player competes against the computer, and program a strategy that always allows the computer to win. On any turn, if the player enters a value other than 1, 2, or 3, force the player to reenter the value. Save the game as Count21.java.

Here's the code that I'm working on:

 

import java.util.Scanner;
public class Count21 {
public static void main(String[] args) {
// Scanner object to get user input
Scanner scanner = new Scanner(System.in);
// Stores the running total.

//Displays a welcome for the user

public void promptEnterKey(){
System.out.println("Welcome to Count21! \n");
System.out.println("Press \"ENTER\" to continue...");
Scanner scanner = new Scanner(System.in);
scanner.nextLine();
}
// Game ends when running total exceeds 20.
int runningTotal = 0;
// Stores whose turn is it.
// If true, the next turn will be played by the user.
boolean isUserTurn = true;
while (true) {
// Stores the current number for the turn.
int turnNum = -1;
// If the current turn is for the user, get the turn from the user.
// else calculate the winning move for computer.
if(isUserTurn) {
// Prompt the user for his turn, until the user enters a valid
// number (1, 2, or 3)
while(turnNum < 1 || turnNum > 3) {
System.out.print("Its your turn, enter a number of 1, 2, or 3): \n");
turnNum = scanner.nextInt();
if(turnNum < 1 || turnNum > 3) {
System.out.println("Sorry, you can only enter 1, 2, or 3. \n");
}
}
} else {
// The winning move can be calculated by the following ways:
// 1. The user should play the first turn
// 2. The computer should increase the running total
// to the next multiple of 4.
for(int i=1; 4*i <= 20; i++) {
if(runningTotal < 4*i) {
turnNum = 4*i - runningTotal;
break;
}
}
// Printing computer's turn.
System.out.println("Computer turn: " + turnNum);
}
// Adding the current turn to the running total
runningTotal += turnNum;
System.out.println("Running total: " + runningTotal);
// If running total exceeds 20, game ends.
if(runningTotal >= 21) {
break;
}
isUserTurn = !isUserTurn;
}
// Print the winner according to who player last.
if(isUserTurn) {
System.out.println("Sorry you lose.. \n");
} else {
System.out.println("You win! \n");
}
}
}

 

What I want to add is a welcome message while the user can press ENTER or any key button to start the game but I'm having problems of getting it to work any help would be apprecitated.

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 2 images

Blurred answer
Knowledge Booster
Hiring Problem
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