From 88ec6edbed450c300658aa176843f965211e15f9 Mon Sep 17 00:00:00 2001 From: Oleksii Date: Mon, 29 Apr 2024 22:28:25 +0300 Subject: [PATCH] add a solution to RobotRoute class --- src/main/java/core/basesyntax/RobotRoute.java | 34 ++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/src/main/java/core/basesyntax/RobotRoute.java b/src/main/java/core/basesyntax/RobotRoute.java index 351ca4b9..fe5af729 100644 --- a/src/main/java/core/basesyntax/RobotRoute.java +++ b/src/main/java/core/basesyntax/RobotRoute.java @@ -2,6 +2,38 @@ public class RobotRoute { public void moveRobot(Robot robot, int toX, int toY) { - //write your solution here + //write your solution here + if (toX - robot.getX() > 0) { + while (robot.getDirection() != Direction.RIGHT) { + robot.turnRight(); + } + while (toX - robot.getX() != 0) { + robot.stepForward(); + } + } + if (toX - robot.getX() < 0) { + while (robot.getDirection() != Direction.LEFT) { + robot.turnLeft(); + } + while (toX - robot.getX() != 0) { + robot.stepForward(); + } + } + if (toY - robot.getY() > 0) { + while (robot.getDirection() != Direction.UP) { + robot.turnLeft(); + } + while (toY - robot.getY() != 0) { + robot.stepForward(); + } + } + if (toY - robot.getY() < 0) { + while (robot.getDirection() != Direction.DOWN) { + robot.turnLeft(); + } + while (toY - robot.getY() != 0) { + robot.stepForward(); + } + } } }