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

commit robot route #1123

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
46 changes: 45 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,50 @@

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

// Calculate the differences in X and Y coordinates
int diffX = toX - currentX;
int diffY = toY - currentY;

// Move in the X direction
while (currentX != toX) {
if (currentX < toX) {
// Turn right to face towards positive X
while (robot.getDirection() != Direction.RIGHT) {
robot.turnRight();
}
robot.stepForward(); // Move one step in the X direction
currentX++;
} else {
// Turn left to face towards negative X
while (robot.getDirection() != Direction.LEFT) {
robot.turnLeft();
}
robot.stepForward(); // Move one step in the X direction
currentX--;
}
}

// Move in the Y direction
while (currentY != toY) {
if (currentY < toY) {
// Turn right to face towards positive Y
while (robot.getDirection() != Direction.UP) {
robot.turnRight();
}
robot.stepForward(); // Move one step in the Y direction
currentY++;
} else {
// Turn right to face towards negative Y
while (robot.getDirection() != Direction.DOWN) {
robot.turnRight();
}
robot.stepForward(); // Move one step in the Y direction
currentY--;
}
}
}
}

Loading