Skip to content

Commit

Permalink
implement moveRobot
Browse files Browse the repository at this point in the history
  • Loading branch information
Nazar committed Feb 3, 2025
1 parent 80be487 commit c1e4d45
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion src/main/java/core/basesyntax/RobotRoute.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,32 @@

public class RobotRoute {
public void moveRobot(Robot robot, int toX, int toY) {
//write your solution here
while (robot.getX() != toX) {
if (robot.getX() < toX) {
faceDirection(robot, Direction.RIGHT);
} else {
faceDirection(robot, Direction.LEFT);
}
robot.stepForward();
}

while (robot.getY() != toY) {
if (robot.getY() < toY) {
faceDirection(robot, Direction.UP);
} else {
faceDirection(robot, Direction.DOWN);
}
robot.stepForward();
}
}

private void faceDirection(Robot robot, Direction targetDirection) {
Direction currentDirection = robot.getDirection();

while (currentDirection != targetDirection) {
robot.turnRight();
currentDirection = robot.getDirection();
}
}
}

0 comments on commit c1e4d45

Please sign in to comment.