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

initial commit(not tested, tests loaded infinetely) #1030

Open
wants to merge 2 commits 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
37 changes: 36 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,41 @@

public class RobotRoute {
public void moveRobot(Robot robot, int toX, int toY) {
//write your solution here
if (robot.getX() == toX && robot.getY() == toY) {
return;
}

rotateRobot(robot, findHorizontalDirection(robot, toX));
while (robot.getX() != toX) {
robot.stepForward();
}

rotateRobot(robot, findVerticalDirection(robot, toY));
while (robot.getY() != toY) {
robot.stepForward();
}
}

private Direction findHorizontalDirection(Robot robot, int targetX) {
if (robot.getX() < targetX) {
return Direction.RIGHT;
} else {
return Direction.LEFT;
}
}

private Direction findVerticalDirection(Robot robot, int targetY) {
if (robot.getY() < targetY) {
return Direction.UP;
} else {
return Direction.DOWN;
}
}

private void rotateRobot(Robot robot, Direction targetDirection) {
while (robot.getDirection() != targetDirection) {
robot.turnLeft();
}
}

}
Loading