-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
88 lines (85 loc) · 2.35 KB
/
index.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
const inquirer = require("inquirer");
const RoverManager = require("./rovermanager");
var questions = [
{
type: "input",
name: "grid_x",
message: "Enter x coordinate for grid:",
validate: function(value) {
var valid = !isNaN(parseFloat(value));
return valid || "Please enter a number";
},
filter: Number
},
{
type: "input",
name: "grid_y",
message: "Enter y coordinate for grid:",
validate: function(value) {
var valid = !isNaN(parseFloat(value));
return valid || "Please enter a number";
},
filter: Number
},
{
type: "input",
name: "rover_x",
message: "Add rover x coordinate:",
validate: function(value) {
var valid = !isNaN(parseFloat(value));
return valid || "Please enter a number";
},
filter: Number
},
{
type: "input",
name: "rover_y",
message: "Add rover y coordinate:",
validate: function(value) {
var valid = !isNaN(parseFloat(value));
return valid || "Please enter a number";
},
filter: Number
},
{
type: "list",
name: "heading",
message: "Add rover heading:",
choices: ["North", "South", "East", "West"],
filter: function(val) {
return val.slice(0, 1);
}
},
{
type: "input",
name: "rover_commands",
message: "Enter rover commands eg.LMLMLM:",
validate: function(value) {
let valid = !!value.toUpperCase().match(/^[LRM]+$/g);
return valid || "You can only enter L or R or M";
}
},
{
type: "list",
name: "option",
message: "Add another rover?",
default: "No",
choices: ["Yes", "No"]
}
];
inquirer.prompt(questions).then(answers => {
//console.log(JSON.stringify(answers, null, " "));
const { grid_x, grid_y, rover_x, rover_y, heading, rover_commands } = answers;
const rover1 = new RoverManager(grid_x, grid_y);
rover1.addRover(rover_x, rover_y, heading);
rover1.sendCommand(rover_commands);
if (answers.option === "Yes") {
return inquirer.prompt(questions.slice(2, 6)).then(answers => {
const { rover_x, rover_y, heading, rover_commands } = answers;
rover1.addRover(rover_x, rover_y, heading);
rover1.sendCommand(rover_commands);
console.log("Rover final positions:", rover1.outputFinalPositions());
});
}
return console.log("Rover final positions:", rover1.outputFinalPositions());
});