Skip to content

Commit

Permalink
Task completed
Browse files Browse the repository at this point in the history
  • Loading branch information
Andriy Gromysh committed Feb 13, 2024
1 parent 80be487 commit c26c84f
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 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,39 @@

public class RobotRoute {
public void moveRobot(Robot robot, int toX, int toY) {
//write your solution here
int diffX = toX - robot.getX();
int diffY = toY - robot.getY();

if (diffX == 0 && diffY == 0) {
return;
}

if (diffX > 0) {
while (robot.getDirection() != Direction.RIGHT) {
robot.turnRight();
}
} else {
while (robot.getDirection() != Direction.LEFT) {
robot.turnRight();
}
}

while (robot.getY() != toY) {
robot.stepForward();
}

if (diffY > 0) {
while (robot.getDirection() != Direction.UP) {
robot.turnRight();
}
} else {
while (robot.getDirection() != Direction.DOWN) {
robot.turnRight();
}
}

while (robot.getX() != toX) {
robot.stepForward();
}
}
}

0 comments on commit c26c84f

Please sign in to comment.