Skip to content

Commit

Permalink
Fix/solution (#2)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
zentox authored Jun 21, 2023
1 parent 70ca3b7 commit 9786d92
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
27 changes: 17 additions & 10 deletions src/main/java/h06/ui/MazeVisualizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/h06/ui/ProblemVisualizer.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package h06.ui;

import h06.problems.ProblemSolver;
import h06.world.DirectionVector;
import h06.world.World;

import java.awt.Point;
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 9786d92

Please sign in to comment.