From fc3ea6c57ff02243a2dd2ceaf8ed66cba502b0b3 Mon Sep 17 00:00:00 2001 From: Your_Name Your_Surname Date: Fri, 20 Dec 2024 02:16:54 +0200 Subject: [PATCH] Implemented the logic in the moveRobot method --- src/main/java/core/basesyntax/RobotRoute.java | 59 ++++++++++++++++++- 1 file changed, 58 insertions(+), 1 deletion(-) diff --git a/src/main/java/core/basesyntax/RobotRoute.java b/src/main/java/core/basesyntax/RobotRoute.java index 351ca4b9..0c5075f8 100644 --- a/src/main/java/core/basesyntax/RobotRoute.java +++ b/src/main/java/core/basesyntax/RobotRoute.java @@ -1,7 +1,64 @@ package core.basesyntax; public class RobotRoute { + public void moveRobot(Robot robot, int toX, int toY) { - //write your solution here + if (toX != robot.getX()) { + RobotRoute.moveRobotToX(robot, toX); + } + if (toY != robot.getY()) { + RobotRoute.moveRobotToY(robot, toX, toY); + } + } + + public static void moveRobotToX(Robot robot, int toX) { + int diffrenceX = toX - robot.getX(); + robot.stepForward(); + if (toX < robot.getX()) { + while (toX - robot.getX() <= diffrenceX) { + robot.turnLeft(); + diffrenceX = toX - robot.getX(); + robot.stepForward(); + } + } else { + while (toX - robot.getX() >= diffrenceX) { + robot.turnLeft(); + diffrenceX = toX - robot.getX(); + robot.stepForward(); + } + } + while (toX != robot.getX()) { + robot.stepForward(); + } + } + + public static void moveRobotToY(Robot robot, int toX, int toY) { + int diffrenceY = toY - robot.getY(); + robot.turnLeft(); + robot.stepForward(); + if (toX != robot.getX()) { + robot.turnLeft(); + robot.turnLeft(); + robot.stepForward(); + robot.turnLeft(); + } + if (toY < robot.getY()) { + while (toY - robot.getY() <= diffrenceY) { + robot.turnLeft(); + robot.turnLeft(); + diffrenceY = toY - robot.getY(); + robot.stepForward(); + } + } else { + while (toY - robot.getY() >= diffrenceY) { + robot.turnLeft(); + robot.turnLeft(); + diffrenceY = toY - robot.getY(); + robot.stepForward(); + } + } + while (toY != robot.getY()) { + robot.stepForward(); + } } }