Skip to content

Commit

Permalink
Initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
Wojtek-A-JAVA committed Dec 7, 2024
1 parent 80be487 commit 7e33b43
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/main/java/core/basesyntax/Robot.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ public void turnRight() {
}
}

public void initialDirection() {
direction = Direction.UP;
}

public void stepForward() {
switch (direction) {
case UP:
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/core/basesyntax/RobotRoute.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,26 @@
public class RobotRoute {
public void moveRobot(Robot robot, int toX, int toY) {
//write your solution here
robot.initialDirection();
int xSteps = toX - robot.getX();
int ySteps = toY - robot.getY();
if (toX < 0) {
robot.turnLeft();
}
if (toX > 0) {
robot.turnRight();
}
if (xSteps < 0) xSteps *= -1;
for (int i = 0; i < xSteps; i++) robot.stepForward();
if (toY > 0 && ySteps < 0 && toX > 0 || toY > 0 && ySteps > 0 && toX < 0 ||
toY < 0 && ySteps > 0 && toX < 0 || toY < 0 && ySteps < 0 && toX > 0) {
robot.turnRight();
}
if (toY > 0 && ySteps > 0 && toX > 0 || toY > 0 && ySteps < 0 && toX < 0 ||
toY < 0 && ySteps < 0 && toX < 0 || toY < 0 && ySteps > 0 && toX > 0) {
robot.turnLeft();
}
if (ySteps < 0) ySteps *= -1;
for (int i = 0; i < ySteps; i++) robot.stepForward();
}
}

0 comments on commit 7e33b43

Please sign in to comment.