-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommandParser.js
More file actions
55 lines (45 loc) · 1.2 KB
/
commandParser.js
File metadata and controls
55 lines (45 loc) · 1.2 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
function ParseCommand(turtle, command = []) {
if (command[0].toLowerCase() == 'fd') {
turtle.moveForward(getNumberInCommand(command));
}
if (command[0].toLowerCase() == 'bk') {
turtle.moveBackward(getNumberInCommand(command));
}
if (command[0].toLowerCase() == 'rt') {
turtle.turnRight(getNumberInCommand(command));
}
if (command[0].toLowerCase() == 'lt') {
turtle.turnLeft(getNumberInCommand(command));
}
if (command[0].toLowerCase() == 'pu') {
drawPath = false;
}
if (command[0].toLowerCase() == 'pd') {
drawPath = true;
}
if (command[0].toLowerCase() == 'cs') {
drawPath = true;
paths = [{ x: canvasSize / 2, y: canvasSize / 2 }];
turtle.reset();
}
if (command[0].toLowerCase() == 'ct') {
turtle.reset();
paths.push({ x: turtle.x, y: turtle.y, drawPath: true });
}
if (command[0].toLowerCase() == 'st') {
showTurtle = true;
}
if (command[0].toLowerCase() == 'ht') {
showTurtle = false;
}
}
function getNumberInCommand(command = []) {
let returnValue = 0;
command.forEach(element => {
const intValue = parseInt(element);
if (!isNaN(intValue)) {
returnValue = intValue;
}
});
return returnValue;
}