From 7652f859d7cd2b9830fcf38680c6674daa4672d8 Mon Sep 17 00:00:00 2001 From: Stupak Serhii Date: Mon, 8 Jul 2024 13:11:42 +0300 Subject: [PATCH] Complete jv-robot --- src/main/java/core/basesyntax/RobotRoute.java | 79 ++++++++++++++++++- 1 file changed, 78 insertions(+), 1 deletion(-) diff --git a/src/main/java/core/basesyntax/RobotRoute.java b/src/main/java/core/basesyntax/RobotRoute.java index 351ca4b9..3cb9632b 100644 --- a/src/main/java/core/basesyntax/RobotRoute.java +++ b/src/main/java/core/basesyntax/RobotRoute.java @@ -2,6 +2,83 @@ public class RobotRoute { public void moveRobot(Robot robot, int toX, int toY) { - //write your solution here + if (!(robot.getX() == toX && robot.getY() == toY)) { + if (robot.getX() < toX) { + switch (robot.getDirection()) { + case UP: + robot.turnRight(); + break; + case LEFT: + robot.turnRight(); + robot.turnRight(); + break; + case DOWN: + robot.turnLeft(); + break; + case RIGHT: + break; + default: + break; + } + } else if (robot.getX() > toX) { + switch (robot.getDirection()) { + case UP: + robot.turnLeft(); + break; + case LEFT: + break; + case DOWN: + robot.turnRight(); + break; + case RIGHT: + robot.turnRight(); + robot.turnRight(); + break; + default: + break; + } + } + while (robot.getX() != toX) { + robot.stepForward(); + } + if (robot.getY() < toY) { + switch (robot.getDirection()) { + case UP: + break; + case LEFT: + robot.turnRight(); + break; + case DOWN: + robot.turnLeft(); + robot.turnLeft(); + break; + case RIGHT: + robot.turnLeft(); + break; + default: + break; + } + } else if (robot.getY() > toY) { + switch (robot.getDirection()) { + case UP: + robot.turnLeft(); + robot.turnLeft(); + break; + case LEFT: + robot.turnLeft(); + break; + case DOWN: + break; + case RIGHT: + robot.turnRight(); + break; + default: + break; + } + } + while (robot.getY() != toY) { + robot.stepForward(); + } + } } }