-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass Robot.js
160 lines (148 loc) · 3.93 KB
/
class Robot.js
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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
class Robot {
constructor() {
this.coor = {
x: 0,
y: 0,
};
this.coordinateMap = {
N: 0,
S: 0,
E: 0,
W: 0,
};
this.crateList = [
{ id: 1, x: 1, y: 1 },
{ id: 2, x: 2, y: 2 },
];
}
getCoor() {
return this.coor;
}
setCoor(coor) {
this.coor = coor;
}
robotMovement(instruction) {
const command = instruction.split(",");
for (let i = 0; i < command.length; i++) {
let coor = this.getCoor();
if ("N" === command[i]) {
if (coor.y < 5) {
this.coordinateMap["N"] += 1;
} else {
throw "out of bound error X axis";
}
}
if ("S" === command[i]) {
if (coor.y > -5) {
this.coordinateMap["S"] += 1;
} else {
return "out of bound error";
}
}
if ("E" === command[i]) {
if (coor.x < 5) {
this.coordinateMap["E"] += 1;
} else {
return "out of bound error";
}
}
if ("W" === command[i]) {
if (coor.x > -5) {
this.coordinateMap["W"] += 1;
} else {
return "out of bound error";
}
}
this.currentRobotPosition();
}
}
simpleRobotMovement(instruction) {
console.log(instruction);
const command = instruction.split(" ");
for (let i = 0; i < command.length; i++) {
let coor = this.getCoor();
if ("N" === command[i]) {
if (coor.y < 5) {
this.coordinateMap["N"] += 1;
} else {
throw "out of bound error X axis";
}
}
if ("S" === command[i]) {
if (coor.y > -5) {
this.coordinateMap["S"] += 1;
} else {
return "out of bound error";
}
}
if ("E" === command[i]) {
if (coor.x < 5) {
this.coordinateMap["E"] += 1;
} else {
return "out of bound error";
}
}
if ("W" === command[i]) {
if (coor.x > -5) {
this.coordinateMap["W"] += 1;
} else {
return "out of bound error";
}
}
this.currentRobotPosition();
}
}
wareHouse(value) {
// 'N'
// it take value of the robot
const command = value.split(" ");
for (let i = 0; i < command.length; i++) {
if (command[i] === "G") {
/// ensure that we are not carry a crate
// need some varification logic, to check if you are at the current location of the crate
let coor = this.getCoor();
console.log("robot coor", coor);
// this.crateList = [{id:1, x:1, y:1}, {id:2, x:5, y:5} ]
const foundItem = this.crateList?.find(
(item) => item.x === coor.x && item.y === coor.y
);
console.log(foundItem);
} else if (command[i] === "D") {
let coor = this.getCoor();
console.log("new robot position after picking", coor);
this.crateList.push({ coor });
// you cant overide an existing value
// this.crateList = [{id:1, x:1, y:1}, {id:2, x:2, y:2} ]
} else {
this.simpleRobotMovement(command[i].toString());
}
}
// it navigates succefully to the position of the crate
// check if its already carrying a crate
// using the G command to pick the crate and and modify the current location of the crate
}
currentRobotPosition() {
const x = this.coordinateMap["E"] - this.coordinateMap["W"];
const y = this.coordinateMap["N"] - this.coordinateMap["S"];
const coor = {
x,
y,
};
const output = `(${coor.x}, ${coor.y})`;
this.setCoor(coor);
console.log("curr", output);
}
robotError(value) {
console.log(value);
}
}
const robotClass = new Robot();
// console.log("robot", robotClass.robotMovement(
// "N"
// ))
console.log(
"robot",
robotClass.wareHouse("N N N N N E E E E E G W W W S S S D")
);
robotClass.currentRobotPosition();
// [N, N, N, N, N, N, S, S, S, , G, W, W, W, W]