Skip to content

Commit

Permalink
added jv-robot task solution
Browse files Browse the repository at this point in the history
  • Loading branch information
BolsunovaNataliia committed Sep 21, 2023
1 parent 3e4e14b commit 02b580d
Showing 1 changed file with 38 additions and 1 deletion.
39 changes: 38 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,43 @@

public class RobotRoute {
public void moveRobot(Robot robot, int toX, int toY) {
//write your solution here
gorizontalMove(robot, toX);
verticalMove(robot, toY);
}

private void verticalMove(Robot robot, int toY) {
if ((robot.getY() - toY) > 0) {
while (robot.getDirection() != Direction.DOWN) {
robot.turnRight();
}
while (robot.getY() - toY > 0) {
robot.stepForward();
}
} else {
while (robot.getDirection() != Direction.UP) {
robot.turnRight();
}
while (robot.getY() - toY < 0) {
robot.stepForward();
}
}
}

private void gorizontalMove(Robot robot, int toX) {
if ((robot.getX() - toX) > 0) {
while (robot.getDirection() != Direction.LEFT) {
robot.turnLeft();
}
while (robot.getX() - toX > 0) {
robot.stepForward();
}
} else {
while (robot.getDirection() != Direction.RIGHT) {
robot.turnLeft();
}
while (robot.getX() - toX < 0) {
robot.stepForward();
}
}
}
}

0 comments on commit 02b580d

Please sign in to comment.