From 871276119f1f379f1ce494b9c1cf0b34ed063ae5 Mon Sep 17 00:00:00 2001 From: "Dymchenko, Mykola" Date: Sat, 12 Oct 2024 17:41:44 -0500 Subject: [PATCH 1/2] Added implementation to the methods: moveRobot(), add method moveRobotByAxis() in class Robot Route --- src/main/java/core/basesyntax/RobotRoute.java | 112 ++++++++++++++++++ 1 file changed, 112 insertions(+) diff --git a/src/main/java/core/basesyntax/RobotRoute.java b/src/main/java/core/basesyntax/RobotRoute.java index 351ca4b9..42f33b20 100644 --- a/src/main/java/core/basesyntax/RobotRoute.java +++ b/src/main/java/core/basesyntax/RobotRoute.java @@ -3,5 +3,117 @@ public class RobotRoute { public void moveRobot(Robot robot, int toX, int toY) { //write your solution here + if (robot.getX() == toX && robot.getY() == toY) { + System.out.println("Robot stays on the same place"); + } + if (toX > 0) { + if (robot.getDirection() == Direction.RIGHT) { + moveRobotByAxis(robot, robot.getX(), toX); + } else if (robot.getDirection() == Direction.LEFT) { + robot.turnLeft(); + robot.turnLeft(); + moveRobotByAxis(robot, robot.getX(), toX); + } else if (robot.getDirection() == Direction.DOWN) { + robot.turnLeft(); + moveRobotByAxis(robot, robot.getX(), toX); + } else { + robot.turnRight(); + moveRobotByAxis(robot, robot.getX(), toX); + } + } + if (toX < 0) { + if (robot.getDirection() == Direction.LEFT && robot.getX() > toX) { + moveRobotByAxis(robot, robot.getX(), toX); + } + if (robot.getDirection() == Direction.LEFT && robot.getX() < toX) { + robot.turnLeft(); + robot.turnLeft(); + moveRobotByAxis(robot, robot.getX(), toX); + } else if (robot.getDirection() == Direction.RIGHT && robot.getX() > toX) { + robot.turnRight(); + robot.turnRight(); + moveRobotByAxis(robot, robot.getX(), toX); + } else if (robot.getDirection() == Direction.RIGHT && robot.getX() < toX) { + moveRobotByAxis(robot, robot.getX(), toX); + } else if (robot.getDirection() == Direction.DOWN && robot.getX() > toX) { + robot.turnRight(); + moveRobotByAxis(robot, robot.getX(), toX); + } else if (robot.getDirection() == Direction.DOWN && robot.getX() < toX) { + robot.turnLeft(); + moveRobotByAxis(robot, robot.getX(), toX); + } else if (robot.getDirection() == Direction.UP && robot.getX() > toX) { + robot.turnLeft(); + moveRobotByAxis(robot, robot.getX(), toX); + } else if (robot.getDirection() == Direction.UP && robot.getX() < toX) { + robot.turnRight(); + moveRobotByAxis(robot, robot.getX(), toX); + } + } + if (toY < 0) { + if (robot.getDirection() == Direction.UP && robot.getY() < toY) { + moveRobotByAxis(robot, robot.getY(), toY); + } else if (robot.getDirection() == Direction.UP && robot.getY() > 0) { + robot.turnRight(); + robot.turnRight(); + moveRobotByAxis(robot, robot.getY(), toY); + } else if (robot.getDirection() == Direction.LEFT && robot.getY() < toY) { + robot.turnRight(); + moveRobotByAxis(robot, robot.getY(), toY); + } else if (robot.getDirection() == Direction.LEFT && robot.getY() > toY) { + robot.turnLeft(); + moveRobotByAxis(robot, robot.getY(), toY); + } else if (robot.getDirection() == Direction.DOWN && robot.getY() > toY) { + moveRobotByAxis(robot, robot.getY(), toY); + } else if (robot.getDirection() == Direction.DOWN && robot.getY() < toY) { + robot.turnLeft(); + robot.turnLeft(); + moveRobotByAxis(robot, robot.getY(), toY); + } else if (robot.getDirection() == Direction.RIGHT && robot.getY() > toY) { + robot.turnRight(); + moveRobotByAxis(robot, robot.getY(), toY); + } else { + robot.turnLeft(); + moveRobotByAxis(robot, robot.getY(), toY); + } + } + if (toY > 0) { + if (robot.getDirection() == Direction.UP && robot.getY() < toY) { + moveRobotByAxis(robot, robot.getY(), toY); + } else if (robot.getDirection() == Direction.UP && robot.getY() > toY) { + robot.turnLeft(); + robot.turnLeft(); + moveRobotByAxis(robot, robot.getY(), toY); + } else if (robot.getDirection() == Direction.RIGHT && robot.getY() < toY) { + robot.turnLeft(); + moveRobotByAxis(robot, robot.getY(), toY); + } else if (robot.getDirection() == Direction.RIGHT && robot.getY() > toY) { + robot.turnRight(); + moveRobotByAxis(robot, robot.getY(), toY); + } else if (robot.getDirection() == Direction.DOWN && robot.getY() < toY) { + robot.turnLeft(); + robot.turnLeft(); + moveRobotByAxis(robot, robot.getY(), toY); + } else if (robot.getDirection() == Direction.DOWN && robot.getY() > toY) { + moveRobotByAxis(robot, robot.getY(), toY); + } else if (robot.getDirection() == Direction.LEFT && robot.getY() < toY) { + robot.turnRight(); + moveRobotByAxis(robot, robot.getY(), toY); + } else if (robot.getDirection() == Direction.LEFT && robot.getY() > toY) { + robot.turnLeft(); + moveRobotByAxis(robot, robot.getY(), toY); + } + } + } + + private void moveRobotByAxis(Robot robot, int coordinate, int coordinateTo) { + if (coordinate > coordinateTo) { + for (int i = coordinate; i > coordinateTo; i--) { + robot.stepForward(); + } + } else { + for (int i = coordinate; i < coordinateTo; i++) { + robot.stepForward(); + } + } } } From 35ff3f7ac15d4c5290b27cd8b247629c24ab85a0 Mon Sep 17 00:00:00 2001 From: "Dymchenko, Mykola" Date: Sat, 12 Oct 2024 17:45:23 -0500 Subject: [PATCH 2/2] Removed comment --- src/main/java/core/basesyntax/RobotRoute.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/core/basesyntax/RobotRoute.java b/src/main/java/core/basesyntax/RobotRoute.java index 42f33b20..6d86b7a5 100644 --- a/src/main/java/core/basesyntax/RobotRoute.java +++ b/src/main/java/core/basesyntax/RobotRoute.java @@ -2,7 +2,6 @@ public class RobotRoute { public void moveRobot(Robot robot, int toX, int toY) { - //write your solution here if (robot.getX() == toX && robot.getY() == toY) { System.out.println("Robot stays on the same place"); }