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.
- Loading branch information
Showing
1 changed file
with
36 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,42 @@ | ||
package core.basesyntax; | ||
|
||
import java.util.Objects; | ||
|
||
public class RobotRoute { | ||
public void moveRobot(Robot robot, int toX, int toY) { | ||
//write your solution here | ||
int stepsX = setDirectionAndStepsX(robot, toX); | ||
for (int i = 0; i < stepsX; i++) { | ||
robot.stepForward(); | ||
} | ||
int stepsY = setDirectionAndStepsY(robot, toY); | ||
for (int i = 0; i < stepsY; i++) { | ||
robot.stepForward(); | ||
} | ||
} | ||
|
||
public static int setDirectionAndStepsX(Robot robot, int toX) { | ||
if (robot.getX() < toX) { | ||
while (!Objects.equals(robot.getDirection().toString(), "RIGHT")) { | ||
robot.turnRight(); | ||
} | ||
} else { | ||
while (!Objects.equals(robot.getDirection().toString(), "LEFT")) { | ||
robot.turnRight(); | ||
} | ||
} | ||
return Math.max(robot.getX(), toX) - Math.min(robot.getX(), toX); | ||
} | ||
|
||
public static int setDirectionAndStepsY(Robot robot, int toY) { | ||
if (robot.getY() < toY) { | ||
while (!Objects.equals(robot.getDirection().toString(), "UP")) { | ||
robot.turnRight(); | ||
} | ||
} else { | ||
while (!Objects.equals(robot.getDirection().toString(), "DOWN")) { | ||
robot.turnRight(); | ||
} | ||
} | ||
return Math.max(robot.getY(), toY) - Math.min(robot.getY(), toY); | ||
} | ||
} |