Skip to content

Commit

Permalink
created moveRobot method
Browse files Browse the repository at this point in the history
  • Loading branch information
sannchous committed Oct 8, 2024
1 parent 80be487 commit 84fe80f
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion src/main/java/core/basesyntax/RobotRoute.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,30 @@

public class RobotRoute {
public void moveRobot(Robot robot, int toX, int toY) {
//write your solution here
while (robot.getX() != toX || robot.getY() != toY) {
int subX = toX - robot.getX();
int subY = toY - robot.getY();

Direction needDirection = robot.getDirection();

if (subX > 0) {
needDirection = Direction.RIGHT;
} else if (subX < 0) {
needDirection = Direction.LEFT;
} else if (subY > 0) {
needDirection = Direction.UP;
} else if (subY < 0) {
needDirection = Direction.DOWN;
}
turnRobotToDirection(robot, needDirection);

robot.stepForward();
}
}

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

0 comments on commit 84fe80f

Please sign in to comment.