Skip to content

Commit

Permalink
implementing logic in the RobotRoute
Browse files Browse the repository at this point in the history
  • Loading branch information
pMukuta committed Nov 12, 2024
1 parent 80be487 commit 7e4c536
Showing 1 changed file with 37 additions and 1 deletion.
38 changes: 37 additions & 1 deletion src/main/java/core/basesyntax/RobotRoute.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,43 @@
package core.basesyntax;

public class RobotRoute {

public void moveRobot(Robot robot, int toX, int toY) {
//write your solution here
moveInXAxis(robot, toX);
moveInYAxis(robot, toY);
}

private void moveInXAxis(Robot robot, int toX) {
if (robot.getX() < toX) {
turnToDirection(robot, Direction.RIGHT);
moveToPosition(robot, toX, 'X');
} else if (robot.getX() > toX) {
turnToDirection(robot, Direction.LEFT);
moveToPosition(robot, toX, 'X');
}
}

private void moveInYAxis(Robot robot, int toY) {
if (robot.getY() < toY) {
turnToDirection(robot, Direction.UP);
moveToPosition(robot, toY, 'Y');
} else if (robot.getY() > toY) {
turnToDirection(robot, Direction.DOWN);
moveToPosition(robot, toY, 'Y');
}
}

private void turnToDirection(Robot robot, Direction targetDirection) {
while (robot.getDirection() != targetDirection) {
robot.turnRight();
}
}

private void moveToPosition(Robot robot, int targetPosition, char axis) {
while ((axis == 'X' && robot.getX() != targetPosition)
|| (axis == 'Y' && robot.getY() != targetPosition)) {
robot.stepForward();
}
}
}

0 comments on commit 7e4c536

Please sign in to comment.