From e7b5e68bba82efe30bfc12b1544de67289cb1cb0 Mon Sep 17 00:00:00 2001 From: Artem Horopakha Date: Sat, 22 Feb 2025 23:46:25 +0200 Subject: [PATCH] Implemented the RobotRoute class method moveRobot logic using while, if, else if, and switch operators. The general algorithm is as follows: The robot stays in the loops until its X and Y coordinate reaches the target coordinate. If the coordinate is not met, the robot rotates in the appropriate direction or moves forward. --- src/main/java/core/basesyntax/RobotRoute.java | 49 ++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/src/main/java/core/basesyntax/RobotRoute.java b/src/main/java/core/basesyntax/RobotRoute.java index 351ca4b9..d861685b 100644 --- a/src/main/java/core/basesyntax/RobotRoute.java +++ b/src/main/java/core/basesyntax/RobotRoute.java @@ -2,6 +2,53 @@ public class RobotRoute { public void moveRobot(Robot robot, int toX, int toY) { - //write your solution here + + while (!(robot.getY() == toY)) { + if (toY > robot.getY()) { + switch (robot.getDirection()) { + case UP : robot.stepForward(); + break; + case LEFT, DOWN : robot.turnRight(); + break; + case RIGHT : robot.turnLeft(); + break; + default: break; + } + } else if (toY < robot.getY()) { + switch (robot.getDirection()) { + case DOWN : robot.stepForward(); + break; + case LEFT, UP : robot.turnLeft(); + break; + case RIGHT : robot.turnRight(); + break; + default: break; + } + } + } + + while (!(robot.getX() == toX)) { + if (toX > robot.getX()) { + switch (robot.getDirection()) { + case DOWN : robot.turnLeft(); + break; + case LEFT, UP : robot.turnRight(); + break; + case RIGHT : robot.stepForward(); + break; + default: break; + } + } else if (toX < robot.getX()) { + switch (robot.getDirection()) { + case DOWN : robot.turnRight(); + break; + case RIGHT, UP : robot.turnLeft(); + break; + case LEFT : robot.stepForward(); + break; + default: break; + } + } + } } }