From dbc9ef9dd536ef085b7a3e6f1daa1420ff926cb4 Mon Sep 17 00:00:00 2001 From: Zorii Bohdan Date: Wed, 24 Jan 2024 13:47:44 +0200 Subject: [PATCH] fixed infinite loop issue --- src/main/java/core/basesyntax/RobotRoute.java | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/main/java/core/basesyntax/RobotRoute.java b/src/main/java/core/basesyntax/RobotRoute.java index 6c3abf4e..c6b400be 100644 --- a/src/main/java/core/basesyntax/RobotRoute.java +++ b/src/main/java/core/basesyntax/RobotRoute.java @@ -7,10 +7,14 @@ public void moveRobot(Robot robot, int toX, int toY) { } rotateRobot(robot, findHorizontalDirection(robot, toX)); - moveForward(robot, robot.getX(), toX); + while (robot.getX() != toX) { + robot.stepForward(); + } rotateRobot(robot, findVerticalDirection(robot, toY)); - moveForward(robot, robot.getY(), toY); + while (robot.getY() != toY) { + robot.stepForward(); + } } private Direction findHorizontalDirection(Robot robot, int targetX) { @@ -35,9 +39,4 @@ private void rotateRobot(Robot robot, Direction targetDirection) { } } - private void moveForward(Robot robot, int currentPosition, int targetPosition) { - while (currentPosition != targetPosition) { - robot.stepForward(); - } - } }