-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrobot.js
More file actions
53 lines (45 loc) · 1.34 KB
/
Copy pathrobot.js
File metadata and controls
53 lines (45 loc) · 1.34 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
const helperFunctions = require('./helperFunctions.js');
const robot = {
placed: false,
position: {
x: null,
y: null,
direction: null,
},
setPosition(x, y, direction) {
robot.position.x = parseInt(x);
robot.position.y = parseInt(y);
robot.position.direction = direction;
robot.placed = true;
},
checkForErrors(results) {
if (results[0]) {
console.log(`Error: ${ results[1] }`);
return;
}
},
input(input) {
const inputArray = input.split( /[ ,]+/);
switch (inputArray[0]) {
case 'PLACE':
const x = inputArray[1];
const y = inputArray[2];
const direction = inputArray[3]
robot.checkForErrors(helperFunctions.checkValues(x, y, direction));
robot.setPosition(x,y,direction);
break;
case 'MOVE':
if (!robot.placed) return;
const results = helperFunctions.checkMoveStatus(robot.position);
robot.checkForErrors(results);
robot.position[results[1]] = results[2];
break;
case 'REPORT':
console.log(`The robot is in position x:${robot.position.x} y:${robot.position.y} and is facing ${robot.position.direction}`);
break;
default:
robot.position.direction = helperFunctions.turnRobot(robot.position.direction, inputArray[0]);
}
}
}
module.exports = robot;