Skip to content

Commit

Permalink
solution java robot
Browse files Browse the repository at this point in the history
  • Loading branch information
VdBondarev committed Nov 30, 2023
1 parent 80be487 commit 54bf806
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 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,24 @@

public class RobotRoute {
public void moveRobot(Robot robot, int toX, int toY) {
//write your solution here
if (robot.getY() == toY && robot.getX() == toX) {
return;
}
Direction directionX = robot.getX() > toX ? Direction.LEFT : Direction.RIGHT;
Direction directionY = robot.getY() > toY ? Direction.DOWN : Direction.UP;

while (robot.getDirection() != directionX) {
robot.turnLeft();
}
while (robot.getX() != toX) {
robot.stepForward();
}
while (robot.getDirection() != directionY) {
robot.turnLeft();
}
while (robot.getY() != toY) {
robot.stepForward();
}

}
}

0 comments on commit 54bf806

Please sign in to comment.