diff --git a/src/main/java/core/basesyntax/RobotRoute.java b/src/main/java/core/basesyntax/RobotRoute.java index 351ca4b9..c6b400be 100644 --- a/src/main/java/core/basesyntax/RobotRoute.java +++ b/src/main/java/core/basesyntax/RobotRoute.java @@ -2,6 +2,41 @@ public class RobotRoute { public void moveRobot(Robot robot, int toX, int toY) { - //write your solution here + if (robot.getX() == toX && robot.getY() == toY) { + return; + } + + rotateRobot(robot, findHorizontalDirection(robot, toX)); + while (robot.getX() != toX) { + robot.stepForward(); + } + + rotateRobot(robot, findVerticalDirection(robot, toY)); + while (robot.getY() != toY) { + robot.stepForward(); + } } + + private Direction findHorizontalDirection(Robot robot, int targetX) { + if (robot.getX() < targetX) { + return Direction.RIGHT; + } else { + return Direction.LEFT; + } + } + + private Direction findVerticalDirection(Robot robot, int targetY) { + if (robot.getY() < targetY) { + return Direction.UP; + } else { + return Direction.DOWN; + } + } + + private void rotateRobot(Robot robot, Direction targetDirection) { + while (robot.getDirection() != targetDirection) { + robot.turnLeft(); + } + } + }