diff --git a/src/main/java/core/basesyntax/RobotRoute.java b/src/main/java/core/basesyntax/RobotRoute.java index 351ca4b9..fb3a01a4 100644 --- a/src/main/java/core/basesyntax/RobotRoute.java +++ b/src/main/java/core/basesyntax/RobotRoute.java @@ -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; + } } }