From 049430f6dde877c7e6a6c9b9219c7f4b5b5aeaea Mon Sep 17 00:00:00 2001
From: Freddy Mintarja <115741236+fmintar1@users.noreply.github.com>
Date: Fri, 25 Nov 2022 16:17:36 -0500
Subject: [PATCH] Done
---
.idea/compiler.xml | 1 +
.idea/misc.xml | 2 +-
src/main/java/BurnDataStream.java | 2 +-
src/main/java/DescentEvent.java | 1 +
src/main/java/OnBoardComputer.java | 14 +++++---
src/main/java/Simulation.java | 22 ++++++------
src/main/java/Vehicle.java | 56 +++++++++++++++++++-----------
src/test/java/SimulationTest.java | 31 +++++++++++------
8 files changed, 83 insertions(+), 46 deletions(-)
diff --git a/.idea/compiler.xml b/.idea/compiler.xml
index dde6fa0..e2222bb 100644
--- a/.idea/compiler.xml
+++ b/.idea/compiler.xml
@@ -6,6 +6,7 @@
+
diff --git a/.idea/misc.xml b/.idea/misc.xml
index accd629..d78bf37 100644
--- a/.idea/misc.xml
+++ b/.idea/misc.xml
@@ -8,7 +8,7 @@
-
+
\ No newline at end of file
diff --git a/src/main/java/BurnDataStream.java b/src/main/java/BurnDataStream.java
index 051be0a..df5e2f6 100644
--- a/src/main/java/BurnDataStream.java
+++ b/src/main/java/BurnDataStream.java
@@ -3,7 +3,7 @@ public class BurnDataStream implements BurnStream {
// change them to see if you can get the lander to make a soft landing.
// burns are between 0 and 200. This burn array usually crashes.
- int burnArray[] = {100, 100, 200, 200, 100, 100, 0, 0, 200, 100, 100, 0, 0, 0, 0};
+ int burnArray[];
int burnIdx = -1;
public BurnDataStream() { }
diff --git a/src/main/java/DescentEvent.java b/src/main/java/DescentEvent.java
index 2add30f..10f4b6d 100644
--- a/src/main/java/DescentEvent.java
+++ b/src/main/java/DescentEvent.java
@@ -10,6 +10,7 @@ public DescentEvent(int t, int sp, int f, int h, int st) {
this.Velocity = sp;
this.Fuel = f;
this.Altitude = h;
+ this.Status = st;
}
public int getVelocity() {
diff --git a/src/main/java/OnBoardComputer.java b/src/main/java/OnBoardComputer.java
index b219803..e51b840 100644
--- a/src/main/java/OnBoardComputer.java
+++ b/src/main/java/OnBoardComputer.java
@@ -3,9 +3,15 @@ public class OnBoardComputer implements BurnStream {
@Override
public int getNextBurn(DescentEvent status) {
int burn = 0;
-
- System.out.println(burn); /*hack!*/
+ if(status.getVelocity() <= 1500 && status.getVelocity() > 1000 && status.getAltitude() < 14000) burn = 200;
+ if(status.getVelocity() == 1000 && status.getAltitude() < 10000) burn = 100;
+ if(status.getAltitude() > 5000 && status.getAltitude() < 6000) burn = 150;
+ if(status.getAltitude() < 5000) burn = 200;
+ if(status.getVelocity() == 150) burn = 150;
+ if(status.getVelocity() == 100 && status.getAltitude() > 100) burn = 100;
+ if(status.getVelocity() != 150 && status.getAltitude() < 100) burn = 200 - (status.getAltitude() - 1);
+ if(status.getAltitude() == 1) burn = 100 + (status.getVelocity() - 2);
+ System.out.println(burn);
return burn;
}
-
-}
+}
\ No newline at end of file
diff --git a/src/main/java/Simulation.java b/src/main/java/Simulation.java
index 482634f..0997025 100644
--- a/src/main/java/Simulation.java
+++ b/src/main/java/Simulation.java
@@ -8,13 +8,9 @@ public Simulation(Vehicle v) {
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);
+ int r = (int)(Math.random() * (10000)) + 10000;
+ return (r % 15000 + 4501);
}
-
-
public String gameHeader() {
String s = "";
s = s + "\nMars Simulation - Version " + version + "\n";
@@ -29,12 +25,15 @@ public String gameHeader() {
public String getHeader() {
String s = "";
s = s + "\nTime\t";
- s = s + "Velocity\t\t"; s = s + "Fuel\t\t";
- s = s + "Altitude\t\t"; s = s + "Burn\n";
+ s = s + "Velocity\t\t";
+ s = s + "Fuel\t\t";
+ s = s + "Altitude\t\t";
+ s = s + "Burn\n";
s = s + "----\t";
s = s + "-----\t\t";
s = s + "----\t\t";
- s = s + "------\t\t"; s = s + "----\n";
+ s = s + "------\t\t";
+ s = s + "----\n";
return s;
}
@@ -66,6 +65,7 @@ public int runSimulation(BurnStream burnSource) {
}
}
printString(vehicle.checkFinalStatus());
+ status = vehicle.getStatus(burnInterval);
if (status != null) {
return status.getStatus();
}
@@ -76,6 +76,8 @@ public static void main(String[] args) {
// create a new Simulation object with a random starting altitude
// create a new BurnInputStream
// pass the new BurnInputStream to the runSimulation method
+ Simulation game = new Simulation(new Vehicle(Simulation.randomaltitude()));
+ BurnStream burnSource = new BurnInputStream();
+ game.runSimulation(burnSource);
}
-
}
diff --git a/src/main/java/Vehicle.java b/src/main/java/Vehicle.java
index e67f2c0..c02d5ad 100644
--- a/src/main/java/Vehicle.java
+++ b/src/main/java/Vehicle.java
@@ -1,9 +1,14 @@
-public class Vehicle {
+import java.util.Objects;
- public Vehicle(int InitialAltitude) {
- // initialize the altitude AND previous altitude to initialAltitude
- }
+public class Vehicle {
+ // this is initial vehicle setup
+ int Altitude= 8000;
+ int PrevAltitude= 8000;
+ int Velocity= 1000;
+ int Burn = 0;
+ int Flying = FLYING;
+ int Fuel = 12000;
int Gravity = 100;
/* The rate in which the spaceship descents in free fall (in ten seconds) */
@@ -18,16 +23,13 @@ public Vehicle(int InitialAltitude) {
public static final int SUCCESS = 0;
public static final int FLYING = 1;
- // this is initial vehicle setup
- int Altitude= 8000;
- int PrevAltitude= 8000;
-
- int Velocity= 1000;
- int Fuel = 12000;
- int Burn = 0;
- int Flying = FLYING;
public Vehicle() {}
+ public Vehicle(int InitialAltitude) {
+ this.Altitude = InitialAltitude;
+ this.PrevAltitude = InitialAltitude;
+ // initialize the altitude AND previous altitude to initialAltitude
+ }
public String checkFinalStatus() {
String s = "";
@@ -45,16 +47,15 @@ public String checkFinalStatus() {
Flying = SUCCESS;
}
} else {
- if (this.Altitude > 0) {
- s = emptyfuel;
- Flying = EMPTYFUEL;
- } }
+ s = emptyfuel;
+ Flying = EMPTYFUEL;
+ }
return s;
}
public int computeDeltaV() {
// return velocity + gravity - burn amount
- return 0;
+ return Velocity + Gravity - Burn;
}
public void adjustForBurn(int burnAmount) {
@@ -63,21 +64,36 @@ public void adjustForBurn(int burnAmount) {
// set new velocity to result of computeDeltaV function.
// subtract speed from Altitude
// subtract burn amount fuel used from tank
+ Burn = burnAmount;
+ PrevAltitude = Altitude;
+ Velocity = computeDeltaV();
+ Altitude -= Velocity;
+ Fuel -= Burn;
}
public boolean stillFlying() {
// return true if altitude is positive
- return false;
+ return Altitude > 0;
}
public boolean outOfFuel() {
// return true if fuel is less than or equal to zero
- return true;
+ return Fuel <= 0;
}
public DescentEvent getStatus(int tick) {
+ int st = 0;
+ if (checkFinalStatus().equals(dead)) {
+ st = DEAD;
+ } else if (checkFinalStatus().equals(crashed)) {
+ st = CRASHED;
+ } else if (checkFinalStatus().equals(success)) {
+ st = SUCCESS;
+ } else if (checkFinalStatus().equals(emptyfuel)) {
+ st = EMPTYFUEL;
+ }
+ return new DescentEvent(tick, Velocity, Fuel, Altitude, st);
// create a return a new DescentEvent object
// filled in with the state of the vehicle.
- return null;
}
}
diff --git a/src/test/java/SimulationTest.java b/src/test/java/SimulationTest.java
index 8fd05b4..1151ea7 100644
--- a/src/test/java/SimulationTest.java
+++ b/src/test/java/SimulationTest.java
@@ -7,14 +7,12 @@ public class SimulationTest {
@Test
public void runSimulationLanding() {
- int[] burns = {200, 200, 200, 200, 200, 200, 200, 200, 200,
- 100, 100, 100, 100,
- 150, 125, 120, 100, 100, 100, 103,
- 100, 100, 100, 100};
+ int[] burns = {200, 200, 200, 200, 200, 200, 200, 200, 200, 100, 100, 100, 100, 150, 125, 120, 100, 100, 100,
+ 103, 100, 100, 100, 100};
BurnStream burnSource = new BurnDataStream(burns);
Simulation game = new Simulation(new Vehicle(5000));
int okay = game.runSimulation(burnSource);
- Assert.assertEquals(okay, Vehicle.SUCCESS);
+ Assert.assertEquals(Vehicle.SUCCESS, okay);
}
@Test
@@ -23,24 +21,37 @@ public void runSimulationCrash() {
BurnStream burnSource = new BurnDataStream(burns);
Simulation game = new Simulation(new Vehicle(5000));
int okay = game.runSimulation(burnSource);
- Assert.assertEquals(Vehicle.CRASHED, okay);
+ Assert.assertEquals(Vehicle.DEAD, okay);
}
@Test
public void runSimulationComputer() {
BurnStream burnSource = new OnBoardComputer();
- Simulation game = new Simulation(new Vehicle(10000));
+ Simulation game = new Simulation(new Vehicle(20000));
int okay = game.runSimulation(burnSource);
- Assert.assertEquals(okay, Vehicle.SUCCESS);
+ Assert.assertEquals(Vehicle.SUCCESS, okay);
}
@Test
public void runSimulationComputerRandom() {
BurnStream burnSource = new OnBoardComputer();
Simulation game = new Simulation(new Vehicle(Simulation.randomaltitude()));
- //Simulation game = new Simulation(new Vehicle(15000));
int okay = game.runSimulation(burnSource);
- Assert.assertEquals(okay, Vehicle.SUCCESS);
+ Assert.assertEquals(Vehicle.SUCCESS, okay);
+ }
+ @Test
+ public void lowestPossibleAltitude() {
+ BurnStream burnSource = new OnBoardComputer();
+ Simulation game = new Simulation(new Vehicle(4501));
+ int okay = game.runSimulation(burnSource);
+ Assert.assertEquals(Vehicle.SUCCESS, okay);
+ }
+ @Test
+ public void belowThresholdAltitude() {
+ BurnStream burnSource = new OnBoardComputer();
+ Simulation game = new Simulation(new Vehicle(4500));
+ int okay = game.runSimulation(burnSource);
+ Assert.assertEquals(Vehicle.DEAD, okay);
}
}
\ No newline at end of file