generated from mate-academy/jv-homework-template
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implementing logic in the RobotRoute
- Loading branch information
Showing
1 changed file
with
37 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,43 @@ | ||
package core.basesyntax; | ||
|
||
public class RobotRoute { | ||
|
||
public void moveRobot(Robot robot, int toX, int toY) { | ||
//write your solution here | ||
moveInXAxis(robot, toX); | ||
moveInYAxis(robot, toY); | ||
} | ||
|
||
private void moveInXAxis(Robot robot, int toX) { | ||
if (robot.getX() < toX) { | ||
turnToDirection(robot, Direction.RIGHT); | ||
moveToPosition(robot, toX, 'X'); | ||
} else if (robot.getX() > toX) { | ||
turnToDirection(robot, Direction.LEFT); | ||
moveToPosition(robot, toX, 'X'); | ||
} | ||
} | ||
|
||
private void moveInYAxis(Robot robot, int toY) { | ||
if (robot.getY() < toY) { | ||
turnToDirection(robot, Direction.UP); | ||
moveToPosition(robot, toY, 'Y'); | ||
} else if (robot.getY() > toY) { | ||
turnToDirection(robot, Direction.DOWN); | ||
moveToPosition(robot, toY, 'Y'); | ||
} | ||
} | ||
|
||
private void turnToDirection(Robot robot, Direction targetDirection) { | ||
while (robot.getDirection() != targetDirection) { | ||
robot.turnRight(); | ||
} | ||
} | ||
|
||
private void moveToPosition(Robot robot, int targetPosition, char axis) { | ||
while ((axis == 'X' && robot.getX() != targetPosition) | ||
|| (axis == 'Y' && robot.getY() != targetPosition)) { | ||
robot.stepForward(); | ||
} | ||
} | ||
} | ||
|