|
1
|
|
|
"use strict"; |
|
2
|
|
|
|
|
3
|
|
|
var _ = require('lodash'), |
|
4
|
|
|
Promise = require("bluebird"); |
|
5
|
|
|
|
|
6
|
|
|
// Expose `Move` |
|
7
|
|
|
|
|
8
|
|
|
module.exports = Move; |
|
9
|
|
|
|
|
10
|
|
|
var cardinalPoints = ['N','E','S','W']; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Set up Move with `mower` and `grid` |
|
14
|
|
|
* which is responsible for moving mowers |
|
15
|
|
|
* |
|
16
|
|
|
* @param {Object} mower |
|
17
|
|
|
* @param {String} grid |
|
18
|
|
|
* @api public |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
function Move(mower, grid){ |
|
22
|
|
|
this.mower = mower; |
|
23
|
|
|
this.grid = grid; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Call the movement method from an instruction |
|
28
|
|
|
* |
|
29
|
|
|
* @param {String} instruction |
|
30
|
|
|
* @api public |
|
31
|
|
|
*/ |
|
32
|
|
|
|
|
33
|
|
|
Move.prototype.runInstruction = function(instruction) { |
|
34
|
|
|
var self = this; |
|
35
|
|
|
return new Promise(function(resolve, reject) { |
|
36
|
|
|
switch(instruction) { |
|
37
|
|
|
case 'A': |
|
38
|
|
|
self.goForward(); |
|
39
|
|
|
break; |
|
40
|
|
|
case 'G': |
|
41
|
|
|
case 'D': |
|
42
|
|
|
self.turn(instruction); |
|
43
|
|
|
break; |
|
44
|
|
|
default: |
|
45
|
|
|
reject( new Error('Invalid instruction: '+instruction) ); |
|
46
|
|
|
} |
|
47
|
|
|
resolve(self.mower.position); |
|
48
|
|
|
}); |
|
49
|
|
|
}; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Update mower's position if not occupied |
|
53
|
|
|
* |
|
54
|
|
|
* @param {Object} newPosition |
|
55
|
|
|
* @api public |
|
56
|
|
|
*/ |
|
57
|
|
|
|
|
58
|
|
|
Move.prototype.updatePosition = function(newPosition) { |
|
59
|
|
|
this.mower.position = newPosition; |
|
60
|
|
|
this.grid.setItemPosition(this.mower.id, newPosition); // save new position in grid object |
|
61
|
|
|
}; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Return false if the given position is occupied or not allowed |
|
65
|
|
|
* |
|
66
|
|
|
* @param {Object} position |
|
67
|
|
|
* @return {Boolean} |
|
68
|
|
|
* @api public |
|
69
|
|
|
*/ |
|
70
|
|
|
|
|
71
|
|
|
Move.prototype.isNextPositionAllowed = function(position) { |
|
72
|
|
|
var nextCellOccupation = this.grid.getCellOccupation(position); |
|
73
|
|
|
return (nextCellOccupation === null || nextCellOccupation === this.mower.id) && |
|
74
|
|
|
(position.x >= 0 && position.x <= this.grid.width && position.y >= 0 && position.y <= this.grid.heigth); |
|
75
|
|
|
}; |
|
76
|
|
|
|
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Move a mower forward. |
|
80
|
|
|
* The direction is the actual cardinal. |
|
81
|
|
|
* |
|
82
|
|
|
* @api public |
|
83
|
|
|
*/ |
|
84
|
|
|
|
|
85
|
|
|
Move.prototype.goForward = function() { |
|
86
|
|
|
var actualPosition = _.clone(this.mower.position), |
|
87
|
|
|
nextPosition = getNextPositionForward(actualPosition); |
|
88
|
|
|
if(this.isNextPositionAllowed(nextPosition)) { |
|
89
|
|
|
this.updatePosition(nextPosition); |
|
90
|
|
|
} |
|
91
|
|
|
}; |
|
92
|
|
|
|
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Move a mower to the left or the right. |
|
96
|
|
|
* |
|
97
|
|
|
* @param {String} instruction |
|
98
|
|
|
* @api public |
|
99
|
|
|
*/ |
|
100
|
|
|
Move.prototype.turn = function(instruction) { |
|
101
|
|
|
var mower = this.mower, |
|
102
|
|
|
newPosition = _.clone(mower.position), |
|
103
|
|
|
cardinalIndex = _.findIndex(cardinalPoints, function(c) { return c === mower.position.c; }), |
|
104
|
|
|
newCardinalIndex = cardinalIndex; |
|
105
|
|
|
switch(instruction) { |
|
106
|
|
|
case 'D': |
|
107
|
|
|
newCardinalIndex = cardinalIndex+1 > cardinalPoints.length-1 ? 0 : cardinalIndex+1; |
|
108
|
|
|
break; |
|
109
|
|
|
case 'G': |
|
110
|
|
|
newCardinalIndex = cardinalIndex-1 < 0 ? cardinalPoints.length-1 : cardinalIndex-1; |
|
111
|
|
|
break; |
|
112
|
|
|
default: |
|
113
|
|
|
throw new Error('Invalid instruction: '+mower.position.c); |
|
114
|
|
|
} |
|
115
|
|
|
newPosition.c = cardinalPoints[newCardinalIndex]; |
|
116
|
|
|
this.updatePosition(newPosition); |
|
117
|
|
|
}; |
|
118
|
|
|
|
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Move a mower forward. |
|
122
|
|
|
* The direction is the actual cardinal. |
|
123
|
|
|
* |
|
124
|
|
|
* @api protected |
|
125
|
|
|
*/ |
|
126
|
|
|
|
|
127
|
|
|
function getNextPositionForward(position) { |
|
128
|
|
|
var valueToAdd = position.c==='N'||position.c==='E' ? 1 : -1; |
|
129
|
|
|
var propToChange = position.c==='W'||position.c==='E'?'x':'y'; |
|
130
|
|
|
position[propToChange] = position[propToChange]+valueToAdd; |
|
131
|
|
|
return position; |
|
132
|
|
|
} |