From ed126b86e8797fe2d655ba60571e94a7bb469c14 Mon Sep 17 00:00:00 2001 From: Havrylenko Volodymyr Date: Mon, 1 Jul 2024 18:47:26 +0300 Subject: [PATCH] home_task5_buildSuccess --- src/main/java/core/basesyntax/RobotRoute.java | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/main/java/core/basesyntax/RobotRoute.java b/src/main/java/core/basesyntax/RobotRoute.java index 351ca4b9..fb3a01a4 100644 --- a/src/main/java/core/basesyntax/RobotRoute.java +++ b/src/main/java/core/basesyntax/RobotRoute.java @@ -2,6 +2,23 @@ public class RobotRoute { public void moveRobot(Robot robot, int toX, int toY) { - //write your solution here + while (robot.getX() != toX || robot.getY() != toY) { + while (robot.getDirection() != getDirectionToTarget(robot, toX, toY)) { + robot.turnRight(); + } + robot.stepForward(); + } + } + + private Direction getDirectionToTarget(Robot robot, int toX, int toY) { + if (toY > robot.getY()) { + return Direction.UP; + } else if (toY < robot.getY()) { + return Direction.DOWN; + } else if (toX > robot.getX()) { + return Direction.RIGHT; + } else { + return Direction.LEFT; + } } }