Skip to content

Commit

Permalink
home_task5_buildSuccess
Browse files Browse the repository at this point in the history
  • Loading branch information
VovaZSU committed Jul 1, 2024
1 parent 80be487 commit ed126b8
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 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,23 @@

public class RobotRoute {
public void moveRobot(Robot robot, int toX, int toY) {
//write your solution here
while (robot.getX() != toX || robot.getY() != toY) {
while (robot.getDirection() != getDirectionToTarget(robot, toX, toY)) {
robot.turnRight();
}
robot.stepForward();
}
}

private Direction getDirectionToTarget(Robot robot, int toX, int toY) {
if (toY > robot.getY()) {
return Direction.UP;
} else if (toY < robot.getY()) {
return Direction.DOWN;
} else if (toX > robot.getX()) {
return Direction.RIGHT;
} else {
return Direction.LEFT;
}
}
}

0 comments on commit ed126b8

Please sign in to comment.