Skip to content

Commit

Permalink
added solution for the moveRobot method
Browse files Browse the repository at this point in the history
  • Loading branch information
AgroFix committed Sep 28, 2023
1 parent 3e4e14b commit e334391
Showing 1 changed file with 40 additions and 1 deletion.
41 changes: 40 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,45 @@

public class RobotRoute {
public void moveRobot(Robot robot, int toX, int toY) {
//write your solution here
int stepsX = Math.abs(robot.getX() - toX);

changeDirectionX(robot, toX);
for (int i = 0; i < stepsX; i++) {
robot.stepForward();
}

int stepsY = Math.abs(robot.getY() - toY);

changeDirectionY(robot,toY);
for (int i = 0; i < stepsY; i++) {
robot.stepForward();
}

}

private void changeDirectionX(Robot robot, int toX) {
if (robot.getX() < toX) {
while (!robot.getDirection().equals(Direction.RIGHT)) {
robot.turnRight();
}
}
if (robot.getX() > toX) {
while (!robot.getDirection().equals(Direction.LEFT)) {
robot.turnLeft();
}
}
}

private void changeDirectionY(Robot robot, int toY) {
if (robot.getY() < toY) {
while (!robot.getDirection().equals(Direction.UP)) {
robot.turnRight();
}
}
if (robot.getY() > toY) {
while (!robot.getDirection().equals(Direction.DOWN)) {
robot.turnLeft();
}
}
}
}

0 comments on commit e334391

Please sign in to comment.