Skip to content

Instantly share code, notes, and snippets.

@dchaplinsky
Created January 5, 2014 16:04
Show Gist options
  • Select an option

  • Save dchaplinsky/8269996 to your computer and use it in GitHub Desktop.

Select an option

Save dchaplinsky/8269996 to your computer and use it in GitHub Desktop.
bit's quest #6 especially for IlyaF
if (!String.prototype.endsWith) {
Object.defineProperty(String.prototype, 'endsWith', {
enumerable: false,
configurable: false,
writable: false,
value: function (searchString, position) {
position = position || this.length;
position = position - searchString.length;
var lastIndex = this.lastIndexOf(searchString);
return lastIndex !== -1 && lastIndex === position;
}
});
}
var stop = function(self) {
self.thrusters.top(false);
self.thrusters.left(false);
self.thrusters.bottom(false);
self.thrusters.right(false);
}
var strategy = [
// start
{
"bottom": {
true: "left"
},
"right": {
true: "bottom"
},
"left": {
true: "top",
false: "right."
}
},
{
"left": {
true: "bottom."
},
},
{
"top": {
true: "left"
},
"right": {
true: "top."
}
},
{
"left": {
true: "left"
},
"right": {
true: "bottom."
}
},
{
"right": {
true: "bottom",
false: "left"
}
}
]
var tactic = 0;
this.on("all", function(){
var args = [].slice.call(arguments, 0),
match = args[0].match(/sensor\:(.+)$/);
if (match && match[1]) {
if (match[1] in strategy[tactic] && args[1] in strategy[tactic][match[1]]) {
stop(this);
var new_action = strategy[tactic][match[1]][args[1]]
if (new_action.endsWith(".")) {
tactic += 1;
console.log("switching strategy");
new_action = new_action.replace(".", "");
}
if (new_action in this.thrusters) {
this.thrusters[new_action](true);
}
}
}
});
this.on("start", function() {
this.thrusters.top(true);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment