Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Robot {
- public Direction getDirection() {
- // текущее направление взгляда
- }
- public int getX() {
- // текущая координата X
- }
- public int getY() {
- // текущая координата Y
- }
- public void turnLeft() {
- // повернуться на 90 градусов против часовой стрелки
- }
- public void turnRight() {
- // повернуться на 90 градусов по часовой стрелке
- }
- public void stepForward() {
- // шаг в направлении взгляда
- // за один шаг робот изменяет одну свою координату на единицу
- }
- }
- public enum Direction {
- UP,
- DOWN,
- LEFT,
- RIGHT
- }
- public static void moveRobot(Robot robot, int toX, int toY) {
- while (toX != robot.getX()) {
- if (toX > robot.getX();) {
- turn(robot, Right);
- robot.stepForward();
- } else {
- turn(robot, Left);
- robot.stepForward();
- }
- while (toY != robot.getY()) {
- if (toY > robot.getY()) {
- turn(robot, UP);
- robot.stepForward();
- } else {
- turn(robot, DOWN);
- robot.stepForward();
- }
- }
- }
- public static void turn(Robot bot, Direction dir) {
- while (dir != bot.getDirection()) {
- bot.turnRight();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement