-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmars-rover.js
216 lines (189 loc) · 4.77 KB
/
mars-rover.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
const getUserInput = (question) => {
return new Promise((resolve, reject) => {
rl.question(question, (answer) => {
resolve(answer);
});
});
};
const main = async () => {
const platauCoordinates = await getUserInput(
"Specify the size of the Mars plateau (e.g. 5 5):"
);
if (!arePlatauCoordinatesValid(platauCoordinates)) {
return rl.close();
}
let responsePlateau = platauCoordinates.split(" ");
const plateau = {
x: parseInt(responsePlateau[0]),
y: parseInt(responsePlateau[1]),
};
const roverPositionAndDirection = await getUserInput(
"Specify the initial coordinates and orientation of the mars rover (e.g. 1 2 N):"
);
if (!isRoverPositionAndDirectionValid(roverPositionAndDirection)) {
return rl.close();
}
const roverPosition = roverPositionAndDirection.split(" ");
if (!isRoverPositionOnMars(roverPosition, plateau)) {
return rl.close();
}
const roverOrientation = roverPosition[2].toUpperCase();
if (!isRoverOrientationValid(roverOrientation)) {
return rl.close();
}
const rover = {
x: parseInt(roverPosition[0]),
y: parseInt(roverPosition[1]),
orientation: roverPosition[2],
};
console.log("rover: ", rover);
const directions = await getUserInput(
"Specify the instructions for the mars rover (e.g. LMLMLMLMM):"
);
if (!areDirectionCorrect(directions)) {
return rl.close();
}
moveRover(rover, directions, plateau);
rl.close();
};
function turnRoverLeft(rover) {
switch (rover.orientation) {
case "N":
rover.orientation = "W";
break;
case "W":
rover.orientation = "S";
break;
case "S":
rover.orientation = "E";
break;
case "E":
rover.orientation = "N";
break;
}
// console.log(`The rover turned left and is now facing ${rover.orientation}.`);
}
function turnRoverRight(rover) {
switch (rover.orientation) {
case "N":
rover.orientation = "E";
break;
case "E":
rover.orientation = "S";
break;
case "S":
rover.orientation = "W";
break;
case "W":
rover.orientation = "N";
break;
}
// console.log(`The rover turned right and is now facing ${rover.orientation}.`);
}
function moveRoverForward(rover) {
switch (rover.orientation) {
// Assume that the square directly North from (x, y) is (x, y+1).
case "N":
rover.y++;
break;
case "E":
rover.x++;
break;
case "S":
rover.y--;
break;
case "W":
rover.x--;
break;
}
/*
console.log(
`The rover moved forward and is now in:\n x: ${rover.x} \n y: ${rover.y}`
);
let newPosition = { x: rover.x, y: rover.y };
console.log(newPosition);
*/
}
function moveRover(rover, directions, plateau) {
/* Uses the supplied operations and moves the rover according to the string of operations. */
for (const orientation of directions) {
switch (orientation) {
case "L":
turnRoverLeft(rover);
break;
case "R":
turnRoverRight(rover);
break;
case "M":
moveRoverForward(rover);
break;
}
}
if (
rover.x < 0 ||
rover.y < 0 ||
rover.x > plateau.x ||
rover.y > plateau.y
) {
console.log("The rover left Mars. Congratulations!");
} else {
// console.log(`The rover ended in: x: ${rover.x} y: ${rover.y}`);
console.log(`${rover.x} ${rover.y} ${rover.orientation}`);
}
}
function arePlatauCoordinatesValid(input) {
let regexX = /^\d+ \d+$/;
if (!regexX.test(input)) {
console.log("Wrong plateau coordindates entered");
return false;
}
return true;
}
function isRoverPositionAndDirectionValid(input) {
let regexY = /^\d+ \d+ \w$/;
if (!regexY.test(input)) {
console.log("The position given for the Rover is incorrect");
return false;
}
return true;
}
function isRoverPositionOnMars(input, plateau) {
if (
parseInt(input[0]) < 0 ||
parseInt(input[0]) > plateau.x ||
parseInt(input[1]) < 0 ||
parseInt(input[1]) > plateau.y
) {
console.log("You positioned the Mars outside of the Plateau");
return false;
}
return true;
}
function isRoverOrientationValid(roverOrientation) {
if (
roverOrientation !== "N" &&
roverOrientation !== "S" &&
roverOrientation !== "W" &&
roverOrientation !== "E"
) {
console.log(
`You've entered ${roverOrientation} which is not a correct coordinate`
);
return false;
}
return true;
}
function areDirectionCorrect(directions) {
let regexZ = /^[LMR]*$/;
if (!regexZ.test(directions)) {
console.log("Wrong directions. Only values 'L', 'M' or 'R' accepted!");
return false;
}
return true;
}
main();