diff --git a/81_Splat/java/src/Splat.java b/81_Splat/java/src/Splat.java index 4c0e2fdf..3c4cd817 100644 --- a/81_Splat/java/src/Splat.java +++ b/81_Splat/java/src/Splat.java @@ -1,5 +1,22 @@ import java.util.*; +/** + * SPLAT simulates a parachute jump in which you try to open your parachute at the last possible moment without going + * splat! You may select your own terminal velocity or let the computer do it for you. You many also select the + * acceleration due to gravity or, again, let the computer do it in which case you might wind up on any of eight + * planets (out to Neptune), the moon, or the sun. + *
+ * The computer then tells you the height you’re jumping from and asks for the seconds of free fall. It then divides + * your free fall time into eight intervals and gives you progress reports on your way down. The computer also keeps + * track of all prior jumps in the array A and lets you know how you compared with previous successful jumps. If you + * want to recall information from previous runs, then you should store array A in a disk or take file and read it + * before each run. + *
+ * John Yegge created this program while at the Oak Ridge Associated Universities. + *
+ * Ported from BASIC by jason plumb (@breedx2) + *
+ */ public class Splat { private static final Random random = new Random(); private final Scanner scanner = new Scanner(System.in); @@ -31,13 +48,74 @@ public class Splat { JumpResult jump = executeJump(initial, freefallTime); showJumpResults(initial, jump); - if(!playAgain()){ + if (!playAgain()) { System.out.println("SSSSSSSSSS."); return; } } } + private void showIntroduction() { + System.out.printf("%33s%s\n", " ", "SPLAT"); + System.out.printf("%15s%s\n", " ", "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY"); + System.out.print("\n\n\n"); + System.out.println("WELCOME TO 'SPLAT' -- THE GAME THAT SIMULATES A PARACHUTE"); + System.out.println("JUMP. TRY TO OPEN YOUR CHUTE AT THE LAST POSSIBLE"); + System.out.println("MOMENT WITHOUT GOING SPLAT."); + } + + private InitialJumpConditions buildInitialConditions() { + System.out.print("\n\n"); + float terminalVelocity = promptTerminalVelocity(); + float acceleration = promptGravitationalAcceleration(); + return InitialJumpConditions.create(terminalVelocity, acceleration); + } + + private float promptTerminalVelocity() { + if (askYesNo("SELECT YOUR OWN TERMINAL VELOCITY")) { + System.out.print("WHAT TERMINAL VELOCITY (MI/HR) "); + return mphToFeetPerSec(scanner.nextFloat()); + } + float terminalVelocity = (int) (1000 * random.nextFloat()); + System.out.printf("OK. TERMINAL VELOCITY = %.2f MI/HR\n", terminalVelocity); + return mphToFeetPerSec(terminalVelocity); + } + + private float promptGravitationalAcceleration() { + if (askYesNo("WANT TO SELECT ACCELERATION DUE TO GRAVITY")) { + System.out.print("WHAT ACCELERATION (FT/SEC/SEC) "); + return scanner.nextFloat(); + } + return chooseRandomAcceleration(); + } + + private JumpResult executeJump(InitialJumpConditions initial, float chuteOpenTime) { + JumpResult jump = new JumpResult(initial.getAltitude()); + for (float time = 0.0f; !jump.isSplat() && (time < chuteOpenTime); time += chuteOpenTime / 8) { + if (!jump.hasReachedTerminalVelocity() && time > initial.getTimeOfTerminalAccelerationReached()) { + jump.setReachedTerminalVelocity(); + System.out.printf("TERMINAL VELOCITY REACHED AT T PLUS %f SECONDS.\n", initial.getTimeOfTerminalAccelerationReached()); + } + float newDistance = computeDistance(initial, time, jump.hasReachedTerminalVelocity()); + jump.setDistance(newDistance); + + if (jump.isSplat()) { + return jump; + } + System.out.printf("%10.2f %f\n", time, jump.getDistance()); + } + return jump; + } + + private float computeDistance(InitialJumpConditions initial, float i, boolean hasReachedTerminalVelocity) { + final float V = initial.getTerminalVelocity(); + final float A = initial.getAcceleration(); + if (hasReachedTerminalVelocity) { + return initial.getAltitude() - ((V * V / (2 * A)) + (V * (i - (V / A)))); + } + return initial.getAltitude() - ((A / 2) * i * i); + } + private void showJumpResults(InitialJumpConditions initial, JumpResult jump) { if (jump.isSplat()) { showSplatMessage(initial, jump); @@ -85,6 +163,9 @@ public class Splat { System.out.printf("%10.2f SPLAT\n", timeOfSplat); } + /** + * Returns the number of jumps for which this jump was better + */ private double computeTimeOfSplat(InitialJumpConditions initial, JumpResult jump) { final float V = initial.getTerminalVelocity(); final float A = initial.getAcceleration(); @@ -94,38 +175,26 @@ public class Splat { return Math.sqrt(2 * initial.getAltitude() / A); } - // Returns the number of jumps for which this jump was better private int countWorseHistoricalJumps(JumpResult jump) { return (int) pastSuccessfulJumpDistances.stream() .filter(distance -> jump.getDistance() < distance) .count(); } - private JumpResult executeJump(InitialJumpConditions initial, float chuteOpenTime) { - JumpResult jump = new JumpResult(initial.getAltitude()); - for (float time = 0.0f; !jump.isSplat() && (time < chuteOpenTime); time += chuteOpenTime / 8) { - if (!jump.hasReachedTerminalVelocity() && time > initial.getTimeOfTerminalAccelerationReached()) { - jump.setReachedTerminalVelocity(); - System.out.printf("TERMINAL VELOCITY REACHED AT T PLUS %f SECONDS.\n", initial.getTimeOfTerminalAccelerationReached()); - } - float newDistance = computeDistance(initial, time, jump.hasReachedTerminalVelocity()); - jump.setDistance(newDistance); - - if (jump.isSplat()) { - return jump; - } - System.out.printf("%10.2f %f\n", time, jump.getDistance()); - } - return jump; - } - - private float computeDistance(InitialJumpConditions initial, float i, boolean hasReachedTerminalVelocity) { - final float V = initial.getTerminalVelocity(); - final float A = initial.getAcceleration(); - if(hasReachedTerminalVelocity) { - return initial.getAltitude() - ((V * V / (2 * A)) + (V * (i - (V / A)))); - } - return initial.getAltitude() - ((A / 2) * i * i); + private void showCleverSplatMessage() { + List