-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
65 lines (59 loc) · 1.84 KB
/
Copy pathmain.js
File metadata and controls
65 lines (59 loc) · 1.84 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
/**
* This is the entry point. Input is from stdin
*/
const readline = require("readline");
const Robot = require("./compoments/robot"),
Map = require("./compoments/map"),
LeftCommand = require("./commands/left-command"),
RightCommand = require("./commands/right-command"),
PlaceCommand = require("./commands/place-command"),
MoveCommand = require("./commands/move-command"),
ReportCommand = require("./commands/report-command");
const robot = new Robot(null, null, null),
map = new Map(5, 5), // A 5x5 map as required
leftCommand = new LeftCommand(robot, map),
rightCommand = new RightCommand(robot, map),
placeCommand = new PlaceCommand(robot, map),
moveCommand = new MoveCommand(robot, map),
reportCommand = new ReportCommand(robot, map);
/**
* Can be refactored into a Class to implement undo feature
*/
executor = (command, param = null) => {
command.execute(param);
};
/**
* reads one line only
*/
parser = input => {
const placeRegex = /^PLACE\s+\d+,\d+,(NORTH|EAST|SOUTH|WEST)$/,
leftRegex = /^LEFT$/,
rightRegex = /^RIGHT$/,
moveRegex = /^MOVE$/,
reportRegex = /^REPORT$/;
if (placeRegex.test(input)) {
const [, x, y, facing] = input.replace(" ", ",").split(","); //to sanitise input
executor(placeCommand, { x, y, facing });
} else if (leftRegex.test(input)) {
executor(leftCommand);
} else if (rightRegex.test(input)) {
executor(rightCommand);
} else if (moveRegex.test(input)) {
executor(moveCommand);
} else if (reportRegex.test(input)) {
executor(reportCommand);
}
// otherwise input is not a valid command
};
/**
* App begins
*/
console.log(
"Welcome to Toy Robot Simulator, please type in your command and Enter"
);
const rl = readline.createInterface(process.stdin, process.stdout);
rl.setPrompt("command> ");
rl.prompt();
rl.on("line", input => {
parser(input);
});