You have to create a class that can support the game of having the user guess a number within a range and telling the user whether their guess is high, low, or correct. Most of the game logic is already given to you in a file named PlayGuessingGame.java. Some utility routines for input are provided in a file named Keyboard.java. You have to create a class named HiLoGame that provides the missing functionality. You have to determine the requirements for HiLoGame by examining the code in PlayGuessingGame.java.
1 /*
2 PlayGuessingGame.java
3 Dave Klick
4 CIS 260
5 2015-01-13
6
7 This program uses the HiLoGame class to play a number
8 guessing game. It is provided as a driver to be used
9 once the HiLoGame class has been implemented.
10
11 Note: This class depends upon the presence of Keyboard.java
12 (or Keyboard.class) in the same directory. Keyboard.java is
13 provided on the course web site.
14 */
15
16 public class PlayGuessingGame {
17 public static void main(String[] args) {
18 int low = 100;
19 int high = 1;
20 int guess = 0;
21 char again = 'Y';
22 String result = "";
23 int status;
24 Keyboard kbd = new Keyboard();
25
26 while (again == 'Y') {
27 HiLoGame game = new HiLoGame(high, low);
28 status = HiLoGame.LOW;
29 System.out.printf("Guess the secret number between %d and %d\n", game.getLow(), game.getHigh());
30 while (status != HiLoGame.CORRECT) {
31 guess = kbd.getInt("Your guess: ", game.getLow(), game.getHigh());
32 status = game.checkGuess(guess);
33 switch (status) {
34 case HiLoGame.LOW: result = "low"; break;
35 case HiLoGame.HIGH: result = "high"; break;
36 case HiLoGame.CORRECT: result = "correct"; break;
37 default: result = "undefined"; break;
38 }
39 System.out.printf("Your guess #%d was %s\n", game.getGuesses(), result);
40 }
41 System.out.println("Congratulations!");
42 System.out.printf("You got the right number in %d guesses.\n\n", game.getGuesses());
43 again = Character.toUpperCase(kbd.getChar("Play again (Y/N)? ", "YyNn"));
44 }
45
46 System.out.printf("\n%d %s played.\n", HiLoGame.getGames(),
47 HiLoGame.getGames() == 1 ? "game was" : "games were");
48 }
49 }
Guess the secret number between 1 and 100 Your guess: 50 Your guess #1 was high Your guess: 25 Your guess #2 was high Your guess: 12 Your guess #3 was high Your guess: 6 Your guess #4 was low Your guess: 9 Your guess #5 was low Your guess: 10 Your guess #6 was correct Congratulations! You got the right number in 6 guesses. Play again (Y/N)? 5 Error: Character entered is not a valid response. Play again (Y/N)? y Guess the secret number between 1 and 100 Your guess: 900 Error: Above maximum value of 100 Your guess: -9 Error: Below minimum value of 1 Your guess: 50 Your guess #1 was high Your guess: 25 Your guess #2 was low Your guess: 37 Your guess #3 was high Your guess: 31 Your guess #4 was high Your guess: 28 Your guess #5 was low Your guess: 29 Your guess #6 was correct Congratulations! You got the right number in 6 guesses. Play again (Y/N)? n 2 games were played.