From b423fb069a261f570691f42b27287b983e0d1041 Mon Sep 17 00:00:00 2001 From: Nastia Pyshak Date: Sun, 17 Mar 2024 21:19:09 +0100 Subject: [PATCH] Task done --- 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..969286f1 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 + int distanceX = toX - robot.getX(); + int distanceY = toY - robot.getY(); + if (distanceX != 0) { + Direction directionX = (distanceX > 0) ? Direction.RIGHT : Direction.LEFT; + while (robot.getDirection() != directionX) { + robot.turnRight(); + } + while (robot.getX() != toX) { + robot.stepForward(); + } + } + if (distanceY != 0) { + Direction directionY = (distanceY > 0) ? Direction.UP : Direction.DOWN; + while (robot.getDirection() != directionY) { + robot.turnRight(); + } + while (robot.getY() != toY) { + robot.stepForward(); + } + } } }