From d995a79eb214a5f803db97b35eba6debb0725165 Mon Sep 17 00:00:00 2001 From: Tetiana Hrytsenko Date: Wed, 19 Jun 2024 14:17:38 +0200 Subject: [PATCH] initial commit --- src/main/java/core/basesyntax/RobotRoute.java | 33 ++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/src/main/java/core/basesyntax/RobotRoute.java b/src/main/java/core/basesyntax/RobotRoute.java index 351ca4b9..bbd85ded 100644 --- a/src/main/java/core/basesyntax/RobotRoute.java +++ b/src/main/java/core/basesyntax/RobotRoute.java @@ -2,6 +2,37 @@ public class RobotRoute { public void moveRobot(Robot robot, int toX, int toY) { - //write your solution here + findValidDirectionX(robot, toX); + while (robot.getX() != toX) { + robot.stepForward(); + } + findValidDirectionY(robot, toY); + while (robot.getY() != toY) { + robot.stepForward(); + } + } + + private void findValidDirectionX(Robot robot, int toX) { + if (robot.getX() < toX) { + while (robot.getDirection() != Direction.RIGHT) { + robot.turnRight(); + } + } else if (robot.getX() > toX) { + while (robot.getDirection() != Direction.LEFT) { + robot.turnRight(); + } + } + } + + private void findValidDirectionY(Robot robot, int toY) { + if (robot.getY() < toY) { + while (robot.getDirection() != Direction.UP) { + robot.turnRight(); + } + } else if (robot.getY() > toY) { + while (robot.getDirection() != Direction.DOWN) { + robot.turnRight(); + } + } } }