Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Done #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/main/java/BurnDataStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() { }
Expand Down
1 change: 1 addition & 0 deletions src/main/java/DescentEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
14 changes: 10 additions & 4 deletions src/main/java/OnBoardComputer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

}
}
22 changes: 12 additions & 10 deletions src/main/java/Simulation.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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;
}

Expand Down Expand Up @@ -66,6 +65,7 @@ public int runSimulation(BurnStream burnSource) {
}
}
printString(vehicle.checkFinalStatus());
status = vehicle.getStatus(burnInterval);
if (status != null) {
return status.getStatus();
}
Expand All @@ -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);
}

}
56 changes: 36 additions & 20 deletions src/main/java/Vehicle.java
Original file line number Diff line number Diff line change
@@ -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) */

Expand All @@ -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 = "";
Expand All @@ -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) {
Expand All @@ -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;
}

}
31 changes: 21 additions & 10 deletions src/test/java/SimulationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}

}