Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Boilerplate Code. Here we assign a numerical index to each direction for
- * sensors and thrusters. We also have all sensor events call the "sense"
- * method so we can handle them all generically.
- */
- var $this = this;
- var directions = ["right", "bottom", "left", "top"];
- var sensors = [];
- var thrusters = [];
- for (var _i = 0; _i < 4; ++_i)
- (function() {
- var i = _i;
- this.on('sensor:' + directions[_i], function(c)
- {
- sensors[i] = c;
- sense.call(this, i);
- });
- sensors[i] = false;
- thrusters[i] = this.thrusters[directions[i]];
- }).call(this);
- /*
- * First we move to a wall: Our algorithm only knows how to follow walls to
- * the solution, and it expects to start against a wall, with no thrusters
- * running.
- */
- this.on('start', function() {
- this.thrusters.right(true);
- var initialized = false;
- this.on('sensor:left', function() {
- if (!initialized)
- {
- initialized = true;
- this.thrusters.right(false);
- }
- });
- });
- /*
- * We either found a new wall, or a wall is no longer next to us. For each
- * wall we're next to, turn on the thruster clockwise to that wall. If we are
- * no longer next to a wall, go towards the last disappeared wall.
- */
- function sense(i) {
- var anySensors = false;
- for (j in sensors)
- anySensors = anySensors || sensors[j];
- // A new direction to unexplored area just opened up, go to it
- if (! anySensors)
- thrusters[(i+2)%4](true);
- // Turn on a thruster clockwise from the wall we just encountered
- // Or, turn it off if there is no wall anymore.
- thrusters[(i+3)%4](sensors[i]);
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement