Advertisement
SebaochzhaiDmitry

Robot

Mar 28th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.95 KB | None | 0 0
  1. package robot;
  2.  
  3. enum Direction {
  4.     UP,
  5.     DOWN,
  6.     LEFT,
  7.     RIGHT
  8. }
  9.  
  10. public class Robot {
  11.  
  12.     Direction dir;
  13.     int x, y;
  14.     static int numberOfRobots = 0;
  15.  
  16.     public Robot (int x, int y, Direction dir) {
  17.         this.x = x;
  18.         this.y = y;
  19.         this.dir = dir;
  20.         numberOfRobots++;
  21.     }
  22.  
  23.     public Direction getDirection() {
  24.         return dir;
  25.     }
  26.  
  27.     public int getX() {return x;}
  28.     public int getY() {return y;}
  29.  
  30.     public void turnLeft() {
  31.  
  32.         switch (getDirection()) {
  33.             case UP:
  34.                 dir = Direction.LEFT;
  35.                 break;
  36.             case DOWN:
  37.                 dir = Direction.RIGHT;
  38.                 break;
  39.             case LEFT:
  40.                 dir = Direction.DOWN;;
  41.                 break;
  42.             case RIGHT:
  43.                 dir = Direction.UP;
  44.                 break;
  45.         }
  46.  
  47.     }
  48.  
  49.     public void turnRight() {
  50.  
  51.         switch (getDirection()) {
  52.             case UP:
  53.                 dir = Direction.RIGHT;
  54.                 break;
  55.             case DOWN:
  56.                 dir = Direction.LEFT;
  57.                 break;
  58.             case LEFT:
  59.                 dir = Direction.UP;
  60.                 break;
  61.             case RIGHT:
  62.                 dir = Direction.DOWN;
  63.                 break;
  64.         }
  65.  
  66.     }
  67.  
  68.     public void stepForward() {
  69.  
  70.         switch (getDirection()) {
  71.             case UP:
  72.                 y++;
  73.                 break;
  74.             case DOWN:
  75.                 y--;
  76.                 break;
  77.             case LEFT:
  78.                 x--;
  79.                 break;
  80.             case RIGHT:
  81.                 x++;
  82.                 break;
  83.         }
  84.  
  85.     }
  86.  
  87.     public void toSpeak() {
  88.         int x = getX();
  89.         int y = getY();
  90.  
  91.         System.out.println("Координаты: x= " + x + " y= " + y);
  92.     }
  93. }
  94.  
  95. class SmartRobot extends Robot {
  96.  
  97.     public SmartRobot(int x, int y, Direction dir) {
  98.         super(x,y,dir);
  99.     }
  100.  
  101.     void moveRobot(int toX, int toY) {
  102.  
  103.         while (x != toX || y != toY) {
  104.  
  105.             if(x < toX) {
  106.                 while (getDirection() != Direction.RIGHT)
  107.                 {
  108.                     turnRight();
  109.                 }
  110.             } else if(x > toX) {
  111.                 while (getDirection() != Direction.LEFT)
  112.                 {
  113.                     turnLeft();
  114.                 }
  115.             }
  116.  
  117.             if(y < toY) {
  118.  
  119.                 while (getDirection() != Direction.UP)
  120.                 {
  121.                     turnRight();
  122.                 }
  123.  
  124.             } else if(y > toY)  {
  125.                 while (getDirection() != Direction.DOWN)
  126.                 {
  127.                     turnLeft();
  128.                 }
  129.             }
  130.  
  131.             stepForward();
  132.             toSpeak();
  133.         }
  134.     }
  135.  
  136.     public static void main(String[] args)
  137.     {
  138.         SmartRobot r = new SmartRobot(0, 0, Direction.LEFT);
  139.         r.moveRobot(6, 8);
  140.     }
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement