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

Over Coded Computer #1

Open
wants to merge 5 commits 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.

3 changes: 2 additions & 1 deletion src/main/java/BurnDataStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ 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[] = {100, 100, 200, 200, 100, 100, 0, 0, 200, 100, 100, 0, 0, 0, 0};
int burnArray[] = {100, 100, 200, 200, 100, 100, 0, 0, 200, 100, 100, 0, 200, 200, 100};
int burnIdx = -1;

public BurnDataStream() { }
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/BurnInputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@ public int getNextBurn(DescentEvent status) {
while (true) {
try {
int burn = Integer.parseInt(tokens[0]);
return burn;
if(burn >= 0 && burn <= 200) {
return burn;
}
System.out.println("Must Enter a Number (0-200)");
} catch (NumberFormatException e) {
System.err.println("Must Enter a Number (0-200)");
}
tokens = scanner.nextLine().split(" ");
}
}
return 0;
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/DescentEvent.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
public class DescentEvent {
public class DescentEvent {
int Seconds = 0;
int Velocity = 0;
int Fuel = 0;
Expand All @@ -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
13 changes: 13 additions & 0 deletions src/main/java/MainClass.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
public class MainClass {

public static void main(String[] args) {
// create a new Simulation object with a random starting altitude
Simulation game = new Simulation(new Vehicle(Simulation.randomaltitude()));
// create a new BurnInputStream
BurnInputStream burnSource = new BurnInputStream();
// pass the new BurnInputStream to the runSimulation method
game.runSimulation(burnSource);

}

}
59 changes: 58 additions & 1 deletion src/main/java/OnBoardComputer.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,66 @@ public class OnBoardComputer implements BurnStream {
@Override
public int getNextBurn(DescentEvent status) {
int burn = 0;

int nextAlt = nextAltWithVelocity(status.getAltitude(), status.getVelocity(), burn);
int alt = status.getAltitude();
int fuel = status.Fuel;
int vel = status.getVelocity();
int stat = status.getStatus();
boolean done = false;

while(!done){
if(alt < 400 && vel <200){
if(alt < 200 && vel < 50){
if(alt < 100 && vel < 25){
if(alt < 15 && vel < 10){
burn = targetVelocity(status.getVelocity(),2);
System.out.println(burn); /*hack!*/
return burn;
}
burn = targetVelocity(status.getVelocity(), 9);
System.out.println(burn); /*hack!*/
return burn;
}
burn = targetVelocity(status.getVelocity(), 24);
System.out.println(burn); /*hack!*/
return burn;
}
burn = targetVelocity(status.getVelocity(), 49);;
System.out.println(burn); /*hack!*/
return burn;
}

if(burn == 200){
System.out.println(burn); /*hack!*/
return burn;
}
if(nextVelocityWithBurn(status.Velocity, burn) < calculateMaxVel(nextAlt)){
System.out.println(burn); /*hack!*/
return burn;
}
burn += 50;
}

System.out.println(burn); /*hack!*/
return burn;
}


public int calculateMaxVel(int altitude) {
return (int) Math.sqrt(200* altitude);
}


public int nextAltWithVelocity(int altitude, int velocity, int burn) {
return altitude - (velocity + 100) + burn;
}

public int nextVelocityWithBurn(int velocity, int burn) {
return velocity + 100 - burn;
}

public int targetVelocity(int velocityCurrent, int velocityTarget){
return velocityCurrent+100-velocityTarget;
}

}
8 changes: 7 additions & 1 deletion src/main/java/Simulation.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,15 @@ public int runSimulation(BurnStream burnSource) {
}

public static void main(String[] args) {
// create a new Simulation object with a random starting altitude

Simulation game = new Simulation(new Vehicle(50000));
//Simulation game = new Simulation(new Vehicle(Simulation.randomaltitude()));
// create a new BurnInputStream
//BurnStream burnSource = new BurnInputStream();
OnBoardComputer burnSource = new OnBoardComputer();
// pass the new BurnInputStream to the runSimulation method
game.runSimulation(burnSource);

}

}
20 changes: 17 additions & 3 deletions src/main/java/Vehicle.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ public class Vehicle {

public Vehicle(int InitialAltitude) {
// initialize the altitude AND previous altitude to initialAltitude
this.Altitude = InitialAltitude;
this.PrevAltitude = InitialAltitude;
}

int Gravity = 100;
Expand Down Expand Up @@ -54,30 +56,42 @@ public String checkFinalStatus() {

public int computeDeltaV() {
// return velocity + gravity - burn amount
return 0;
return this.Velocity + Gravity - Burn;
}

public void adjustForBurn(int burnAmount) {
// set burn to burnamount requested
Burn = burnAmount;
// save previousAltitude with current Altitude
PrevAltitude = this.Altitude;
// set new velocity to result of computeDeltaV function.
Velocity = computeDeltaV();
// subtract speed from Altitude
Altitude -= Velocity;
// subtract burn amount fuel used from tank
Fuel -= Burn;
}

public boolean stillFlying() {
// return true if altitude is positive
if(Altitude>0){
return true;
}
return false;
}
public boolean outOfFuel() {
// return true if fuel is less than or equal to zero
return true;
if(Fuel <= 0){
return true;
}
return false;
}

public DescentEvent getStatus(int tick) {
// create a return a new DescentEvent object
// filled in with the state of the vehicle.
return null;
DescentEvent state= new DescentEvent(tick, Velocity, Fuel, Altitude, this.Flying);
return state;
}

}
69 changes: 69 additions & 0 deletions src/test/java/OnBoardComputerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import junit.framework.TestCase;
import org.junit.Assert;
import org.junit.Test;

public class OnBoardComputerTest {

@Test
public void calcMaxVelocityTest(){

//Given
int altitude = 5000;
int velocity = 1000;
int acceleration = 100;
OnBoardComputer jerry = new OnBoardComputer();


//When
DescentEvent stat = new DescentEvent(10, velocity, 12000, altitude, 0);
int actual = jerry.calculateMaxVel(stat.Altitude);
int expected = 1000;

//Then
Assert.assertEquals(expected,actual);

}

@Test
public void nextAlt(){

//Given
int altitude = 5000;
int velocity = 1000;
int acceleration = 100;
int burn = 200;
OnBoardComputer jerry = new OnBoardComputer();


//When
DescentEvent stat = new DescentEvent(10, velocity, 12000, altitude, 0);
int actual = jerry.nextAltWithVelocity(stat.getAltitude(), stat.getVelocity(), burn);
int expected = 4100;

//Then
Assert.assertEquals(expected,actual);

}

@Test
public void nextVelocity(){

//Given
int altitude = 5000;
int velocity = 1000;
int acceleration = 100;
int burn = 200;
OnBoardComputer jerry = new OnBoardComputer();


//When
DescentEvent stat = new DescentEvent(10, velocity, 12000, altitude, 0);
int actual = jerry.nextVelocityWithBurn(stat.getVelocity(), burn);
int expected = 900;

//Then
Assert.assertEquals(expected,actual);

}

}
8 changes: 5 additions & 3 deletions src/test/java/SimulationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,18 @@ public void runSimulationLanding() {
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
public void runSimulationCrash() {
int[] burns = {0,0,0,0,0};
//int[] burns = {0,0,0,0,0};
int[] burns = {200,200,200,200,200,200,100,200,200,200,150,125,120,100,100,100,104,100,100,100,100};
//values that passed ^^
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
Expand Down