Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This question has been answered twice on chegg but its wrong. Please read the di

ID: 3865195 • Letter: T

Question

This question has been answered twice on chegg but its wrong. Please read the directions carefully and do not use buffer read.

Hi-Lo Game (with Dice) In this homework, you will be writing a program, which repeatedly asks the user if the next roll will be higher or lower than the last dice roll. You will continually play until the user types quit on a line by itself. The game will remember how many times you were successful and how many times you tried and report the ratio of success to tries after the user quits In addition, the game will maintain persistence: remembering you and how many times you've played even after exiting (by maintaining a file). You will need to ask the user for their user name prior to playing and use that to record their results in a file. If the user has played before, the number of successful tries and the number of tries is in a file named after the user with a .txt extension. If not, we start at zero for that user. Once the user quits, you need to save the new results into the correct file. 1. (10%) Your dialog should be clear each time you ask for something what you are asking for and output the result. Note: you should also inform the user what you consider correct and what w end the for the user 2. (20%) Logic reading/determining the hi/lo/quit 3. (20%) Logic providing the dice rolls (note: you'll need a dice roll to begin) 4. (30%) Logic providing the persistence which includes: a. Determine if the file exists b. Opening the file, reading the number correct and the number of tries (each on their own line c. Once finished, rewriting the file so that it contains current data 5. (20%) The logic providing the rest of the result (managing the play, keeping score, calculations) 6. Name your program: DiceHILo.java

Explanation / Answer

package hilo;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Random;

/**
* The hilo game
*/
public class HiLo {

private Random generator;
private int generatedNumber;
private int numberOfAttempts;
BufferedReader reader = null;

public HiLo()
{
generator = new Random();
reader = new BufferedReader(new InputStreamReader(System.in));
}

public void start() throws IOException {

boolean wantToPlay = false;
boolean firstTime = true;

do {
System.out.println();
System.out.println();
System.out.println("Play a game of Hi and Lo?");
if (wantToPlay = prompt()) {
generatedNumber = generateSecretNumber();
numberOfAttempts = 0;
if (firstTime) {
describeRules();
firstTime = false;
}
playGame();
}

} while (wantToPlay);

System.out.println();
System.out.println("Exiting program...");
reader.close();
}

private void describeRules() {
System.out.println();
System.out.println("Rules:");
System.out.println("Guess the computer-generated secret number in the least number of tries.");
System.out.println("The secret number is an integer between 1 and 100, inclusive.");
System.out.println("The maximum number of tries allowed in a game is six.");
System.out.println();
System.out.println();
}

private int generateSecretNumber() {
return (generator.nextInt(200) + 1);
}

private void playGame() throws IOException {

while (numberOfAttempts < 6) {

int guess = getNextGuess();

if (guess > generatedNumber) {
System.out.println("HI");
} else if (guess < generatedNumber) {
System.out.println("LO");
} else {
System.out.println("You guessed the right number!");
return;
}
numberOfAttempts++;
}

System.out.println("Sorry, you didn't guess the right number in 6 attempts.");
System.out.println("The secret number was " + generatedNumber);

}

private boolean prompt() {

boolean answer = false;

try {

boolean inputOk = false;
while (!inputOk) {
System.out.print("Yes / No : ");
String input = reader.readLine();
if (input.equalsIgnoreCase("yes")) {
inputOk = true;
answer = true;
} else if (input.equalsIgnoreCase("no")) {
inputOk = true;
answer = false;
} else {
System.out.println("Invalid input (" + input + "), try again.");
}
}

} catch (IOException e) {
e.printStackTrace();
System.exit(-1);
}

return answer;
}

private int getNextGuess() throws IOException {

boolean inputOk = false;
int number = 0;
String input = null;
while (!inputOk) {
try {

System.out.print("Guess the secret number: ");
input = reader.readLine();
number = Integer.parseInt(input);
if (number >= 1 && number <= 200) {
inputOk = true;
} else {
System.out.println("Number is not between 1 and 200 (" + number + ")");
}
} catch (NumberFormatException e) {
System.out.println("Invalid input (" + input + ")");
}
}

return number;
}
}

The main class creates a new instance of the HiLo class and calls its start() method:-)


package hilo;

import java.io.IOException;

public class Main
{

public static void main(String[] args)
{
HiLo hiLo = new HiLo();
try
{
hiLo.start();
}
catch (IOException e)
{
e.printStackTrace();
}
}

}

The output like this......

Play a game of Hi and Lo?
Yes / No : yes

Rules:
Guess the computer-generated secret number in the least number of tries.
The secret number is an integer between 1 and 200, inclusive.
The maximum number of tries allowed in a game is six.


Guess the secret number: 60
HI
Guess the secret number: 34
LO
Guess the secret number: 40
LO
Guess the secret number: 45
HI
Guess the secret number: 53
HI
Guess the secret number: 32
HI
Sorry, you didn't guess the right number in six attempts.
The secret number was 41

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote