From 0bad2ec08db137abe6b04c4028d1e181d660be5a Mon Sep 17 00:00:00 2001 From: Volodymyr Pryt Date: Fri, 15 Nov 2024 12:59:00 +0200 Subject: [PATCH] myRobot --- src/main/java/core/basesyntax/RobotRoute.java | 35 ++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/src/main/java/core/basesyntax/RobotRoute.java b/src/main/java/core/basesyntax/RobotRoute.java index 351ca4b9..e25a4086 100644 --- a/src/main/java/core/basesyntax/RobotRoute.java +++ b/src/main/java/core/basesyntax/RobotRoute.java @@ -2,6 +2,39 @@ public class RobotRoute { public void moveRobot(Robot robot, int toX, int toY) { - //write your solution here + if (robot.getX() != toX) { + if (robot.getX() < toX) { + while (robot.getDirection() != Direction.RIGHT) { + robot.turnRight(); + } + while (robot.getX() < toX) { + robot.stepForward(); + } + } else if (robot.getX() > toX) { + while (robot.getDirection() != Direction.LEFT) { + robot.turnRight(); + } + while (robot.getX() > toX) { + robot.stepForward(); + } + } + } + if (robot.getY() != toY) { + if (robot.getY() < toY) { + while (robot.getDirection() != Direction.UP) { + robot.turnRight(); + } + while (robot.getY() < toY) { + robot.stepForward(); + } + } else if (robot.getY() > toY) { + while (robot.getDirection() != Direction.DOWN) { + robot.turnRight(); + } + while (robot.getY() > toY) { + robot.stepForward(); + } + } + } } }