package lander;

import java.util.Scanner;

public class LunarLander {

    /**
     * Lunar Lander game.
     * @param args Unused.
     */
    public static void main(String[] args) {
        new LunarLander().run();
    }

    private void run() {
        double fuel;
        double altitude;
        double velocity;
        double fuelToBurn;
        final double FUEL_CONSTANT = 1.0;
        boolean playAgain = true;
        boolean goodAnswer;
        char letter = '?';
        Scanner scanner = new Scanner(System.in);
        
        System.out.println("Welcome to Lunar Lander!");
        while (playAgain) {        
            fuel = 600;
            altitude = 5000;
            velocity = 500;
            fuelToBurn = 0;
            while (altitude >= 0) {
                System.out.println();
                System.out.println("Your altitude is " + altitude + " meters.");
                System.out.println("Your velocity is " + velocity + " meters/second.");
                System.out.println("You have " + fuel + " liters of fuel remaining.");
                if (fuel > 0 ) {
                    System.out.print("How much fuel would you like to burn? ");
                    fuelToBurn = scanner.nextDouble();
                    if (fuelToBurn > fuel) {
                        fuelToBurn = fuel;
                    }
                    if (fuelToBurn < 0) {
                        fuelToBurn = 0;
                    }
                }
                velocity = velocity + 1.6;
                velocity = velocity - FUEL_CONSTANT * fuelToBurn;
                altitude = altitude - velocity;
                fuel = fuel - fuelToBurn;
            }
            if (velocity > 10) {
                System.out.println("CRASH!");
                System.out.println("You have just blasted a crater " +
                                   velocity / 10 + " meters deep.");
            }
            else {
                System.out.println("Congratulations on a safe landing!");
            }
            goodAnswer = false;
            while (!goodAnswer) {
                System.out.print("Would you like to play again? ");
                String answer = scanner.next();
                letter = answer.charAt(0);
                goodAnswer = letter == 'y' || letter == 'Y' ||
                letter == 'n' || letter == 'N';
            }
            playAgain = letter == 'y' || letter == 'Y';
        }
    }
}
