We will be developing a simulation of landing a lunar module
We will be developing a simulation of landing a lunar module on the surface of the moon. It\'s a lunar lander game. C++
Lunar Lander Game (Question by Prof. Popyack) Falling Towards the Moon We will be developing a simulation of landing a lunar module on the surface of the moon. It\'s a lunar lander game. Let\'s start with the motion of our lander simply falling to the moon\'s surface. All we need to know to simulate this free fall is the rate of acceleration due to gravity. Because the gravity of the moon is only 1/6 that here on Earth, an object falling on the moon will accelerate at the rate of -1.63 m/s2 Your first task for this is to write a program (from scratch that prompts the user for an initial altitude and an initial velocity. It then updates the altitude and the velocity after a second has passed, and prints out the new values with two decimal values after the decimal place. To do this, we start with an altitude of a meters and a velocity of v meter per second. One second later, the altitude will be a+v. Similarly, if we start with a velocity of v meters per second and an acceleration due to gravity of g meters per second squared, one second later the velocity will be v+g Landing Once you are confident in your calculations, extend your code to run until the lander either lands or crashes. If the speed at which your lander hits the ground is faster than -2m/s it has crashed. When the lander reaches the ground, tell the user if it was safe or crashed. Use the Rockets to slow down Our next step is to allow us to burn fuel to slow our descent. At the end of each iteration (actually before adding the gravity to the velocity), ask the user for a number of fuel units to burn during that second. If we burn f units of fuel, our acceleration during that second will be -1.63+0.1f (as opposed to our normal -1.63). Make sure the user can\'t cheat by burning negative fuel. Finite Fuel A real lander can\'t have unlimited fuel. Ask the user for the amount of fuel on board and make use the user can\'t burn more than they have. Example Run Enter an initial altitude: 10 Enter an initial velocity: 1 Enter the initial fuel amount: 0 After 1 seconds Altitude is 11.00 meters, velocity is 1.00m/s How much fuel do you want to burn? 0 After 2 seconds Altitude is 10.37 meters, velocity is -0.63m/s How much fuel do you want to burn? 0 After 3 seconds Altitude is 8.11 meters, velocity is -2.26m/s How much fuel do you want to burn? 0 After 4 seconds Altitude is 4.22 meters, velocity is -3.89m/s How much fuel do you want to burn? 0 After 5 seconds Altitude is 0.00 meters, velocity is -5.52m/s We crashed!Solution
#include <iostream>
#include <unistd.h>
using namespace std;
double altitude = numeric_limits<double>::infinity();
double velocity;
double fuel;
double g = -1.63; // 1/6 of earth\'s gravity(9.807)
int seconds = 0;
double fuel_burnt; // amount of fuel burnt during 1 second.
// update the speed and altitude
void update(){
if(fuel_burnt>0){
velocity += g+(0.1*fuel_burnt);
fuel_burnt = 0;
}else{
velocity += g;
}
altitude += velocity;
}
// initialize the speed and altitude
void init_update(double a,double v){
altitude = a+v;
velocity = v;
}
int main(int argc, char const *argv[])
{
double a,v;
cout << \"Enter an initial altitude: \";
cin >> a;
cout << \"Enter an initial velocity: \";
cin >> v;
cout << \"Enter an initial fuel amount: \";
cin >> fuel;
while(fuel<0){
cout << \"Enter a valid(>=0) initial fuel amount: \";
cin >> fuel;
}
while(altitude>0){
sleep(1);
seconds++;
if(seconds==1){
init_update(a,v);
}
else{ update();}
// if altitude <0 means it is landed on surface
if(altitude<0){altitude=0;}
cout << \"After \"<< seconds << \" seconds altitude is \" << altitude << \" meters, velocity is \" << velocity << \"m/s\"<<endl;
// no further process once it landed on surface
if(altitude==0)break;
cout << \"How much fuel do you want to burn: \";
cin >> fuel_burnt;
// if remaining fuel is less than the fuel user want to burn.
if(fuel_burnt>fuel){
cout << \"total fuel amount \"<<fuel<<\" is less than \"<< fuel_burnt<<\" you can burn max \"<<fuel<<endl;
cout << \"How much fuel do you want to burn: \";
cin >> fuel_burnt;
}
// if entered fuel to be burnt is negative
if(fuel_burnt<0){fuel_burnt=0;}
fuel -= fuel_burnt;
}
if(velocity<-2){
cout << \"We crashed :(\"<<endl;
}
else{
cout << \"We landed safely :)\" <<endl;
}
return 0;
}
________________________________________________________________
Output
Enter an initial altitude: 10
Enter an initial velocity: 1
Enter an initial fuel amount: 0
After 1 seconds altitude is 11 meters, velocity is 1m/s
How much fuel do you want to burn: 0
After 2 seconds altitude is 10.37 meters, velocity is -0.63m/s
How much fuel do you want to burn: 0
After 3 seconds altitude is 8.11 meters, velocity is -2.26m/s
How much fuel do you want to burn: 0
After 4 seconds altitude is 4.22 meters, velocity is -3.89m/s
How much fuel do you want to burn: 0
After 5 seconds altitude is 0 meters, velocity is -5.52m/s
We crashed :(


