Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented method moveRobot #1260

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 88 additions & 1 deletion src/main/java/core/basesyntax/RobotRoute.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,94 @@
package core.basesyntax;

public class RobotRoute {
private Robot robot;

private void turnUp() {
switch (robot.getDirection()) {
case DOWN:
robot.turnRight();
robot.turnRight();
break;
case LEFT:
robot.turnRight();
break;
case RIGHT:
robot.turnLeft();
break;
default:
break;
}
}

private void turnDown() {
switch (robot.getDirection()) {
case UP:
robot.turnRight();
robot.turnRight();
break;
case LEFT:
robot.turnLeft();
break;
case RIGHT:
robot.turnRight();
break;
default:
break;
}
}

private void turnLeft() {
switch (robot.getDirection()) {
case RIGHT:
robot.turnRight();
robot.turnRight();
break;
case UP:
robot.turnRight();
break;
case DOWN:
robot.turnLeft();
break;
default:
break;
}
}

private void turnRight() {
switch (robot.getDirection()) {
case LEFT:
robot.turnRight();
robot.turnRight();
break;
case UP:
robot.turnLeft();
break;
case DOWN:
robot.turnRight();
break;
default:
break;
}
}

public void moveRobot(Robot robot, int toX, int toY) {
//write your solution here
this.robot = robot;
Direction direction = robot.getDirection();
if (robot.getX() < toX) {
turnRight();
} else {
turnLeft();
}
while (robot.getX() != toX) {
robot.stepForward();
}
if (robot.getY() < toY) {
turnUp();
} else {
turnDown();
}
while (robot.getY() != toY) {
robot.stepForward();
}
}
}
Loading