diff --git a/assignments/src/MV3500Cohort2024JulySeptember/homework3/Lennon/LennonSimulationProgram.java b/assignments/src/MV3500Cohort2024JulySeptember/homework3/Lennon/LennonSimulationProgram.java index a6bf39142a82b9a8f07e507e3acb7540398aac60..826a31c19290b5dc54aa801df3404e0c560207fb 100644 --- a/assignments/src/MV3500Cohort2024JulySeptember/homework3/Lennon/LennonSimulationProgram.java +++ b/assignments/src/MV3500Cohort2024JulySeptember/homework3/Lennon/LennonSimulationProgram.java @@ -11,9 +11,13 @@ import edu.nps.moves.dis7.enumerations.*; import edu.nps.moves.dis7.pdus.*; import edu.nps.moves.dis7.utilities.DisChannel; import edu.nps.moves.dis7.utilities.PduFactory; +import static java.lang.Math.max; +import static java.lang.Math.min; import java.time.LocalDateTime; import java.util.ArrayList; import java.util.List; +import java.util.Random; +import java.util.Scanner; import java.util.logging.Level; import java.util.logging.Logger; @@ -205,7 +209,7 @@ public class LennonSimulationProgram { try { - final int SIMULATION_MAX_LOOP_COUNT = 10; // set number of turns in the game also avoid infinite loops. + final int SIMULATION_MAX_LOOP_COUNT = 10; // set number of turns in the game; also avoid infinite loops. int simulationLoopCount = 0; // variable, initialized at 0 boolean simulationComplete = false; // sentinel variable as termination condition, are we done yet? final int NUMBER_MINES = 5; // preset number of mines to be placed @@ -217,12 +221,36 @@ public class LennonSimulationProgram // =================================================================================================== // loop the simulation while allowed, programmer can set additional conditions to break out and finish + Random random = new Random(); + Scanner scanner = new Scanner(System.in); List<double[]> minefield = new ArrayList<>(); + //set start point at (0,0) + entityStatePdu_1.getEntityLocation().setX(0.0); + entityStatePdu_1.getEntityLocation().setY(0.0); + double[] start = {entityStatePdu_1.getEntityLocation().getX(), entityStatePdu_1.getEntityLocation().getY()}; + + //exit square is somewhere on the middle third of the far right edge of the minefield (space 3-6) + double exitY = random.nextInt(4)+3; + double[] exit = {9, exitY}; + for (int i = 0; i < NUMBER_MINES; i++){ - //double x = Random.nextInt(); - //double[] mine = [] + //random placement of the mines within the 10x10 grid + double x = random.nextInt(10); + double y = random.nextInt(10) ; + double[] mine = {x,y}; + //No mine placed at the start position or on the exit space + if ((mine[0] == start[0] && mine[1] == start[1]) || (mine[0] == exit[0] && mine[1] == exit[1])){ + continue; + } + //add mines to the minefield list + minefield.add(mine); } + + System.out.println("You are in a minefield. The exit is at (" + exit[0] + ", " + exit[1] + + "). \nWhen prompted, use the WASD keys to pick a move. \nCareful, there's a time limit, and plenty of mines!"); + System.out.flush(); + while (simulationLoopCount < SIMULATION_MAX_LOOP_COUNT) // are we done yet? { simulationLoopCount++; // good practice: increment loop counter as first action in that loop @@ -230,10 +258,33 @@ public class LennonSimulationProgram // ============================================================================================= // * your own simulation code starts here! ***************************************************** // ============================================================================================= - + System.out.println("\nCurrent Location: (" + entityStatePdu_1.getEntityLocation().getX()+","+ entityStatePdu_1.getEntityLocation().getY() + "); Exit: (" + exit[0] + ", " + exit[1] + ")."); + System.out.println("Move "+ simulationLoopCount+ "of " + SIMULATION_MAX_LOOP_COUNT + ". Where would you like to move now?" ); // are there any other variables to modify at the beginning of your loop? - - // are your reading any DIS PDUs from the network? check for them here + boolean validMove = false; + String move = null; + while (!validMove){ + move = scanner.nextLine().toUpperCase(); + switch(move){ + case("W") -> { + entityStatePdu_1.getEntityLocation().setY(min(9.0, entityStatePdu_1.getEntityLocation().getY() + 1.0)); + validMove = true; + } + case("A") -> { + entityStatePdu_1.getEntityLocation().setX(max(0.0, entityStatePdu_1.getEntityLocation().getX() - 1.0)); + validMove = true; + } + case("S") -> { + entityStatePdu_1.getEntityLocation().setY(max(0.0, entityStatePdu_1.getEntityLocation().getY() - 1.0)); + validMove = true; + } + case("D") -> { + entityStatePdu_1.getEntityLocation().setX(min(9.0, entityStatePdu_1.getEntityLocation().getX() + 1.0)); + validMove = true; + } + default -> System.out.println("Not a valid move. Try again"); + } + } // compute a track, update an ESPDU, whatever it is that your model is doing... @@ -250,15 +301,34 @@ public class LennonSimulationProgram // make your reports: narrative code for CommentPdu here (set all to empty strings to avoid sending) - narrativeMessage1 = "MV3500 ExampleSimulationProgram"; + narrativeMessage1 = "MV3500 LennonSimulationProgram"; narrativeMessage2 = "runSimulation() loop " + simulationLoopCount; narrativeMessage3 = ""; // intentionally blank for testing // your loop termination condition goes here - if (simulationLoopCount > MAX_LOOP_COUNT) // for example + + //Check for entity to reach exit + if (entityStatePdu_1.getEntityLocation().getX() == exit[0] && entityStatePdu_1.getEntityLocation().getY() == exit[1]){ + System.out.println("You've escaped the minefield. Good job."); + narrativeMessage3 = "Entity escaped successfully."; + simulationComplete = true; + } + //Check for turn limit reached + else if (simulationLoopCount >= SIMULATION_MAX_LOOP_COUNT) { + System.out.println("You've run out of time. You lose."); + narrativeMessage3 = "Entity ran out of time."; simulationComplete = true; - } + } + + //Check for entity to hit mine + for (double[] mine : minefield){ + if (entityStatePdu_1.getEntityLocation().getX() == mine[0]&& entityStatePdu_1.getEntityLocation().getY() == mine[1]){ + System.out.println("BOOM! you hit a mine. You're dead."); + narrativeMessage3 = "Entity hit a mine and was destroyed."; + simulationComplete = true; + } + } // ============================================================================================= // * your own simulation code is finished here! ************************************************ // =============================================================================================