From e8b85afd7b8fd397c82cbfa7df3e14e93e67a472 Mon Sep 17 00:00:00 2001 From: Polina Kirf Date: Wed, 5 Feb 2025 19:35:35 -1200 Subject: [PATCH 1/2] robot route --- src/main/java/core/basesyntax/Main.java | 19 ++++++++++++ src/main/java/core/basesyntax/RobotRoute.java | 31 +++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 src/main/java/core/basesyntax/Main.java diff --git a/src/main/java/core/basesyntax/Main.java b/src/main/java/core/basesyntax/Main.java new file mode 100644 index 00000000..fad2160a --- /dev/null +++ b/src/main/java/core/basesyntax/Main.java @@ -0,0 +1,19 @@ +package core.basesyntax; + +import java.util.Random; +public class Main { + public static void main(String[] args) { + Random random = new Random(); + Robot robot = new Robot(Direction.UP, 0, 0); + RobotRoute route = new RobotRoute(); + + int toX = random.nextInt(21) - 10; + int toY = random.nextInt(21) - 10; + + route.moveRobot(robot, toX, toY); + + System.out.println("Starting position: " + robot); + System.out.println("Target coordinates: (" + toX + ", " + toY + ")"); + System.out.println("After moving: " + robot); + } +} diff --git a/src/main/java/core/basesyntax/RobotRoute.java b/src/main/java/core/basesyntax/RobotRoute.java index 351ca4b9..73d15dda 100644 --- a/src/main/java/core/basesyntax/RobotRoute.java +++ b/src/main/java/core/basesyntax/RobotRoute.java @@ -3,5 +3,36 @@ public class RobotRoute { public void moveRobot(Robot robot, int toX, int toY) { //write your solution here + int currentX = robot.getX(); + int currentY = robot.getY(); + + if (toX > currentX) { + turnToDirection(robot, Direction.RIGHT); + while (robot.getX() < toX) { + robot.stepForward(); + } + } else if (toX < currentX) { + turnToDirection(robot, Direction.LEFT); + while (robot.getX() > toX) { + robot.stepForward(); + } + } + if (toY > currentY) { + turnToDirection(robot, Direction.UP); + while (robot.getY() < toY) { + robot.stepForward(); + } + } else if (toY < currentY) { + turnToDirection(robot, Direction.DOWN); + while (robot.getY() > toY) { + robot.stepForward(); + } + } + } + private void turnToDirection(Robot robot, Direction targetDirection) { + while (robot.getDirection() != targetDirection) { + robot.turnLeft(); + robot.turnRight(); + } } } From a63a16b474f71d3e02eb496e3163f07d5d249ff4 Mon Sep 17 00:00:00 2001 From: Polina Kirf Date: Wed, 5 Feb 2025 19:39:17 -1200 Subject: [PATCH 2/2] robot route --- src/main/java/core/basesyntax/Main.java | 1 + src/main/java/core/basesyntax/RobotRoute.java | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/java/core/basesyntax/Main.java b/src/main/java/core/basesyntax/Main.java index fad2160a..152779f5 100644 --- a/src/main/java/core/basesyntax/Main.java +++ b/src/main/java/core/basesyntax/Main.java @@ -1,6 +1,7 @@ package core.basesyntax; import java.util.Random; + public class Main { public static void main(String[] args) { Random random = new Random(); diff --git a/src/main/java/core/basesyntax/RobotRoute.java b/src/main/java/core/basesyntax/RobotRoute.java index 73d15dda..0ad75c58 100644 --- a/src/main/java/core/basesyntax/RobotRoute.java +++ b/src/main/java/core/basesyntax/RobotRoute.java @@ -2,7 +2,7 @@ public class RobotRoute { public void moveRobot(Robot robot, int toX, int toY) { - //write your solution here + //write your solution here int currentX = robot.getX(); int currentY = robot.getY(); @@ -29,6 +29,7 @@ public void moveRobot(Robot robot, int toX, int toY) { } } } + private void turnToDirection(Robot robot, Direction targetDirection) { while (robot.getDirection() != targetDirection) { robot.turnLeft();