forked from pokle/coding-exercise-javascript-robot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimulation.js
More file actions
42 lines (32 loc) · 986 Bytes
/
Copy pathsimulation.js
File metadata and controls
42 lines (32 loc) · 986 Bytes
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
var Heading = {
NORTH: { name: 'North', dx: 0, dy: -1, right: 'EAST', left: 'WEST'},
EAST: { name: 'East', dx: 1, dy: 0, right: 'SOUTH', left: 'NORTH'},
SOUTH: { name: 'South', dx: 0, dy: 1, right: 'WEST', left: 'EAST'},
WEST: { name: 'West', dx: -1, dy: 0, right: 'NORTH', left: 'SOUTH'}
}
function Robot() {
var self = this
self.x = 0
self.y = 0
self.heading = Heading.NORTH
self.place = function(x,y,heading) {
self.x = x
self.y = y
self.heading = heading
}
self.right = function() {
self.heading = Heading[self.heading.right]
}
self.left = function() {
self.heading = Heading[self.heading.left]
}
self.move = function () {
self.x = self.x + self.heading.dx
self.y = self.y + self.heading.dy
}
self.report = function() {
return [self.x, self.y, self.heading.name].join(',')
}
}
exports.Robot = Robot
exports.Heading = Heading