From 9786d9236271012b8ae1f080e728abbbe19d9b2f Mon Sep 17 00:00:00 2001 From: nyanyan <46193012+zentox@users.noreply.github.com> Date: Wed, 21 Jun 2023 16:44:09 +0200 Subject: [PATCH] Fix/solution (#2) * Add parameter to run the visualizer with different directions * Add default constructor * Add annotation do emphasize the parts of the code which should be modified by the students * Revert annotations --- src/main/java/h06/ui/MazeVisualizer.java | 27 +++++++++++++-------- src/main/java/h06/ui/ProblemVisualizer.java | 4 ++- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/src/main/java/h06/ui/MazeVisualizer.java b/src/main/java/h06/ui/MazeVisualizer.java index f54ae47..c5d1f81 100644 --- a/src/main/java/h06/ui/MazeVisualizer.java +++ b/src/main/java/h06/ui/MazeVisualizer.java @@ -21,6 +21,13 @@ public class MazeVisualizer implements ProblemVisualizer { */ private World world; + + /** + * Constructs a maze visualizer. + */ + public MazeVisualizer() { + } + @Override public void init(World world) { this.world = world; @@ -47,16 +54,16 @@ public void show(boolean visible) { } @Override - public void run(ProblemSolver solver, Point s, Point e) { - Point[] path = solver.solve(world, s, e, DirectionVector.UP); + public void run(ProblemSolver solver, Point s, Point e, DirectionVector d) { + Point[] path = solver.solve(world, s, e, d); + Robot robot = new Robot(s.x, s.y, Direction.values()[d.ordinal()], 0); + fopbot.World.getGlobalWorld().setFieldColor(s.x, s.y, Color.BLUE); fopbot.World.getGlobalWorld().setFieldColor(e.x, e.y, Color.YELLOW); - Robot mazeRunner = new Robot(s.x, s.y); - for (int i = 1; i < path.length; i++) { Point p = path[i]; - int x = mazeRunner.getX(); - int y = mazeRunner.getY(); + int x = robot.getX(); + int y = robot.getY(); // Compute movement direction Direction direction; @@ -71,12 +78,12 @@ public void run(ProblemSolver solver, Point s, Point e) { } // Turn robot to the correct direction and then move - while (mazeRunner.getDirection() != direction) { - mazeRunner.turnLeft(); + while (robot.getDirection() != direction) { + robot.turnLeft(); } - mazeRunner.move(); + robot.move(); } - mazeRunner.turnOff(); + robot.turnOff(); } @Override diff --git a/src/main/java/h06/ui/ProblemVisualizer.java b/src/main/java/h06/ui/ProblemVisualizer.java index f1e5dae..710d122 100644 --- a/src/main/java/h06/ui/ProblemVisualizer.java +++ b/src/main/java/h06/ui/ProblemVisualizer.java @@ -1,6 +1,7 @@ package h06.ui; import h06.problems.ProblemSolver; +import h06.world.DirectionVector; import h06.world.World; import java.awt.Point; @@ -39,8 +40,9 @@ default void show() { * @param solver the solver to use * @param s the start point * @param e the end point + * @param d the direction to start with */ - void run(ProblemSolver solver, Point s, Point e); + void run(ProblemSolver solver, Point s, Point e, DirectionVector d); /** * Sets the delay between each step in milliseconds.