Skip to content

Commit

Permalink
Implemented moveRobot method
Browse files Browse the repository at this point in the history
  • Loading branch information
SSXcorp committed Dec 3, 2024
1 parent 80be487 commit be7e372
Showing 1 changed file with 36 additions and 1 deletion.
37 changes: 36 additions & 1 deletion src/main/java/core/basesyntax/RobotRoute.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,42 @@
package core.basesyntax;

import java.util.Objects;

public class RobotRoute {
public void moveRobot(Robot robot, int toX, int toY) {
//write your solution here
int stepsX = setDirectionAndStepsX(robot, toX);
for (int i = 0; i < stepsX; i++) {
robot.stepForward();
}
int stepsY = setDirectionAndStepsY(robot, toY);
for (int i = 0; i < stepsY; i++) {
robot.stepForward();
}
}

public static int setDirectionAndStepsX(Robot robot, int toX) {
if (robot.getX() < toX) {
while (!Objects.equals(robot.getDirection().toString(), "RIGHT")) {
robot.turnRight();
}
} else {
while (!Objects.equals(robot.getDirection().toString(), "LEFT")) {
robot.turnRight();
}
}
return Math.max(robot.getX(), toX) - Math.min(robot.getX(), toX);
}

public static int setDirectionAndStepsY(Robot robot, int toY) {
if (robot.getY() < toY) {
while (!Objects.equals(robot.getDirection().toString(), "UP")) {
robot.turnRight();
}
} else {
while (!Objects.equals(robot.getDirection().toString(), "DOWN")) {
robot.turnRight();
}
}
return Math.max(robot.getY(), toY) - Math.min(robot.getY(), toY);
}
}

0 comments on commit be7e372

Please sign in to comment.