From 118eaf0cd41f658759b53369c2ef5f6530b5fde9 Mon Sep 17 00:00:00 2001 From: Artem Hurievskyi Date: Mon, 27 Jan 2025 17:22:15 +0200 Subject: [PATCH] did smthing not clever in order to complete this task --- src/main/java/core/basesyntax/RobotRoute.java | 87 ++++++++++++++++++- 1 file changed, 86 insertions(+), 1 deletion(-) diff --git a/src/main/java/core/basesyntax/RobotRoute.java b/src/main/java/core/basesyntax/RobotRoute.java index 351ca4b9..d1819477 100644 --- a/src/main/java/core/basesyntax/RobotRoute.java +++ b/src/main/java/core/basesyntax/RobotRoute.java @@ -2,6 +2,91 @@ public class RobotRoute { public void moveRobot(Robot robot, int toX, int toY) { - //write your solution here + if (robot.getX() == toX && robot.getY() > toY) { + while (robot.getDirection() != Direction.UP) { + robot.turnLeft(); + } + while (robot.getY() != toY) { + robot.stepForward(); + } + } + if (robot.getX() == toX && robot.getY() < toY) { + while (robot.getDirection() != Direction.DOWN) { + robot.turnLeft(); + } + while (robot.getY() != toY) { + robot.stepForward(); + } + } + if (robot.getX() > toX && robot.getY() == toY) { + while (robot.getDirection() != Direction.LEFT) { + robot.turnLeft(); + } + while (robot.getX() != toX) { + robot.stepForward(); + } + } + if (robot.getX() < toX && robot.getY() == toY) { + while (robot.getDirection() != Direction.RIGHT) { + robot.turnLeft(); + } + while (robot.getY() != toX) { + robot.stepForward(); + } + } + if (robot.getX() > toX && robot.getY() > toY) { + while (robot.getDirection() != Direction.LEFT) { + robot.turnLeft(); + } + while (robot.getX() != toX) { + robot.stepForward(); + } + robot.turnLeft(); + while (robot.getY() != toY) { + robot.stepForward(); + } + } + if (robot.getX() > toX && robot.getY() < toY) { + while (robot.getDirection() != Direction.RIGHT) { + robot.turnLeft(); + } + while (robot.getX() != toX) { + robot.stepForward(); + } + robot.turnRight(); + while (robot.getY() != toY) { + robot.stepForward(); + } + } + if (robot.getX() < toX && robot.getY() < toY) { + while (robot.getDirection() != Direction.RIGHT) { + robot.turnLeft(); + } + while (robot.getX() != toX) { + robot.stepForward(); + } + robot.turnRight(); + while (robot.getY() != toY) { + robot.stepForward(); + } + } + if (robot.getX() < toX && robot.getY() > toY) { + while (robot.getDirection() != Direction.RIGHT) { + robot.turnLeft(); + } + while (robot.getX() != toX) { + robot.stepForward(); + } + robot.turnRight(); + while (robot.getY() != toY) { + robot.stepForward(); + } + } + } + + public static void main(String[] args) { + Robot robot = new Robot(Direction.UP, 1, 1); + RobotRoute robotRoute = new RobotRoute(); + robotRoute.moveRobot(robot, 7, -3); } }