Skip to content

Commit

Permalink
fixed infinite loop issue
Browse files Browse the repository at this point in the history
  • Loading branch information
BohdanZorii committed Jan 24, 2024
1 parent b155bec commit dbc9ef9
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions src/main/java/core/basesyntax/RobotRoute.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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();
}
}
}

0 comments on commit dbc9ef9

Please sign in to comment.