Skip to content

Commit

Permalink
myRobot
Browse files Browse the repository at this point in the history
  • Loading branch information
VovaPryt committed Nov 15, 2024
1 parent 80be487 commit 0bad2ec
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
if (robot.getX() != toX) {
if (robot.getX() < toX) {
while (robot.getDirection() != Direction.RIGHT) {
robot.turnRight();
}
while (robot.getX() < toX) {
robot.stepForward();
}
} else if (robot.getX() > toX) {
while (robot.getDirection() != Direction.LEFT) {
robot.turnRight();
}
while (robot.getX() > toX) {
robot.stepForward();
}
}
}
if (robot.getY() != toY) {
if (robot.getY() < toY) {
while (robot.getDirection() != Direction.UP) {
robot.turnRight();
}
while (robot.getY() < toY) {
robot.stepForward();
}
} else if (robot.getY() > toY) {
while (robot.getDirection() != Direction.DOWN) {
robot.turnRight();
}
while (robot.getY() > toY) {
robot.stepForward();
}
}
}
}
}

0 comments on commit 0bad2ec

Please sign in to comment.