From a127aa283d457c8b4af95521c790c65fa023d18d Mon Sep 17 00:00:00 2001 From: Geks Date: Fri, 13 Dec 2024 21:29:40 +0200 Subject: [PATCH] implemented moveRobot() method to carry our friend to desired destination --- src/main/java/core/basesyntax/RobotRoute.java | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/main/java/core/basesyntax/RobotRoute.java b/src/main/java/core/basesyntax/RobotRoute.java index 351ca4b9..9ac9499c 100644 --- a/src/main/java/core/basesyntax/RobotRoute.java +++ b/src/main/java/core/basesyntax/RobotRoute.java @@ -2,6 +2,25 @@ public class RobotRoute { public void moveRobot(Robot robot, int toX, int toY) { - //write your solution here + //write your solution here + int curX = robot.getX(); + int distanceX = ((toX - curX) < 0) ? ((toX - curX) * -1) : (toX - curX); + Direction neededDirectionX = (curX < toX) ? Direction.RIGHT : Direction.LEFT; + while (robot.getDirection() != neededDirectionX) { + robot.turnLeft(); + } + for (int i = 1; i <= distanceX; ++i) { + robot.stepForward(); + } + + int curY = robot.getY(); + int distanceY = ((toY - curY) < 0) ? ((toY - curY) * -1) : (toY - curY); + Direction neededDirectionY = (curY < toY) ? Direction.UP : Direction.DOWN; + while (robot.getDirection() != neededDirectionY) { + robot.turnLeft(); + } + for (int i = 1; i <= distanceY; ++i) { + robot.stepForward(); + } } }