a) In the Hi-Lo game, the player begins with a score of 1000. The player is prompted for the number of points to risk and a second prompt asks the player to choose either High or Low. The player's choice of either High or Low is compared to random number between 1 and 13, inclusive. If the number is between 1 and 6 inclusive, then it is considered "low". A number between 8 and 13 inclusive is "high". The number 7 is neither high nor low, and the player loses the points at risk. If the player had guessed correctly, the points at risk are doubled and added to the total points. For a wrong choice, the player loses the points at risk. Create a HiLo application based on this specification. Application output should look similar to: High Low Gane RULES Nunbers 8 through 13 are high Nunber ? is neither high or low You have 1800 points. Enter points to risk: 500
![a) In the Hi-Lo game, the player begins with a score of 1000. The player is prompted for the
number of points to risk and a second prompt asks the player to choose either High or
Low. The player's choice of either High or Low is compared to random number between
1 and 13, inclusive. If the number is between 1 and 6 inclusive, then it is considered
"low". A number between 8 and 13 inclusive is "high". The number 7 is neither high
nor low, and the player loses the points at risk. If the player had guessed correctly,
the points at risk are doubled and added to the total points. For a wrong choice, the
player loses the points at risk. Create a HiLo application based on this specification.
Application output should look similar to:
High Low Gane
RULES
1 through 6_are lou
Nunbers 8 through 13 are high
Number ? is neither high or low
You have 1000 points.
Enter points to risk: 500
Predict (1=High, 0=Low): 0
Nunber is 4
You win.
Play again? y
You have 2808 points.
b) Modify the application to allow the player to continue until there are 0 points left. At
the end of the game, display the number of guesses the user took before running out
of points.](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F567fd8db-fb82-4c75-b1a0-f30bd5515e7f%2Fdfcb918c-26bd-4c9d-be6a-54bb9f2d2ae1%2Fo873c48_processed.jpeg&w=3840&q=75)
![](/static/compass_v2/shared-icons/check-mark.png)
Java Code:
import java.util.Random;
import java.util.Scanner;
import java.util.regex.*;
public class Main {
public static void main(String[] args) {
final int iPs = 1000;
int p = iPs;
int count = 0;
do {
count++;
display(p);
int pFromUser = getpFromUser("Enter p to risk? ", p);
String option = getHiLoOption("Enter either Hi or Lo? ");
int MN = getMN(1, 13);
if (isWon(option, MN)) {
p += pFromUser;
System.out.println("Won");
} else {
p -= pFromUser;
System.out.println("Lost");
}
} while (p > 0);
report(iPs, count);
System.exit(0);
}
private static void report(int p, int count) {
String prompt = "It took %d number(s) of guess before running out of %,d ps.%n";
System.out.printf(prompt, count, p);
}
private static void display(int p) {
String prompt = "You have %d number of ps to risk.%n";
System.out.printf(prompt, p);
}
public static int getMN(int min, int max) {
int ans = 1;
Random rnd = new Random();
ans = Math.abs(rnd.nextInt() % (max - min)) + 1;
return ans;
}
private static int getpFromUser(String prompt, int p) {
System.out.print(prompt);
Scanner keyboard = new Scanner(System.in);
int ans = keyboard.nextInt();
if (ans > p) {
ans = p;
}
return ans;
}
public static String getHiLoOption(String prompt) {
String ans = "hi";
boolean done = false;
do {
System.out.println(prompt);
Scanner keyboard = new Scanner(System.in);
String option = keyboard.next();
if (isValid(option)) {
ans = option;
done = true;
} else {
System.out
.printf("Invalid option \"%s\" was entered, valid optons are either Hi or Lo. Try again.%n",
option);
}
} while (!done);
return ans;
}
private static boolean isValid(String option) {
boolean ans = false;
Pattern p = Pattern.compile("(hi|lo)", Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(option);
ans = m.find();
return ans;
}
private static boolean isWon(String option, int guessNumber) {
boolean ans = false;
if (option.equalsIgnoreCase("hi") && (guessNumber >= 8)) {
ans = true;
} else if (option.equalsIgnoreCase("lo") && (guessNumber <= 6)) {
ans = true;
}
return ans;
}
}
Trending now
This is a popular solution!
Step by step
Solved in 2 steps
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Computer Networking: A Top-Down Approach (7th Edi…](https://www.bartleby.com/isbn_cover_images/9780133594140/9780133594140_smallCoverImage.gif)
![Computer Organization and Design MIPS Edition, Fi…](https://www.bartleby.com/isbn_cover_images/9780124077263/9780124077263_smallCoverImage.gif)
![Network+ Guide to Networks (MindTap Course List)](https://www.bartleby.com/isbn_cover_images/9781337569330/9781337569330_smallCoverImage.gif)
![Computer Networking: A Top-Down Approach (7th Edi…](https://www.bartleby.com/isbn_cover_images/9780133594140/9780133594140_smallCoverImage.gif)
![Computer Organization and Design MIPS Edition, Fi…](https://www.bartleby.com/isbn_cover_images/9780124077263/9780124077263_smallCoverImage.gif)
![Network+ Guide to Networks (MindTap Course List)](https://www.bartleby.com/isbn_cover_images/9781337569330/9781337569330_smallCoverImage.gif)
![Concepts of Database Management](https://www.bartleby.com/isbn_cover_images/9781337093422/9781337093422_smallCoverImage.gif)
![Prelude to Programming](https://www.bartleby.com/isbn_cover_images/9780133750423/9780133750423_smallCoverImage.jpg)
![Sc Business Data Communications and Networking, T…](https://www.bartleby.com/isbn_cover_images/9781119368830/9781119368830_smallCoverImage.gif)