|
Do you need help with your Java programming?
Click here for instant help with your Java code. |
Java Source Code for The HiLo Game
The purpose of the HiLo (or Hi-Lo) game is to guess the right number in a certain number of attempts. If the guess is too high the word HI is printed out and if the guess is too low the word LO is printed out. In this example you have six attempts to guess the right number. The code consists of two classes, one main class and one class that contains the actual game logic. The HiLo class generates its secret number, between 1-100, with the java.util.Random class. Here's a short description of every method in the HiLo class: HiLo() - the constructor. Creates the Random object and the stream to read user input. Start() - the entry point to the game, loops until the user don't want to play anymore. describeRules() - prints out the rules for the HiLo game. generateSecretNumber() - generates the number to guess. playGame() - loops at the most 6 times and get the input from the user. If the guess was correct the loop stops. prompt() - asks the user if he / she wants to play again and take care of input. getNextGuess() - gets the next guess from the user. |
package hilo; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Random; /** * The hilo game * @author www.javadb.com */ 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(100) + 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 six 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 <= 100) { inputOk = true; } else { System.out.println("Number is not between 1 and 100 (" + 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; /** * * @author www.javadb.com */ public class Main { /** * @param args the command line arguments */ public static void main(String[] args) { HiLo hiLo = new HiLo(); try { hiLo.start(); } catch (IOException e) { e.printStackTrace(); } } } |
The output from a game could look 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 100, inclusive. The maximum number of tries allowed in a game is six. Guess the secret number: 50 HI Guess the secret number: 34 LO Guess the secret number: 40 LO Guess the secret number: 45 HI Guess the secret number: 43 HI Guess the secret number: 42 HI Sorry, you didn't guess the right number in six attempts. The secret number was 41 |
| Do you know your Java? | |
| Take a Ten-Question-Java-Quiz! | |
Search for code examples on this site
