-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmowers_test.js
99 lines (85 loc) · 2.39 KB
/
mowers_test.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
const mowerMove = (x, y, init, sequence) => {
const lawn = { x, y };
let indexSeq = 0;
const orientations = ["N", "E", "S", "W"];
const positionInit = {
x: init.charAt(0),
y: init.charAt(2),
orientation: init.charAt(4),
};
let indexOrient = orientations.findIndex(
(orient) => orient === positionInit.orientation
);
let mowerPosition = {
x: positionInit.x,
y: positionInit.y,
direction: positionInit.orientation,
};
document.write(
`Mower's starting position : ${mowerPosition.x}, ${mowerPosition.y}, ${mowerPosition.direction} </br>`
);
console.log("START");
for (i = 0; i < sequence.length; i++) {
switch (sequence.charAt(indexSeq)) {
case "L":
if (mowerPosition.direction == "N") {
indexOrient = orientations.length - 1;
mowerPosition.direction = orientations[indexOrient];
} else {
indexOrient = indexOrient - 1;
mowerPosition.direction = orientations[indexOrient];
}
indexSeq++;
console.log("LEFT");
console.log(`Mower's orientation : ${mowerPosition.direction}`);
break;
case "R":
if (mowerPosition.direction == "W") {
indexOrient = 0;
mowerPosition.direction = orientations[indexOrient];
} else {
indexOrient = indexOrient + 1;
mowerPosition.direction = orientations[indexOrient];
}
indexSeq++;
console.log("RIGHT");
console.log(`Mower's orientation : ${mowerPosition.direction}`);
break;
case "F":
switch (mowerPosition.direction) {
case "N":
if (mowerPosition.y == lawn.y) {
console.log("Out of zone");
break;
} else mowerPosition.y++;
break;
case "E":
if (mowerPosition.x == lawn.x) {
console.log("Out of zone");
break;
} else mowerPosition.x++;
break;
case "S":
if (mowerPosition.y == 0) {
console.log("Out of zone");
break;
} else mowerPosition.y--;
break;
case "W":
if (mowerPosition.x == 0) {
console.log("Out of zone");
break;
} else mowerPosition.x--;
break;
}
indexSeq++;
console.log("FORWARD");
break;
}
}
document.write(
`Mower's final position : ${mowerPosition.x}, ${mowerPosition.y}, ${mowerPosition.direction} </br>`
);
};
mowerMove(5, 5, "1 2 N", "LFLFLFLFF");
mowerMove(5, 5, "3 3 E", "FFRFFRFRRF");