Skip to content

Commit

Permalink
added robot route
Browse files Browse the repository at this point in the history
  • Loading branch information
skviropitek committed May 6, 2024
1 parent 80be487 commit 99a9c89
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 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,30 @@

public class RobotRoute {
public void moveRobot(Robot robot, int toX, int toY) {
//write your solution here
int currentX = robot.getX();
int currentY = robot.getY();
if (currentX < toX) {
while (robot.getDirection() != Direction.RIGHT) {
robot.turnRight();
}
}
else if (currentX > toX) {
while (robot.getDirection() != Direction.LEFT) {
robot.turnLeft();
}
}
else if (currentY < toY) {
while (robot.getDirection() != Direction.UP) {
robot.turnLeft();
}
}
else if (currentY > toY) {
while (robot.getDirection() != Direction.DOWN) {
robot.turnLeft();
}
}
while (currentX != toX || currentY != toY) {
robot.stepForward();
}
}
}

0 comments on commit 99a9c89

Please sign in to comment.