Skip to content

Instantly share code, notes, and snippets.

@phoenixbox
Created July 12, 2013 19:03
Show Gist options
  • Save phoenixbox/5986890 to your computer and use it in GitHub Desktop.
Save phoenixbox/5986890 to your computer and use it in GitHub Desktop.
JS solution to part of robot
_ = require('./underscore');
Robot = function () {
var directions = {
north: [0, 1],
east: [1, 0],
south: [0, -1],
west: [-1, 0]
}
function directionNames() {
return Object.keys(directions)
}
this.bearing = function() {
return directionNames()[this._bearing]
};
this.orient = function (direction) {
if (_.contains(directionNames(), direction)){
this._bearing = directionNames().indexOf(direction)
} else {
throw "Invalid Robot Bearing";
};
};
this.turnRight = function() {
this.turn(1)
};
this.turnLeft = function() {
this.turn(-1)
};
this.at = function(x, y) {
this._at = [x,y]
}
this.coordinates = function() {
return this._at
}
this.advance = function() {
var delta = directions[this.bearing()]
this._at = _.zip(this._at, delta).map(function(diff) {
return diff.reduce(function(a, b) { return a + b })
})
}
// (map sumDiff) . zip $ _at delta
this.turn = function(direction) {
var len = directionNames().length
this._bearing = (this._bearing + direction + len) % len
}
};
Simulator = function () {
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment