-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathSimulation.java
More file actions
84 lines (73 loc) · 2.79 KB
/
Simulation.java
File metadata and controls
84 lines (73 loc) · 2.79 KB
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
public class Simulation {
private final Vehicle vehicle;
public Simulation(Vehicle v) {
this.vehicle = v;
}
// Mars Simulation Source Code.
static String version = "2.0"; /* The Version of the program */
public static int randomaltitude() {
int max = 20000;
int min = 10000;
int r = (int)(Math.random() * (max - min)) + min;
return (r % 15000 + 4000);
}
public String gameHeader() {
String s = "";
s = s + "\nMars Simulation - Version " + version + "\n";
s = s + "Elon Musk has sent a really expensive Starship to land on Mars.\n";
s = s + "The on-board computer has failed! You have to land the spacecraft manually.\n";
s = s + "Set burn rate of retro rockets to any value between 0 (free fall) and 200\n";
s = s + "(maximum burn) kilo per second. Set burn rate every 10 seconds.\n"; /* That's why we have to go with 10 second-steps. */
s = s + "You must land at a speed of 2 or 1. Good Luck!\n\n";
return s;
}
public String getHeader() {
String s = "";
s = s + "\nTime\t";
s = s + "Velocity\t"; s = s + "Fuel\t\t";
s = s + "Altitude\t"; s = s + "Burn\n";
s = s + "----\t";
s = s + "-----\t\t";
s = s + "----\t\t";
s = s + "------\t\t"; s = s + "----\n";
return s;
}
public void printString(String string) {
// print long strings with new lines the them.
String[] a = string.split("\r?\n");
for (String s : a) {
System.out.println(s);
}
}
// main game loop
public int runSimulation(BurnStream burnSource) {
DescentEvent status = null;
int burnInterval = 0;
printString(gameHeader());
printString(getHeader());
while (vehicle.stillFlying()) {
status = vehicle.getStatus(burnInterval);
System.out.print(status.toString()+"\t\t");
vehicle.adjustForBurn(burnSource.getNextBurn(status));
if (vehicle.outOfFuel()) {
break;
}
burnInterval++;
if (burnInterval % 9 == 0) {
printString(getHeader());
}
}
printString(vehicle.checkFinalStatus());
if (status != null) {
return status.getStatus();
}
return -1;
}
public static void main(String[] args) {
Vehicle ship = new Vehicle(9000);
Simulation newSim = new Simulation(ship);// create a new Simulation object with a random starting altitude
//BurnInputStream bis = new BurnInputStream();// create a new BurnInputStream
OnBoardComputer computer = new OnBoardComputer();
newSim.runSimulation(computer);// pass the new BurnInputStream to the runSimulation method
}
}