CIT 591 Assignment 1: Lunar Lander
Fall 2008, David Matuszek

# Purposes of this assignment:

• To give you practice with:
• input (`Scanner`)
• output (`System.out.print` and `println`)
• `while` loops
• `if` statements, and
• arithmetic (with `double`s)
• tests (with `boolean` values)

# General idea of the assignment:

Lunar Lander is one of the earliest computer games. With a proper choice of initial values, it is quite a bit of fun to play, even as a text-only program.

You are in a lunar module, some distance above the Moon's surface. Gravity is pulling you toward the Moon at an ever-increasing rate of speed. You have a limited amount of fuel to use, and each time you burn fuel, you reduce your speed by a certain amount. If you burn the right amount of fuel at the right time, you can land safely (that is, when you reach the Moon's surface, you are not going too fast).

# The math:

This game is not timed; you can take as long as you want to enter numbers. Each time you enter a number, one second of "game time" will have passed. In other words, 1 turn = 1 second.

At each turn, you are told:

• Your altitude in meters above the moon
• Your velocity toward the Moon in meters/second (this number will be negative if you are moving away)
• How much fuel you have remaining (in kilograms)

You then get to specify:

• How much fuel to burn
• Zero is a legal value (and may be all you have!)
• If you ask to burn more fuel than you have, burn it all

At the end (which you can determine because your altitude becomes zero or negative), you are either congratulated for a safe landing, or told how deep a crater you have blasted into the surface of the moon (based on your velocity).

At each turn, you will need to do the following calculations:

• Your velocity increases by 1.6 meters/second, due to the acceleration of gravity; but in additon,
• Your velocity decreases by an amount proportional to the amount of fuel you just burned (zero at the first turn). That is, velocity decreases by some constant times the amount of fuel; but you have to choose the constant.
• Your altitude decreases by your velocity multiplied by the amount of "time" each turn takes. (Each turn takes 1 second, so we don't actually need to do this multiplication.)
• Your fuel decreases by the amount you burn, but of course you cannot burn more than you have, so the amount of fuel never becomes negative. (If you ask to burn more than you have, burn only as much as you do have, and set the amount remaining to zero.)
After these calculations, you need to determine if you have "landed." You have landed if your altitude is less than or equal to zero (it is highly improbable that you would ever get exactly zero). A safe landing is one where your velocity is not more than 10 meters/second.

Either way, the game ends when you have landed. If you have landed safely, adjust your altitude to be zero before printing out the final numbers, along with a congratulatory message. If you didn't land safely, use your velocity to print out how deep a crater you have just blasted in the lunar surface.

After each game, ask the user if they want to play again. Any response that begins with `'y'` or `'Y'` means "yes," any response that begins with `'n'` or `'N'` means "no," and for any other answer you should ask again.

Note 1: We talk about values using their measurement units, for example, "1.6 meters/second." In our programming, however, we don't use the units, just the numbers, for example `1.6`. It is your responsibility as a programmer to know what units you are using. (The \$125 million 1999 Mars orbiter was lost because Lockheed Martin used English units in its programming, while NASA expected metric units.)

Note 2: There are some numbers that I have not specified, such as your initial altitude, or how much fuel will result in how much change in velocity. You will need to experiment to find numbers that result in a game that is difficult (but not too difficult) to win.

# Things you need to know:

If `scanner` is a `Scanner`, you can read a `double` with `scanner.nextDouble()`. You can read a "word" (any characters bounded by whitespace) with `scanner.next()`. Words are returned as values of type `String`. Whatever you read, remember to save it in a variable.

If `word` is a variable of type `String`, you can ask `word` for its first character with `word.charAt(0)`. This will return a `char` value--that is, it will if there is anything in the String.

In Java, constants (variables whose value can never change) are marked with the keyword `final`, and are written with all capital letters and underscores between words, for example,
`final double ACCELERATION_OF_GRAVITY = 1.6;`

# Details:

As you can imagine, it could take my TAs a lot of time to check out your game. Accordingly, I want you to turn in a file named `readme.txt`, which provides (1) a list of numbers to enter in order to win the game by landing safely, and (2) a list of numbers which will cause you to lose the game. (The latter list should be very easy to find.)

# Due date:

Your program is due before midnight, Thursday September 18. Zip up all files (`.java`, `.class`, and `readme.txt`), and submit via Blackboard.