diff --git a/src/main/java/core/basesyntax/RobotRoute.java b/src/main/java/core/basesyntax/RobotRoute.java index 351ca4b9..af96609a 100644 --- a/src/main/java/core/basesyntax/RobotRoute.java +++ b/src/main/java/core/basesyntax/RobotRoute.java @@ -2,6 +2,36 @@ public class RobotRoute { public void moveRobot(Robot robot, int toX, int toY) { - //write your solution here + int x = robot.getX(); + int y = robot.getY(); + + rotate(robot, x, toX, Direction.RIGHT, Direction.LEFT); + + while (x != toX) { + robot.stepForward(); + x = robot.getX(); + } + + rotate(robot, y, toY, Direction.UP, Direction.DOWN); + + while (y != toY) { + robot.stepForward(); + y = robot.getY(); + } + } + + private void rotate(Robot robot, int currentCoordinate, int targetCoordinate, + Direction positiveDirection, Direction negativeDirection) { + + if (currentCoordinate < targetCoordinate) { + while (robot.getDirection() != positiveDirection) { + robot.turnRight(); + } + } else if (currentCoordinate > targetCoordinate) { + while (robot.getDirection() != negativeDirection) { + robot.turnLeft(); + } + } + } }