-
Notifications
You must be signed in to change notification settings - Fork 0
/
immutablerover.js
66 lines (56 loc) · 1.54 KB
/
immutablerover.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
class Rover {
constructor(x, y, heading) {
this.state = {
pos: { x, y },
heading: heading
};
// Rover rotating
this.rotateMap = {
N: { L: "W", R: "E" },
E: { L: "N", R: "S" },
W: { L: "S", R: "N" },
S: { L: "E", R: "W" }
};
// move in direction of heading
this.moveMap = {
N: { plane: "y", amount: +1 },
E: { plane: "x", amount: +1 },
W: { plane: "x", amount: -1 },
S: { plane: "y", amount: -1 }
};
Object.freeze(this);
}
// Rotate given direction L || R
rotate(direction) {
// let current_heading = []
// let last_element = current_heading.concat(heading).concat(direction).slice(-1);
// return this.rotateMap[last_element][direction];
this.state.heading = this.rotateMap[this.state.heading][direction];
}
// Move one step in the direction
// pass isPositionAvailable function into predicate parameter
move(predicate) {
//console.log(predicate);
const moveBy = this.moveMap[this.state.heading];
// console.log(moveBy);
const nextPos = { x: this.state.pos.x, y: this.state.pos.y };
//console.log(nextPos);
// Set next position
nextPos[moveBy.plane] += moveBy.amount;
// Check if move is legal
const isLegal = predicate(nextPos);
if (isLegal) {
this.state.pos = nextPos;
}
}
// get current state
getState() {
return {
pos: this.state.pos,
heading: this.state.heading
};
}
}
// let test = new Rover(5,5,'M')
// console.log(test.getState());
module.exports = Rover;