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

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question
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.
Transcribed Image Text: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.
Expert Solution
Step 1

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

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY