-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2069-walking-robot-simulation-ii.java
More file actions
98 lines (92 loc) · 2.77 KB
/
2069-walking-robot-simulation-ii.java
File metadata and controls
98 lines (92 loc) · 2.77 KB
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
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
class Robot {
int width;
int height;
int[] pos;
String dir;
int x;
int y;
int perimiter;
public Robot(int width, int height) {
this.width = width;
this.height = height;
this.x = 0;
this.y = 0;
this.dir = "East";
this.perimiter = (width-1)*2 + (height-1)*2;
}
public void move(int num) {
int delta;
if (num == 0) {
return;
}
if (this.perimiter > 0)
num = num % this.perimiter;
while (num > 0) {
if (this.dir.equals("East")) {
if (x + 1 < width) {
delta = Math.min(num, width - x - 1);
x += delta;
num -= delta;
}
else {
this.dir = "North";
}
}
else if (this.dir.equals("North")) {
if (y + 1 < height) {
delta = Math.min(num, height - y - 1);
y += delta;
num -= delta;
}
else {
this.dir = "West";
}
}
else if (this.dir.equals("West")) {
if (x - 1 >= 0) {
delta = Math.min(num, x);
x -= delta;
num -= delta;
}
else {
this.dir = "South";
}
}
else if (this.dir.equals("South")) {
if (y - 1 >= 0) {
delta = Math.min(num, y);
y -= delta;
num -= delta;
}
else {
this.dir = "East";
}
}
}
if (x == 0 && y == 0) {
this.dir = "South";
}
else if (y == 0 && x == width - 1) {
this.dir = "East";
}
else if (x == width - 1 && y == height - 1) {
this.dir = "North";
}
else if (x == 0 && y == height - 1) {
this.dir = "West";
}
}
public int[] getPos() {
return new int[]{this.x, this.y};
}
public String getDir() {
return this.dir;
}
}
/**
* Your Robot object will be instantiated and called as such:
* Robot obj = new Robot(width, height);
* obj.move(num);
* int[] param_2 = obj.getPos();
* String param_3 = obj.getDir();
*/