Skip to content

Commit

Permalink
Task done
Browse files Browse the repository at this point in the history
  • Loading branch information
nastiapyshak committed Mar 17, 2024
1 parent 80be487 commit b423fb0
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 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,25 @@

public class RobotRoute {
public void moveRobot(Robot robot, int toX, int toY) {
//write your solution here
int distanceX = toX - robot.getX();
int distanceY = toY - robot.getY();
if (distanceX != 0) {
Direction directionX = (distanceX > 0) ? Direction.RIGHT : Direction.LEFT;
while (robot.getDirection() != directionX) {
robot.turnRight();
}
while (robot.getX() != toX) {
robot.stepForward();
}
}
if (distanceY != 0) {
Direction directionY = (distanceY > 0) ? Direction.UP : Direction.DOWN;
while (robot.getDirection() != directionY) {
robot.turnRight();
}
while (robot.getY() != toY) {
robot.stepForward();
}
}
}
}

0 comments on commit b423fb0

Please sign in to comment.