HIDE NAV

Higher-Lower Game with Methods

The below command line program demonstrates the declaration and use of static methods with both a parameter and return value.

HLGameApp.java


import java.util.Random;
import java.util.Scanner;

public class HlGameApp {
    public static void main(String[] args) {
        int guessValue, difficulty, maxValue, minValue, targetValue, guessesRemaining, totalGuesses;

        printIntroMsg();

        while(true) {
            difficulty = askDifficulty();
            minValue = 1;
            maxValue = (int)(Math.pow(10,difficulty+1));
            switch (difficulty){
                default:
                case 1: //easy
                    totalGuesses = 8;
                    break;
                case 2:
                    totalGuesses = 11;
                    break;
                case 3:
                    totalGuesses = 14;
                    break;
            }

            guessesRemaining = totalGuesses;
            targetValue = generateTargetValue(minValue, maxValue);
            ///////////debug
            System.out.println("HJINT = " + targetValue);
            //////////////
            while (guessesRemaining > 0) {
                printTurnMessage(minValue, maxValue, guessesRemaining);
                guessValue = scanForGuess(minValue,maxValue);
                if (guessValue < targetValue) {
                    System.out.println("Your guess is too low.");
                    guessesRemaining--;
                } else if (guessValue > targetValue) {
                    System.out.println("Your guess is too high.");
                    guessesRemaining--;
                } else {
                    System.out.println("You Got iT!!!");
                    break;
                }
            }
            if(guessesRemaining == 0) {
                System.out.println("You lose! The value was " + targetValue);
            }
            boolean playAgain = askPlayAgain();
            if(playAgain == false) break;
        }
        System.out.println("Thanks for Playing!");
    }
    static void printIntroMsg(){
        System.out.println("----------------------------------");
        System.out.println("THE HIGHER LOWER GUESSY THINGY!");
        System.out.println("how lucky arent you?");
        System.out.println("----------------------------------");
    }
    //return a 1 for easy, 2 for med. 3 for hard
    static int askDifficulty(){
        Scanner s = new Scanner(System.in);
        System.out.println("Select your Difficulty [1] Easy [2] Normal  [3] Tricky");
        int selection;
        try{
            selection = s.nextInt();
            if (selection < 1 || selection > 3) {
                System.out.println("invalid selection, try again");
                selection = askDifficulty();
                return  selection;
            } else return selection;
        } catch (Exception ex){
            System.out.println("invalid selection, try again");
            selection = askDifficulty();
            return  selection;
        }
    }

    static void printTurnMessage(int min, int max, int guesses){
        String msg = String.format("Guess a number between %d and %d. You have %d guesses remaining", min, max, guesses);
        System.out.println(msg);
    }

    //should ask again if out of range or improper input.
    static int scanForGuess(int min, int max){
        Scanner s = new Scanner(System.in);
        int guess;
        try{
            guess = s.nextInt();
            if (guess < min || guess > max) {
                System.out.println("invalid selection, try again");
                guess = scanForGuess(min,max);
                return  guess;
            } else return guess;
        } catch (Exception ex){
            System.out.println("invalid selection, try again");
            guess = scanForGuess(min,max);
            return  guess;
        }
    }

    // generates a random value from min to max (inclusive of both)
    static int generateTargetValue(int min, int max){
        Random rand = new Random();
        int range = max - min + 1;
        int r = rand.nextInt(range) + min;
        return r;
    }

    static boolean askPlayAgain(){
        System.out.println("play again? [y] yes   [n] no");
        Scanner s = new Scanner(System.in);
        String userIn;
        try {
            userIn = s.nextLine();
            if(userIn.toLowerCase().charAt(0) == 'y') return true;
            else if(userIn.toLowerCase().charAt(0) == 'n') return  false;
            else {
                System.out.println("invalid input, try again");
                return  askPlayAgain();
            }
        } catch (Exception ex){
            System.out.println("invalid input, try again");
            return  askPlayAgain();
        }
    }
}