Lander

Line: Object Line
Hard Difficulty Object Orientation Learning Outcome One Learning Outcome Two Exercise
Type: Exercise
You should have completed:

Virtual Student

This topic leads to:

This is the end of the line

Summary

A classic game from the days of text-only computing, Lander challenged the player to land on a celestial body using only thrust to slow their descent. This is similar to the way the Apollo missions descended to the moon. The difficulty is that the player has limited fuel, so if you descend too slowly you will run out before reaching the surface.

In this exercise you will create a version of that game. For simplicity, only vertical speed and altitude will be considered, and the thrust from the rocket will be on or of. Further it is assumed that all steps and calculations are over a time period of one second.

Task

  1. Create a console application.
  2. Create a SpaceCraft class. Give this class properties for the following data:
    1. Altitude (in meters)
    2. Velocity (in meters per second), negative values will indicate descent towards the surface.
    3. Mass (in Kg) of the spacecraft without fuel.
    4. Fuel (in Kg) remaining.

    Choose carefully what data type(s) to use, and encapsulate and comment these values properly.

  3. Now give this class methods for the following behaviours:
    1. ApplyThrust. This method will burn 10Kg of fuel, or as much as is left, producing a force of 2,000 Newtons per Kg burnt. Calculate the Acceleration this produces (Accel = Force/Mass) and adjust the velocity property accordingly.
    2. ApplyGravity. This method will apply the acceleration due to the force of gravity (approx -10 meters per second per second) to the spacecraft.
    3. Move. This method changes the spacecraft's altitude according to its current velocity. It then checks to see if the altitude is 0 or <0. If so, it then checks to see if the ship was travelling too fast (more than 10 m/s) and crashed, or if it landed safely. Return the result as a code 0 for in flight, 1 for landed, and 2 for crashed.
    4. A constructor which allows the initial values for all the properties to be set.

    Make sure to comment these methods fully.

  4. Now write a program that creates a SpaceCraft with a mass of 1000kg, 500kg of fuel and 500m altitude at a dead stop. Simulate landing this ship, by repeatedly asking the user whether they wish to use the thrust or not. Each turn simulates the passage of one second, and at each turn the ApplyThrust method must be called if the user wants to thrust. ApplyGravity and Move methods must be called each turn regardless and your program will then need to inform the user of the current altitude, velocity and fuel remaining of the craft.
  5. The program will check for a successful or failed landing and report this to the user when it happens, before exiting.

Questions

  1. Can you modify this program to allow the user to select starting values for the weight, fuel, altitude and velocity of the spacecraft, and the strength of gravity?
  2. Can you modify this program to allow the user to select how much fuel to burn each turn?