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
2 changed files
with
106 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
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,104 @@ | ||
package core.basesyntax; | ||
|
||
public class RobotRoute { | ||
|
||
public void moveRobot(Robot robot, int toX, int toY) { | ||
//write your solution here | ||
//write your solution here | ||
if (robot.getX() <= toX && robot.getY() <= toY) { | ||
switch (robot.getDirection()) { | ||
case DOWN: | ||
robot.turnLeft(); | ||
break; | ||
case LEFT: | ||
robot.turnLeft(); | ||
robot.turnLeft(); | ||
break; | ||
case UP: | ||
robot.turnRight(); | ||
break; | ||
default: | ||
break; | ||
} | ||
|
||
while (robot.getX() < toX) { | ||
robot.stepForward(); | ||
} | ||
robot.turnLeft(); | ||
while (robot.getY() < toY) { | ||
robot.stepForward(); | ||
} | ||
} | ||
if (robot.getX() <= toX && robot.getY() >= toY) { | ||
switch (robot.getDirection()) { | ||
case DOWN: | ||
robot.turnLeft(); | ||
break; | ||
case LEFT: | ||
robot.turnLeft(); | ||
robot.turnLeft(); | ||
break; | ||
case UP: | ||
robot.turnRight(); | ||
break; | ||
default: | ||
break; | ||
} | ||
|
||
while (robot.getX() < toX) { | ||
robot.stepForward(); | ||
} | ||
robot.turnRight(); | ||
while (robot.getY() > toY) { | ||
robot.stepForward(); | ||
} | ||
} | ||
if (robot.getX() >= toX && robot.getY() <= toY) { | ||
switch (robot.getDirection()) { | ||
case DOWN: | ||
robot.turnRight(); | ||
break; | ||
case RIGHT: | ||
robot.turnLeft(); | ||
robot.turnLeft(); | ||
break; | ||
case UP: | ||
robot.turnLeft(); | ||
break; | ||
default: | ||
break; | ||
} | ||
|
||
while (robot.getX() > toX) { | ||
robot.stepForward(); | ||
} | ||
robot.turnRight(); | ||
while (robot.getY() < toY) { | ||
robot.stepForward(); | ||
} | ||
} | ||
if (robot.getX() >= toX && robot.getY() >= toY) { | ||
switch (robot.getDirection()) { | ||
case DOWN: | ||
robot.turnRight(); | ||
break; | ||
case RIGHT: | ||
robot.turnLeft(); | ||
robot.turnLeft(); | ||
break; | ||
case UP: | ||
robot.turnLeft(); | ||
break; | ||
default: | ||
break; | ||
} | ||
|
||
while (robot.getX() > toX) { | ||
robot.stepForward(); | ||
} | ||
robot.turnLeft(); | ||
while (robot.getY() > toY) { | ||
robot.stepForward(); | ||
} | ||
} | ||
} | ||
} |