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

Hw 03 jv robot #1162

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
19 changes: 19 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,25 @@ public void turnRight() {
}
}

public void turnAround() {
switch (direction) {
case UP:
direction = Direction.DOWN;
break;
case LEFT:
direction = Direction.RIGHT;
break;
case DOWN:
direction = Direction.UP;
break;
case RIGHT:
direction = Direction.LEFT;
break;
default:
break;
}
}

public void stepForward() {
switch (direction) {
case UP:
Expand Down
109 changes: 108 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,113 @@

public class RobotRoute {
public void moveRobot(Robot robot, int toX, int toY) {
//write your solution here
final int actX = robot.getX();
final int actY = robot.getY();
if (toX != actX || toY != actY) {
if ((toX > actX)) {
moveRight(robot, actX, toX);
} else {
moveLeft(robot, actX, toX);
}
if (toY > actY) {
moveUp(robot, actY, toY);
} else {
moveDown(robot, actY, toY);
}
}
}

private static void moveRight(Robot robot, int actX, int toX) {
switch (robot.getDirection()) {
case UP:
robot.turnRight();
countStepsForward(robot, toX, actX);
break;
case LEFT:
robot.turnAround();
countStepsForward(robot, toX, actX);
break;
case DOWN:
robot.turnLeft();
countStepsForward(robot, toX, actX);
break;
case RIGHT:
countStepsForward(robot, toX, actX);
break;
default:
break;
}
}

private static void moveLeft(Robot robot, int actX, int toX) {
switch (robot.getDirection()) {
case UP:
robot.turnLeft();
countStepsForward(robot, actX, toX);
break;
case LEFT:
countStepsForward(robot, actX, toX);
break;
case DOWN:
robot.turnRight();
countStepsForward(robot, actX, toX);
break;
case RIGHT:
robot.turnAround();
countStepsForward(robot, actX, toX);
break;
default:
break;
}
}

private static void moveUp(Robot robot, int actY, int toY) {
switch (robot.getDirection()) {
case UP:
countStepsForward(robot, toY, actY);
break;
case LEFT:
robot.turnRight();
countStepsForward(robot, toY, actY);
break;
case DOWN:
robot.turnAround();
countStepsForward(robot, toY, actY);
break;
case RIGHT:
robot.turnLeft();
countStepsForward(robot, toY, actY);
break;
default:
break;
}
}

private static void moveDown(Robot robot, int actY, int toY) {
switch (robot.getDirection()) {
case UP:
robot.turnAround();
countStepsForward(robot, actY, toY);
break;
case LEFT:
robot.turnLeft();
countStepsForward(robot, actY, toY);
break;
case DOWN:
countStepsForward(robot, actY, toY);
break;
case RIGHT:
robot.turnRight();
countStepsForward(robot, actY, toY);
break;
default:
break;
}
}

private static void countStepsForward(Robot robot, int begin, int end) {
for (int i = 0; i < (begin - end); i++) {
robot.stepForward();
}
}
}
Loading