From 00d0dde27d136b244895795e6d519b1036451c62 Mon Sep 17 00:00:00 2001 From: Roman Leshchyshyn Date: Sat, 12 Oct 2024 11:07:34 +0300 Subject: [PATCH] f --- src/main/java/core/basesyntax/RobotRoute.java | 30 +++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/src/main/java/core/basesyntax/RobotRoute.java b/src/main/java/core/basesyntax/RobotRoute.java index 351ca4b9..26027511 100644 --- a/src/main/java/core/basesyntax/RobotRoute.java +++ b/src/main/java/core/basesyntax/RobotRoute.java @@ -2,6 +2,32 @@ public class RobotRoute { public void moveRobot(Robot robot, int toX, int toY) { - //write your solution here + moveHorizontally(robot, toX); + moveVertically(robot, toY); } -} + private void moveHorizontally(Robot robot, int toX) { + if (robot.getX() < toX) { + turnRobotToDirection(robot, Direction.RIGHT); + while (robot.getX() < toX) { + robot.stepForward(); + } + } else if (robot.getX() > toX) { + turnRobotToDirection(robot, Direction.LEFT); + while (robot.getX() > toX) { + robot.stepForward(); + } + } + } + private void moveVertically(Robot robot, int toY) { + if (robot.getY() < toY) { + turnRobotToDirection(robot, Direction.UP); + while (robot.getY() < toY) { + robot.stepForward(); + } + } + } private void turnRobotToDirection(Robot robot, Direction targetDirection) { + while (robot.getDirection() != targetDirection) { + robot.turnRight(); + } + } +} \ No newline at end of file