From 7e0cc19335cf8fad6ccdd702dab85b73c4cbecf3 Mon Sep 17 00:00:00 2001 From: Nataliya Plastun Date: Tue, 16 Apr 2024 21:06:06 +0300 Subject: [PATCH] commit robot route --- src/main/java/core/basesyntax/RobotRoute.java | 46 ++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/src/main/java/core/basesyntax/RobotRoute.java b/src/main/java/core/basesyntax/RobotRoute.java index 351ca4b9..428f8895 100644 --- a/src/main/java/core/basesyntax/RobotRoute.java +++ b/src/main/java/core/basesyntax/RobotRoute.java @@ -2,6 +2,50 @@ public class RobotRoute { public void moveRobot(Robot robot, int toX, int toY) { - //write your solution here + int currentX = robot.getX(); + int currentY = robot.getY(); + + // Calculate the differences in X and Y coordinates + int diffX = toX - currentX; + int diffY = toY - currentY; + + // Move in the X direction + while (currentX != toX) { + if (currentX < toX) { + // Turn right to face towards positive X + while (robot.getDirection() != Direction.RIGHT) { + robot.turnRight(); + } + robot.stepForward(); // Move one step in the X direction + currentX++; + } else { + // Turn left to face towards negative X + while (robot.getDirection() != Direction.LEFT) { + robot.turnLeft(); + } + robot.stepForward(); // Move one step in the X direction + currentX--; + } + } + + // Move in the Y direction + while (currentY != toY) { + if (currentY < toY) { + // Turn right to face towards positive Y + while (robot.getDirection() != Direction.UP) { + robot.turnRight(); + } + robot.stepForward(); // Move one step in the Y direction + currentY++; + } else { + // Turn right to face towards negative Y + while (robot.getDirection() != Direction.DOWN) { + robot.turnRight(); + } + robot.stepForward(); // Move one step in the Y direction + currentY--; + } + } } } +