From 92be3ecf84858250be52d62b4e2bc83b35bd5491 Mon Sep 17 00:00:00 2001 From: SviatoslavTataryn Date: Tue, 2 Apr 2024 21:34:07 +0200 Subject: [PATCH] Implementation the logic in the `moveRobot` method of the `RobotRoute` class to navigate the robot from its current position to the target position --- src/main/java/core/basesyntax/RobotRoute.java | 32 ++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/src/main/java/core/basesyntax/RobotRoute.java b/src/main/java/core/basesyntax/RobotRoute.java index 351ca4b9..f06ef04f 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 + if (toX > robot.getX()) { + while (robot.getDirection() != Direction.RIGHT) { + robot.turnRight(); + } + while (robot.getX() < toX) { + robot.stepForward(); + } + } else if (toX < robot.getX()) { + while (robot.getDirection() != Direction.LEFT) { + robot.turnLeft(); + } + while (robot.getX() > toX) { + robot.stepForward(); + } + } + + if (toY > robot.getY()) { + while (robot.getDirection() != Direction.UP) { + robot.turnRight(); + } + while (robot.getY() < toY) { + robot.stepForward(); + } + } else if (toY < robot.getY()) { + while (robot.getDirection() != Direction.DOWN) { + robot.turnLeft(); + } + while (robot.getY() > toY) { + robot.stepForward(); + } + } } }