-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
170 lines (144 loc) · 3.7 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
int calculate(int height, int speed, int burn, int gravity) {
return (speed+gravity-burn);
}
int windowcleaner(int step) {
if(step>=24){
printf("\nTime\t");
printf("Speed\t\t");
printf("Fuel\t\t");
printf("Height\t\t");
printf("Burn\n");
printf("----\t");
printf("-----\t\t");
printf("----\t\t");
printf("------\t\t");
printf("----\n");
step=1;
}
else if(step<24){
step++;
}
return step;
}
int randomheight() {
time_t t;
time(&t);
srand((unsigned int)t);
return (rand() % 15000 + 4000);
}
int main(int argc, char *argv[]){
const int gravity = 100; /* The rate in which the spaceship descents in free fall (in ten seconds) */
int height; /* The height of the spaceship. */
int speed; /* The speed of the spaceship. */
int burn; /* The fuel which gets burned this step */
int tensec; /* The time the flight is running for. (in ten second steps) */
int fuel; /* The fuel you have left. (kilogram) */
int prevheight; /* The previous height to compare with actual. (coloured digits) */
int step; /* Counts the steps passed since last output of the collumn names */
char dead[]="\nThere were no survivors.\n\n";
char crashed[]="\nThe Spaceship crashed. Good luck getting back home.\n\n";
char success[]="\nYou made it! Good job!\n\n";
char emptyfuel[]="\nThere is no fuel left. You're floating around like Wheatley.\n\n";
printf("\nLunar Lander - Version 1.0\n");
printf("This is a computer simulation of an Apollo lunar landing capsule.\n");
printf("The on-board computer has failed so you have to land the capsule manually.\n");
printf("Set burn rate of retro rockets to any value between 0 (free fall) and 200\n");
printf("(maximum burn) kilo per second. Set burn rate every 10 seconds.\n"); /*that's wy we have to go with 10 second-steps.*/
printf("Good Luck!\n");
/* Set initial height, time, fuel, burn, prevheight, step and speed properly to difficulty. */
if(argv[1]="-d"){
if(argv[2]="1"){ /* Easy */
speed=1000;
height=randomheight();
fuel=12000;
tensec=0;
burn=0;
prevheight=height;
step=1;
}
if(argv[2]="2"){ /* Medium */
speed=1000;
height=randomheight();
fuel=1000;
tensec=0;
burn=0;
prevheight=height;
step=1;
}
if(argv[2]="3"){ /* Hard */
speed=2000;
height=randomheight() - 2000;
fuel=900;
tensec=0;
burn=0;
prevheight=height;
step=1;
}
}
else { /* Default: Easy */
speed=1000;
height=randomheight();
fuel=12000;
tensec=0;
burn=0;
prevheight=height;
step=1;
}
printf("\nTime\t");
printf("Speed\t\t");
printf("Fuel\t\t");
printf("Height\t\t");
printf("Burn\n");
printf("----\t");
printf("-----\t\t");
printf("----\t\t");
printf("------\t\t");
printf("----\n");
do {
step=windowcleaner(step);
printf("%d0\t", tensec);
printf("%d\t\t", speed);
printf("%d\t\t", fuel);
if(height<prevheight){
printf("\x1b[31m%d\x1b[0m\t\t", height);
}
else if(height==prevheight){
printf("%d\t\t", height);
}
else if(height>prevheight){
printf("\x1b[32m%d\x1b[0m\t\t", height);
}
scanf("%i", &burn);
if(burn<0 || burn>200) { /* If there is a wrong entry */
printf("The burn rate rate must be between 0 and 200.\n");
continue;
}
prevheight = height;
speed = calculate(height, speed, burn, gravity);
height=height-speed;
fuel = fuel-burn;
if(fuel<=0) {
break;
}
tensec++;
}while(height>0);
if(height<=0){
if(speed>10){
printf("%s", dead);
}
else if(speed<10){
printf("%s", crashed);
}
else if(speed>0 || speed<3){
printf("%s", success);
}
}
else if(height>0){
printf("%s", emptyfuel);
}
return 0;
}