From cafeb961339fc5082b2d3d876f3401fd0a978a2e Mon Sep 17 00:00:00 2001 From: Evhen Timofeiev Date: Fri, 2 Feb 2024 17:26:36 +0200 Subject: [PATCH] create robot navigator --- 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..a502aa10 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 + int currentX = robot.getX(); + int currentY = robot.getY(); + + while (currentX != toX || currentY != toY) { + if (currentX < toX) { + while (robot.getDirection() != Direction.RIGHT) { + robot.turnRight(); + } + robot.stepForward(); + currentX++; + } else if (currentX > toX) { + while (robot.getDirection() != Direction.LEFT) { + robot.turnLeft(); + } + robot.stepForward(); + currentX--; + } + if (currentY < toY) { + while (robot.getDirection() != Direction.UP) { + robot.turnRight(); + } + robot.stepForward(); + currentY++; + } else if (currentY > toY) { + while (robot.getDirection() != Direction.DOWN) { + robot.turnLeft(); + } + robot.stepForward(); + currentY--; + } + + } + } }