Robot control
# Topic
Fields and methods in enum
# Problem
Robot control
注意
Wow! This problem is kind of tricky. If you're ready to put your thinking cap on, brace yourself and good luck! Otherwise, you can skip it for now and return any time later
There is a robot in the game field. The position of the robot in this field is described by two integer coordinates: X and Y. The X axis is oriented from left to right, the Y axis — from bottom to top.
At the initial moment, the robot is located at some coordinate on the field. It's also known where the robot looks: up, down, to the right or to the left. The initial position of the robot and its direction can have any values. You need to bring the robot to the destination point of the game field.
A robot is described by the Robot
class. You can use the following methods of this class (with unknown implementation):
public class Robot {
public Direction getDirection() {
// current direction
}
public int getX() {
// current X coordinate
}
public int getY() {
// current Y coordinate
}
public void turnLeft() {
// rotate the robot 90 degrees counterclockwise
}
public void turnRight() {
// rotate the robot 90 degrees clockwise
}
public void stepForward() {
// take one step in the current direction
// x or y coordinate will be changed by 1
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
The direction of the robot is an enumeration:
public enum Direction {
UP,
DOWN,
LEFT,
RIGHT
}
2
3
4
5
6
It looks like the picture below:
Example
- The following values are passed to the method: toX == 3, toY == 0.
- The initial state of this robot: robot.getX() == 0, robot.getY() == 0, robot.getDirection() == Direction.UP.
To bring the robot to the destination point (3, 0), the method should call the following methods:
robot.turnRight();
robot.stepForward();
robot.stepForward();
robot.stepForward();
2
3
4
Another Example
- The following target values are passed to the method: toX == 0, toY == -1.
- The initial state of this robot: robot.getX() == 1, robot.getY() == 1, robot.getDirection() == Direction.RIGHT.
To bring the robot to the destination point (0, -1), the method should call the following methods:
robot.turnRight();
robot.turnRight();
robot.stepForward();
robot.turnLeft();
robot.stepForward();
robot.stepForward();
2
3
4
5
6
Try to crack this problem!
# Hint & Explain
// 输入提示
# Solution
# Other person solution
class Move {
public static void moveRobot(Robot robot, int toX, int toY) {
// get x positive or negative(right or left)
// make robot go left or right
if (toX > robot.getX()) {
// set to facing right
while (robot.getDirection() != Direction.RIGHT) {
System.out.println(robot.getDirection());
robot.turnRight();
}
} else {
// set to facing left
while (robot.getDirection() != Direction.LEFT) {
robot.turnRight();
}
}
// iterate over difference between toX and current X
while (robot.getX() != toX) {
robot.stepForward();
}
// get y positive or negative(Up or down)
// make robot go up or down
if (toY > robot.getY()) {
// set to facing up
while (robot.getDirection() != Direction.UP) {
robot.turnRight();
}
} else {
// set to facing down
while (robot.getDirection() != Direction.DOWN) {
robot.turnRight();
}
}
// iterate over difference between toY and current Y
while (robot.getY() != toY) {
robot.stepForward();
}
}
}
//Don't change code below
enum Direction {
UP(0, 1),
DOWN(0, -1),
LEFT(-1, 0),
RIGHT(1, 0);
private final int dx;
private final int dy;
Direction(int dx, int dy) {
this.dx = dx;
this.dy = dy;
}
public Direction turnLeft() {
switch (this) {
case UP:
return LEFT;
case DOWN:
return RIGHT;
case LEFT:
return DOWN;
case RIGHT:
return UP;
default:
throw new IllegalStateException();
}
}
public Direction turnRight() {
switch (this) {
case UP:
return RIGHT;
case DOWN:
return LEFT;
case LEFT:
return UP;
case RIGHT:
return DOWN;
default:
throw new IllegalStateException();
}
}
public int dx() {
return dx;
}
public int dy() {
return dy;
}
}
class Robot {
private int x;
private int y;
private Direction direction;
public Robot(int x, int y, Direction direction) {
this.x = x;
this.y = y;
this.direction = direction;
}
public void turnLeft() {
direction = direction.turnLeft();
}
public void turnRight() {
direction = direction.turnRight();
}
public void stepForward() {
x += direction.dx();
y += direction.dy();
}
public Direction getDirection() {
return direction;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136