From b454463bec7ba3dea646060da5f8ceb8d00df4af Mon Sep 17 00:00:00 2001 From: Danil Date: Thu, 7 Nov 2024 19:53:41 +0100 Subject: [PATCH] Changed RobotRoute.java --- src/main/java/core/basesyntax/RobotRoute.java | 31 ++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/src/main/java/core/basesyntax/RobotRoute.java b/src/main/java/core/basesyntax/RobotRoute.java index 351ca4b9..25149ef2 100644 --- a/src/main/java/core/basesyntax/RobotRoute.java +++ b/src/main/java/core/basesyntax/RobotRoute.java @@ -2,6 +2,35 @@ public class RobotRoute { public void moveRobot(Robot robot, int toX, int toY) { - //write your solution here + //write your solution here + if (robot.getX() < toX) { + faceDirection(robot, Direction.RIGHT); + while (robot.getX() < toX) { + robot.stepForward(); + } + } else if (robot.getX() > toX) { + faceDirection(robot, Direction.LEFT); + while (robot.getX() > toX) { + robot.stepForward(); + } + } + + if (robot.getY() < toY) { + faceDirection(robot, Direction.UP); + while (robot.getY() < toY) { + robot.stepForward(); + } + } else if (robot.getY() > toY) { + faceDirection(robot, Direction.DOWN); + while (robot.getY() > toY) { + robot.stepForward(); + } + } + } + + private void faceDirection(Robot robot, Direction targetDirection) { + while (robot.getDirection() != targetDirection) { + robot.turnRight(); + } } }