Skip to content

Instantly share code, notes, and snippets.

@real34
Created February 16, 2016 13:19
Show Gist options
  • Save real34/1db38dd26f0e357343f2 to your computer and use it in GitHub Desktop.
Save real34/1db38dd26f0e357343f2 to your computer and use it in GitHub Desktop.
{
"name": "rx-rover",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "babel-node test.js | faucet",
"test_debug": "babel-node test.js"
},
"author": "",
"license": "ISC",
"babel": {
"presets": [
"es2015"
]
},
"dependencies": {
"babel-cli": "^6.4.5",
"rx": "^4.0.7",
"tape": "^4.4.0"
},
"devDependencies": {
"babel-preset-es2015": "^6.3.13",
"faucet": "0.0.1"
}
}
import tape from 'tape'
import {Observable} from 'rx'
tape('position initiale', t => {
var x= 0, y = 0, o = 'N';
var marsRover = {x, y , o};
const SUT = Observable.of(marsRover)
SUT
.tap(valeur => t.deepEqual(valeur, marsRover))
.subscribe(() => t.end())
})
tape('nouvelle position', t => {
var marsRover= new MarsRover(0,0,'N');
const SUT = Observable.of(marsRover)
const commands = Observable.from(['f'])
commands
.map(valeur => {
marsRover.move(valeur);
return marsRover;
})
.tap( valeur =>
t.deepEqual(valeur, new MarsRover(0,1,'N'))
)
.subscribe(() => t.end())
})
tape('nouvelle position', t => {
var marsRover= new MarsRover(0,0,'N');
const SUT = Observable.of(marsRover)
const commands = Observable.from(['b'])
commands
.map(valeur => {
marsRover.move(valeur);
return marsRover;
})
.tap( valeur =>
t.deepEqual(valeur, new MarsRover(0,-1,'N'))
)
.subscribe(() => t.end())
})
function MarsRover(x,y,o){
this.x=x;
this.y=y;
this.o=o;
}
MarsRover.prototype.move =function(direction){
var directions = {'N':{x:0,y:1},'S':{x:0,y:-1},'E':{x:1,y:0},'W':{x:-1,y:0}};
if(direction=='f'){
this.x+=directions[this.o].x;
this.y+=directions[this.o].y;
}else{
this.x-=directions[this.o].x;
this.y-=directions[this.o].y;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment