diff --git a/src/main/java/core/basesyntax/RobotRoute.java b/src/main/java/core/basesyntax/RobotRoute.java index 351ca4b9..ab28d3f6 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 + moveInX(robot, toX); + + moveInY(robot, toY); + } + + private void moveInX(Robot robot, int toX) { + while (robot.getX() != toX) { + if (robot.getX() < toX) { + faceDirection(robot, Direction.RIGHT); + } else { + faceDirection(robot, Direction.LEFT); + } + robot.stepForward(); + } + } + + private void moveInY(Robot robot, int toY) { + while (robot.getY() != toY) { + if (robot.getY() < toY) { + faceDirection(robot, Direction.UP); + } else { + faceDirection(robot, Direction.DOWN); + } + robot.stepForward(); + } + } + + private void faceDirection(Robot robot, Direction targetDirection) { + while (robot.getDirection() != targetDirection) { + robot.turnRight(); + } } }