blob: 62a972d72adebdeba3bdd8594f5831e2a9c371ec (
plain)
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
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
|
package Q9;
public class Robot {
public static final int UP = 1;
public static final int DOWN = 2;
public static final int LEFT = 3;
public static final int RIGHT = 4;
public static final int LEFT_UP_CORNER = 5;
public static final int LEFT_DOWN_CORNER = 6;
public static final int RIGHT_UP_CORNER = 7;
public static final int RIGHT_DOWN_CORNER = 8;
public static final int NORTH = UP;
public static final int NORTH_EAST = RIGHT_UP_CORNER;
public static final int EAST = RIGHT;
public static final int SOUTH_EAST = RIGHT_DOWN_CORNER;
public static final int SOUTH = DOWN;
public static final int SOUTH_WEST = LEFT_DOWN_CORNER;
public static final int WEST = LEFT;
public static final int NORTH_WEST = LEFT_UP_CORNER;
public static final String R1 = "1";
public static final String R2 = "2";
public static final String COLLISION = "X";
public static final String SPACE = " ";
public static final String SEPARATOR = "|";
private int x;
private int y;
public Robot(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public void setX(int x) {
this.x = x;
}
public void setY(int y) {
this.y = y;
}
public boolean atPosition(int x, int y) {
return getX() == x && getY() == y;
}
public static String printGrid(Robot r1, Robot r2) {
String grid = "";
for (int row = 0; row < 10; row++) {
for (int column = 0; column < 10; column++) {
boolean r1InCell = r1.atPosition(row, column);
boolean r2InCell = r2.atPosition(row, column);
grid += SEPARATOR;
if (r1InCell && r2InCell) grid += COLLISION;
else if (r1InCell) grid += R1;
else if (r2InCell) grid += R2;
else grid += SPACE;
}
grid += String.format("%s%s", SEPARATOR, System.lineSeparator());
}
return grid;
}
}
|