import java.util.*;  
  
  
public class Main {  
	// variables which stay between games
    static int topScore = 0;  
    static int gamesPlayed = 0;  
  
    public static void main(String[] args) {  
        // caleb b  
        // number recall game
        
        // initialize game requirements        
        int round = 0;  
        int currentScore = 0;  
        int currentLives = 3;  
        boolean isHardMode = false;  
        int maximumNum = 9;  
        List<Integer> nums = new ArrayList();  
        Scanner input = new Scanner(System.in);  
        boolean playing = false;  
        double clearSpeed = 5000;  
		
		// Print starting text
        System.out.println("Welcome to the number recall game!");  
        System.out.println("Each round you will be given a set of numbers to memorize.");  
        System.out.println("The amount of numbers you have to memorize will increase each round.\n");  
        System.out.println("Before we get started, would you like to play Hard Mode?");  
        System.out.println("This will allow 2 digit numbers to be generated, but will double your score");  
        System.out.println("Type 'y' for yes and 'n' for no.");  
        String hardModeInput = input.nextLine();  
        // Hard mode query
        if (hardModeInput.equals("y")) {  
            isHardMode = true;  
            maximumNum = 99;  
        }  
        System.out.println("Starting the game!");  
        clearScreen(); // clear the screen and start the game loop  
        playing = true;  
        
        // game loop
        while(playing) {  
            round++;  //update the round
            // Print game info to player
            System.out.println("Round " + round);  
            System.out.println("Lives: " + currentLives);  
            System.out.println("Score: " + currentScore);  
            if (gamesPlayed > 0 && topScore > 0) {  
	            // if there was a game played before, show the previous top score
                System.out.println("Score to Beat: " + topScore);  
            }  
            System.out.println("Memorize the following numbers:");  
            // generate numbers  
            nums.clear();  
            for (int i = 0; i < round; i++) {  
                nums.add((int) (Math.random() * maximumNum + 1));  
            }  
            // wait 3 seconds;  
            sleep(3000);  
            // print the numbers
            for (Integer num : nums) {  
                System.out.print(num + " ");  
            }  
            System.out.println();  
            sleep(clearSpeed * Math.pow(0.9, round - 1));  // wait the exponentially decreasing clearSpeed before clearing the screen and allowing number input
            clearScreen();  
            System.out.println("Enter the numbers you saw, separated by spaces.");  
            String guess = input.nextLine();  // get the user's guess
            String[] guessArr = guess.split(" ");  // split into individual numbers
            if (guessArr.length != nums.size()) {  
	            // if the player did not enter the correct amount of numbers, automatically remove a life and continue to the next round
                System.out.println("You entered the wrong amount of numbers!");  
                currentLives--;
                sleep(3000);  
            } else {  
	            // else, check if all the numbers are correct. If not, remove a life before continuing to the next round.
                for (int i = 0; i < guessArr.length; i++) {  
                    if (Integer.parseInt(guessArr[i]) != nums.get(i)) {  
                        System.out.println("You entered the wrong number!");  
                        currentLives--;  
                        sleep(3000);
                        break;                    
                    }  
                }  
            } 
	        // Check if the player still has any lives remaining before continuing to the next round.
            if (currentLives == 0) {  
	            // if the player has no more lives, end the game, and print out relevant info.
                playing = false;  
                System.out.println("Game Over!");  
                System.out.println("You made it to round " + round + "!");  
                System.out.println("Your score was " + currentScore + "!");  
                if (currentScore > topScore) {  
                    System.out.println("You beat your previous high score of " + topScore + "!");  
                    topScore = currentScore;  
                }  
                System.out.println("Thanks for playing!");  
                sleep(5000);  
                clearScreen();  
                // add to the games played counter.
                gamesPlayed++;  
            } else {  
	            // if the player has more than 3 lives, add to the score
                currentScore = round;  
                if (isHardMode) {  
                    currentScore = currentScore * 2;  
                }  
            }
            // Next round begins  
        }  
 
		// Repeat the game
        main(args);  
  
    }  
 
	// Quick function to clear the screen on command, just prints 100 blank lines.
    public static void clearScreen() {  
        for (int i = 0; i < 100; i++) {  
            System.out.println();  
        }  
    }  
	
	// Quick function for safe Thread sleeping.
    public static void sleep(double millis) {  
        try {  
            Thread.sleep((long) millis);  
        } catch (InterruptedException e) {  
            e.printStackTrace();  
        }  
    }  
}