From 7ebd67911d92f2505841be17edd64385d7720846 Mon Sep 17 00:00:00 2001 From: rainett <96131229+rainett@users.noreply.github.com> Date: Sat, 3 Aug 2024 15:12:45 +0300 Subject: [PATCH] optimized function usage --- src/main/java/core/basesyntax/Robot.java | 13 +++++++------ src/main/java/core/basesyntax/RobotRoute.java | 14 ++++++-------- 2 files changed, 13 insertions(+), 14 deletions(-) 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);