Skip to content

Commit

Permalink
The jv-robot program is complete
Browse files Browse the repository at this point in the history
  • Loading branch information
APekariev committed Jun 4, 2024
1 parent 80be487 commit e0413ac
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/main/java/core/basesyntax/Robot.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ public int getY() {
return coordinateY;
}

public void setX(int coordinateX) {
this.coordinateX = coordinateX;
}

public void setY(int coordinateY) {
this.coordinateY = coordinateY;
}

public void turnLeft() {
switch (direction) {
case UP:
Expand Down
99 changes: 98 additions & 1 deletion src/main/java/core/basesyntax/RobotRoute.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,104 @@
package core.basesyntax;

public class RobotRoute {

public void moveRobot(Robot robot, int toX, int toY) {
//write your solution here
//write your solution here
if (robot.getX() <= toX && robot.getY() <= toY) {
switch (robot.getDirection()) {
case DOWN:
robot.turnLeft();
break;
case LEFT:
robot.turnLeft();
robot.turnLeft();
break;
case UP:
robot.turnRight();
break;
default:
break;
}

while (robot.getX() < toX) {
robot.stepForward();
}
robot.turnLeft();
while (robot.getY() < toY) {
robot.stepForward();
}
}
if (robot.getX() <= toX && robot.getY() >= toY) {
switch (robot.getDirection()) {
case DOWN:
robot.turnLeft();
break;
case LEFT:
robot.turnLeft();
robot.turnLeft();
break;
case UP:
robot.turnRight();
break;
default:
break;
}

while (robot.getX() < toX) {
robot.stepForward();
}
robot.turnRight();
while (robot.getY() > toY) {
robot.stepForward();
}
}
if (robot.getX() >= toX && robot.getY() <= toY) {
switch (robot.getDirection()) {
case DOWN:
robot.turnRight();
break;
case RIGHT:
robot.turnLeft();
robot.turnLeft();
break;
case UP:
robot.turnLeft();
break;
default:
break;
}

while (robot.getX() > toX) {
robot.stepForward();
}
robot.turnRight();
while (robot.getY() < toY) {
robot.stepForward();
}
}
if (robot.getX() >= toX && robot.getY() >= toY) {
switch (robot.getDirection()) {
case DOWN:
robot.turnRight();
break;
case RIGHT:
robot.turnLeft();
robot.turnLeft();
break;
case UP:
robot.turnLeft();
break;
default:
break;
}

while (robot.getX() > toX) {
robot.stepForward();
}
robot.turnLeft();
while (robot.getY() > toY) {
robot.stepForward();
}
}
}
}

0 comments on commit e0413ac

Please sign in to comment.