From f36868a61267b7366593031957bcf8b6c19a1702 Mon Sep 17 00:00:00 2001 From: David Date: Sat, 26 Mar 2022 10:48:44 -0400 Subject: [PATCH 1/5] vehicleDone? --- .idea/compiler.xml | 1 + src/main/java/MainClass.java | 13 +++++++++++++ src/main/java/Simulation.java | 5 +++++ src/main/java/Vehicle.java | 20 +++++++++++++++++--- 4 files changed, 36 insertions(+), 3 deletions(-) create mode 100644 src/main/java/MainClass.java 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/src/main/java/MainClass.java b/src/main/java/MainClass.java new file mode 100644 index 0000000..51299db --- /dev/null +++ b/src/main/java/MainClass.java @@ -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); + + } + +} diff --git a/src/main/java/Simulation.java b/src/main/java/Simulation.java index 482634f..4f55e70 100644 --- a/src/main/java/Simulation.java +++ b/src/main/java/Simulation.java @@ -74,8 +74,13 @@ 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(Simulation.randomaltitude())); // create a new BurnInputStream + BurnStream burnSource = new BurnInputStream(); // pass the new BurnInputStream to the runSimulation method + game.runSimulation(burnSource); + + } } diff --git a/src/main/java/Vehicle.java b/src/main/java/Vehicle.java index e67f2c0..b0e006b 100644 --- a/src/main/java/Vehicle.java +++ b/src/main/java/Vehicle.java @@ -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; @@ -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 status = new DescentEvent(tick, Velocity, Fuel, Altitude, 0); + return status; } } From c58d506ec27ede148dd75af37b640311b5101c37 Mon Sep 17 00:00:00 2001 From: David Date: Sun, 27 Mar 2022 20:16:23 -0400 Subject: [PATCH 2/5] landed --- src/main/java/BurnDataStream.java | 3 +- src/main/java/BurnInputStream.java | 6 ++- src/main/java/DescentEvent.java | 3 +- src/main/java/OnBoardComputer.java | 53 +++++++++++++++++++- src/main/java/Simulation.java | 9 +++- src/main/java/Vehicle.java | 4 +- src/test/java/OnBoardComputerTest.java | 69 ++++++++++++++++++++++++++ src/test/java/SimulationTest.java | 6 ++- 8 files changed, 143 insertions(+), 10 deletions(-) create mode 100644 src/test/java/OnBoardComputerTest.java diff --git a/src/main/java/BurnDataStream.java b/src/main/java/BurnDataStream.java index 051be0a..07d28ff 100644 --- a/src/main/java/BurnDataStream.java +++ b/src/main/java/BurnDataStream.java @@ -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() { } diff --git a/src/main/java/BurnInputStream.java b/src/main/java/BurnInputStream.java index c78b6c6..bdb760d 100644 --- a/src/main/java/BurnInputStream.java +++ b/src/main/java/BurnInputStream.java @@ -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; diff --git a/src/main/java/DescentEvent.java b/src/main/java/DescentEvent.java index 2add30f..e39b478 100644 --- a/src/main/java/DescentEvent.java +++ b/src/main/java/DescentEvent.java @@ -1,4 +1,4 @@ -public class DescentEvent { +public class DescentEvent { int Seconds = 0; int Velocity = 0; int Fuel = 0; @@ -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..b7a38c7 100644 --- a/src/main/java/OnBoardComputer.java +++ b/src/main/java/OnBoardComputer.java @@ -3,9 +3,60 @@ 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 < 150 && vel <100){ + if(alt < 50 && vel < 50){ + if(alt < 25 && vel < 25){ + if(alt < 10 && vel < 10){ + System.out.println(burn); /*hack!*/ + return targetVelocity(status.getVelocity(),2); + } + } + System.out.println(burn); /*hack!*/ + return targetVelocity(status.getVelocity(), 9); + } + System.out.println(burn); /*hack!*/ + return targetVelocity(status.getVelocity(), 49); + } + + 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; + } + } diff --git a/src/main/java/Simulation.java b/src/main/java/Simulation.java index 4f55e70..be45359 100644 --- a/src/main/java/Simulation.java +++ b/src/main/java/Simulation.java @@ -74,13 +74,18 @@ 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(Simulation.randomaltitude())); + //Simulation game = new Simulation(new Vehicle(4445)); + //The above simulation 4445 is absolute death. Simulation game = new Simulation(new Vehicle(Simulation.randomaltitude())); + //Simulation game = new Simulation(new Vehicle(Simulation.randomaltitude())); + // create a new BurnInputStream - BurnStream burnSource = new BurnInputStream(); + //BurnStream burnSource = new BurnInputStream(); + OnBoardComputer burnSource = new OnBoardComputer(); // pass the new BurnInputStream to the runSimulation method game.runSimulation(burnSource); - } } diff --git a/src/main/java/Vehicle.java b/src/main/java/Vehicle.java index b0e006b..166622f 100644 --- a/src/main/java/Vehicle.java +++ b/src/main/java/Vehicle.java @@ -90,8 +90,8 @@ public boolean outOfFuel() { public DescentEvent getStatus(int tick) { // create a return a new DescentEvent object // filled in with the state of the vehicle. - DescentEvent status = new DescentEvent(tick, Velocity, Fuel, Altitude, 0); - return status; + DescentEvent state= new DescentEvent(tick, Velocity, Fuel, Altitude, this.Flying); + return state; } } diff --git a/src/test/java/OnBoardComputerTest.java b/src/test/java/OnBoardComputerTest.java new file mode 100644 index 0000000..5966ffb --- /dev/null +++ b/src/test/java/OnBoardComputerTest.java @@ -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); + + } + +} \ No newline at end of file diff --git a/src/test/java/SimulationTest.java b/src/test/java/SimulationTest.java index 8fd05b4..8d15f7f 100644 --- a/src/test/java/SimulationTest.java +++ b/src/test/java/SimulationTest.java @@ -14,12 +14,14 @@ 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); From d0b9eccd81c490c65df4e31cbb3e61150eace013 Mon Sep 17 00:00:00 2001 From: David Date: Sun, 27 Mar 2022 20:21:33 -0400 Subject: [PATCH 3/5] autoPilot done --- src/main/java/OnBoardComputer.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/OnBoardComputer.java b/src/main/java/OnBoardComputer.java index b7a38c7..d25faa3 100644 --- a/src/main/java/OnBoardComputer.java +++ b/src/main/java/OnBoardComputer.java @@ -11,8 +11,8 @@ public int getNextBurn(DescentEvent status) { boolean done = false; while(!done){ - if(alt < 150 && vel <100){ - if(alt < 50 && vel < 50){ + if(alt < 155 && vel <150){ + if(alt < 100 && vel < 50){ if(alt < 25 && vel < 25){ if(alt < 10 && vel < 10){ System.out.println(burn); /*hack!*/ From 0054c47b10bd1f370078fd1ef4015c7d7d6546f8 Mon Sep 17 00:00:00 2001 From: David Date: Sun, 27 Mar 2022 20:32:41 -0400 Subject: [PATCH 4/5] sharpened --- src/main/java/OnBoardComputer.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/OnBoardComputer.java b/src/main/java/OnBoardComputer.java index d25faa3..b780598 100644 --- a/src/main/java/OnBoardComputer.java +++ b/src/main/java/OnBoardComputer.java @@ -14,7 +14,7 @@ public int getNextBurn(DescentEvent status) { if(alt < 155 && vel <150){ if(alt < 100 && vel < 50){ if(alt < 25 && vel < 25){ - if(alt < 10 && vel < 10){ + if(alt < 15 && vel < 10){ System.out.println(burn); /*hack!*/ return targetVelocity(status.getVelocity(),2); } From c0cb3028c28f060d496cdfefdcb1591e6c9c0a4a Mon Sep 17 00:00:00 2001 From: David Date: Mon, 28 Mar 2022 09:56:57 -0400 Subject: [PATCH 5/5] Polished --- src/main/java/OnBoardComputer.java | 18 ++++++++++++------ src/main/java/Simulation.java | 8 ++------ src/test/java/SimulationTest.java | 2 +- 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/src/main/java/OnBoardComputer.java b/src/main/java/OnBoardComputer.java index b780598..8b43971 100644 --- a/src/main/java/OnBoardComputer.java +++ b/src/main/java/OnBoardComputer.java @@ -11,19 +11,25 @@ public int getNextBurn(DescentEvent status) { boolean done = false; while(!done){ - if(alt < 155 && vel <150){ - if(alt < 100 && vel < 50){ - if(alt < 25 && vel < 25){ + 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 targetVelocity(status.getVelocity(),2); + 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 targetVelocity(status.getVelocity(), 9); + return burn; } + burn = targetVelocity(status.getVelocity(), 49);; System.out.println(burn); /*hack!*/ - return targetVelocity(status.getVelocity(), 49); + return burn; } if(burn == 200){ diff --git a/src/main/java/Simulation.java b/src/main/java/Simulation.java index be45359..a31eab6 100644 --- a/src/main/java/Simulation.java +++ b/src/main/java/Simulation.java @@ -73,13 +73,9 @@ 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(Simulation.randomaltitude())); - //Simulation game = new Simulation(new Vehicle(4445)); - //The above simulation 4445 is absolute death. - Simulation game = new Simulation(new Vehicle(Simulation.randomaltitude())); - //Simulation game = new Simulation(new Vehicle(Simulation.randomaltitude())); + 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(); diff --git a/src/test/java/SimulationTest.java b/src/test/java/SimulationTest.java index 8d15f7f..ae7093f 100644 --- a/src/test/java/SimulationTest.java +++ b/src/test/java/SimulationTest.java @@ -25,7 +25,7 @@ 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