Skip to content

Instantly share code, notes, and snippets.

@uadev
Created January 14, 2014 00:49
Show Gist options
  • Save uadev/8411010 to your computer and use it in GitHub Desktop.
Save uadev/8411010 to your computer and use it in GitHub Desktop.
This will work... after several iterations.
/*
* Open all three doors to exit.
*
* The answer is ?.
*/
/*
* Do it.
*
*/
this.on('start', function() {
var directions = {
top: function() {
this.thrusters.bottom(true);
},
bottom: function() {
this.thrusters.top(true);
},
right: function() {
this.thrusters.left(true);
},
left: function() {
this.thrusters.right(true);
},
stop: function() { },
topRight: function() {
stopGo.call(this);
this.thrusters.left(true);
this.thrusters.bottom(true);
}
}
var conditions = {
gt: function(b, a) {
console.log(a + ' > ' + b)
return a > b;
},
lt: function(b, a) {
console.log(a + ' < ' + b)
return a < b;
},
miss: function() {console.log('hit'); return false;}
}
var directionToAngles = function(direction) {
var angles = {
right: 0,
bottom: 90,
left: 180,
top: 270
}
return angles[direction];
}
var go = function(direction) {
console.log(direction)
stopGo.call(this);
directions[direction].call(this);
mapNext.call(this);
}
var makeCheckFunction = function(check) {
var pair = check.split(' ');
console.log(pair)
return conditions[pair[0]].bind(void(0), pair[1]);
}
var navigate = function(mapItem) {
off.call(this);
var hasPreCondition = (mapItem.sensor || mapItem.aim);
if (!hasPreCondition) {
go.call(this, mapItem.go);
return;
}
if (mapItem.go) {
var goBound = go.bind(this, mapItem.go);
}
if (mapItem.sensor) {
this.on('sensor:' + mapItem.sensor, goBound);
}
if (mapItem.check) {
var check = makeCheckFunction(mapItem.check);
if (mapItem.check === 'miss') {
this.on('radar:miss', function(angle) {
console.log('missed ping')
goBound();
this.radar.ping();
})
}
}
if (mapItem.aim) {
var targetAngle = directionToAngles(mapItem.aim);
this.radar.angle(targetAngle);
this.on('radar:hit', function(angle, distance) {
if ( angle == targetAngle && check(distance) ) {
goBound();
return;
}
this.radar.ping();
});
this.radar.ping();
}
}
var stopGo = function() {
this.thrusters.left(false);
this.thrusters.right(false);
this.thrusters.bottom(false);
this.thrusters.top(false);
}
var off = function() {
this.off('sensor:right');
this.off('sensor:left');
this.off('sensor:top');
this.off('sensor:bottom');
this.off('radar:hit');
this.off('radar:miss');
this.radar.ping();
};
var parseInstruction = function(instruction) {
var actions = instruction.split(',');
var mapItem = {}
actions.forEach(function(action) {
var pair = action.split(':');
mapItem[pair[0]] = pair[1];
});
return mapItem;
}
var mapped = 0;
var mapNext = function() {
console.log('mapping instruction ' + ++mapped);
var instruction = this.map.shift();
if (!instruction) {
console.log('All instructions are mapped');
return;
}
var mapItem = parseInstruction(instruction);
navigate.call(this, mapItem);
};
var run = function() {
mapNext.call(this);
};
this.map = [
'go:top',
'sensor:top,go:right',
'sensor:top,go:top',
'aim:top,check:lt 170,go:right',
'aim:right,check:lt 220,go:bottom',
];
run.call(this);
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment