Skip to content

Instantly share code, notes, and snippets.

@dchaplinsky
Last active January 2, 2016 07:59
Show Gist options
  • Select an option

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

Select an option

Save dchaplinsky/8273513 to your computer and use it in GitHub Desktop.
/*
* Open all three doors to exit.
*
* The answer is ?.
*/
var mode = "centering";
var position = 0;
var sequences = [
[1, 0, 0], // On
[1, 0, 0], // Off
[0, 1, 0], // On
[0, 1, 0], // Off
[0, 0, 1], // On
[0, 0, 1], // Off
[1, 1, 1], // On
[1, 1, 1], // Off
[1, 1, 0], // On
[1, 1, 0], // Off
[1, 0, 1], // On
[1, 0, 1], // Off
[0, 1, 1], // On
[0, 1, 1], // Off
]
var sequence_no = 0;
var sequence = sequences[sequence_no];
var stop = function(self) {
self.thrusters.top(false);
self.thrusters.left(false);
self.thrusters.bottom(false);
self.thrusters.right(false);
}
this.on("start", function() {
this.thrusters.bottom(true);
});
this.on("sensor:top", function(){
if (mode == "centering") {
mode = "scan";
stop(this);
this.radar.angle(0);
this.radar.ping();
this.thrusters.left(true);
}
});
this.on("radar:hit", function(angle, distance){
if (mode == "scan" && angle == 0) {
if ((position == 0 && Math.abs(distance - 375) < 10) ||
(position == 1 && Math.abs(distance - 280) < 10) ||
(position == 2 && Math.abs(distance - 170) < 10)) {
position += 1;
if (sequence[position - 1]) {
stop(this);
this.thrusters.bottom(true);
this.radar.angle(270);
mode = "switch";
}
} else if (Math.abs(distance - 150) < 10) {
stop(this);
this.thrusters.right(true);
this.radar.angle(0);
mode = "comeback";
}
this.radar.ping();
}
if (mode == "switch" || mode == "switch_back") {
if ((Math.abs(distance - 115) < 5)) {
stop(this);
mode = "switch_back";
this.thrusters.top(true);
}
if (mode == "switch_back" && Math.abs(distance - 170) < 5) {
stop(this);
if (position == sequence.length) {
this.thrusters.right(true);
this.radar.angle(0);
mode = "comeback";
} else {
this.thrusters.left(true);
this.radar.angle(0);
mode = "scan";
}
}
this.radar.ping();
}
if (mode == "comeback") {
if ((Math.abs(distance - 250) < 20)) {
stop(this);
this.radar.angle(90);
mode = "test_exit";
}
if ((Math.abs(distance - 400) < 5)) {
mode = "scan";
sequence_no += 1;
position = 0;
sequence = sequences[sequence_no];
console.log(sequence);
stop(this);
this.radar.angle(0);
this.radar.ping();
this.thrusters.left(true);
}
this.radar.ping();
}
if (mode == "test_exit") {
this.thrusters.right(true);
this.radar.angle(0);
mode = "comeback";
}
});
this.on("radar:miss", function(){
stop(this);
this.thrusters.top(true);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment