
/*
 * NumberGame2.java
 *
 * A random number guessing game.
 *
 * Author: Eli Saracino 
 * Date: 6/30/2017
 */

import java.util.Scanner;

public class NumberGame2 {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        
        /*
         * Write the code to handle getting the difficulty level here.
         */
        int difficulty = showMenu(input);

        int min = 1;
        int max;
        int guessesAllowed;

        if (difficulty == 1) {
            max = 10;
            guessesAllowed = 4;
        } else if (difficulty == 2) {
            max = 50;
            guessesAllowed = 5;
        } else {
            guessesAllowed = 8;
            max = 500;
        }
        
        //Calculate our random number
        int secretNumber = getRandom(min, max);
        
        /*
         * Our guessing game:
         * loop until the user runs out of guesses
         * or guesses the correct answer.
         */
        int numGuesses = 0;
        int guess;
        do{

            //Get guess from user
            numGuesses++;
            guess = getGuess(input);
            boolean rightGuess = evalGuess(guess, secretNumber); //returns true if the guess was correct.
            
            
            //based on it, print as necessary
            if(rightGuess){ //if(rightGuess == true) is equivalent
                if(numGuesses == 1){
                    System.out.println("Wow! Are you cheating?");
                }
                
                System.out.println("You win! Good guessing");
                break;
            }
            
        } while(numGuesses < guessesAllowed);
        
        /*
         * If the last guess input by the user isn't the secretNumber,
         * we know the user must have lost!
         */
        if(guess != secretNumber){
            System.out.println("Game over! The number was " + secretNumber + "!");
        }

    }

    /*
     * Return a random integer between min and max.
     */
    public static int getRandom(int min, int max) {
        return (int)(Math.random() * (max - min + 1)  + min);
    }
    
    /*
     * Prints the opening menu to the game and returns
     * the user input for the difficulty of the game they
     * want to play.
     */
    public static int showMenu(Scanner input){
        System.out.println("Welcome to NumberGame!");
        
        System.out.println("Available levels of difficulty:");
        System.out.println("\t1. easy");
        System.out.println("\t2. normal");
        System.out.println("\t3. hard");
        
        System.out.print("your choice (1-3): ");
        
        return input.nextInt();
    }
    
    /*
     * Prompts the user to enter a guess and returns that number
     */
    public static int getGuess(Scanner input){
        System.out.println("Please enter your guess: ");
        int guess = input.nextInt();
        
        return guess;
    }
    
    /*
     * Evaluates the correctness of the user's guess. If 
     * the user guessed wrong, prints a hint for them and returns
     * false. Otherwise, returns true to denote that the user
     * has won the game.
     */
    public static boolean evalGuess(int guess, int secretNumber){
        if(guess > secretNumber){
            System.out.println("Your guess is too high!");
            return false;
        }
        else if(guess < secretNumber){
            System.out.println("You guess is too low!");
            return false;
        }
        else{
            return true;
        }
    }
    
    
    
    
    
    
    
    
    
}
