diff --git a/src/main/java/core/basesyntax/Robot.java b/src/main/java/core/basesyntax/Robot.java index 30236e03..8121fc03 100644 --- a/src/main/java/core/basesyntax/Robot.java +++ b/src/main/java/core/basesyntax/Robot.java @@ -92,21 +92,22 @@ public void faceDown() { public void faceRight() { faceLeft(); - turnLeft(); - turnLeft(); + turnAround(); } public void faceLeft() { switch (getDirection()) { case UP -> turnLeft(); - case RIGHT -> { - turnLeft(); - turnLeft(); - } + case RIGHT -> turnAround(); case DOWN -> turnRight(); default -> { } } } + + public void turnAround() { + turnLeft(); + turnLeft(); + } } diff --git a/src/main/java/core/basesyntax/RobotRoute.java b/src/main/java/core/basesyntax/RobotRoute.java index 5c152385..01203405 100644 --- a/src/main/java/core/basesyntax/RobotRoute.java +++ b/src/main/java/core/basesyntax/RobotRoute.java @@ -5,21 +5,19 @@ public class RobotRoute { public void moveRobot(Robot robot, int toX, int toY) { - alignCoordinate(Robot::getX, robot, Robot::faceLeft, Robot::faceRight, toX); - alignCoordinate(Robot::getY, robot, Robot::faceDown, Robot::faceUp, toY); + alignCoordinate(Robot::getX, Robot::faceLeft, robot, toX); + alignCoordinate(Robot::getY, Robot::faceDown, robot, toY); } private void alignCoordinate(Function robotCoordinateFunction, - Robot robot, Consumer faceNegativeConsumer, - Consumer facePositiveConsumer, + Robot robot, int coordinate) { int robotCoordinate = robotCoordinateFunction.apply(robot); while (robotCoordinate != coordinate) { - if (robotCoordinate > coordinate) { - faceNegativeConsumer.accept(robot); - } else { - facePositiveConsumer.accept(robot); + faceNegativeConsumer.accept(robot); + if (robotCoordinate < coordinate) { + robot.turnAround(); } robot.stepForward(); robotCoordinate = robotCoordinateFunction.apply(robot);