Skip to content

Instantly share code, notes, and snippets.

@suhajdab
Last active August 29, 2015 14:25
Show Gist options
  • Save suhajdab/86ccb5d8150ab8273d98 to your computer and use it in GitHub Desktop.
Save suhajdab/86ccb5d8150ab8273d98 to your computer and use it in GitHub Desktop.
var api = require('./API.js');
var route = ['f','r','r','f'],
move = '',
env = [],
lastEnv = [];
function next() {
move = route.shift();
}
function followWall(decision) {
if (lastEnv[1] == 1 && env[1] > 1) decision = 'r';
else if (lastEnv[3] == 1 && env[3] > 1) decision = 'l';
console.log('followWall: ' + decision);
return decision;
}
function avoidObstacle(decision) {
if (env[0] == 1) {
// block ahead
if (env[1] > 1) decision = 'r';
else if (env[3] > 1) decision = 'l';
}
console.log('avoid: ' + decision);
return decision;
}
next();
exports.update = function() {
if (api.identifyTarget()) {
api.fireCannon();
} else {
env = [api.lidarFront(),api.lidarRight(),api.lidarBack(),api.lidarLeft()];
var decision = move;
decision = avoidObstacle(decision);
if (move == 'f') decision = followWall(decision);
switch (decision) {
case 'r':
api.turnRight();
next();
break;
case 'l':
api.turnLeft();
next();
break;
default:
api.moveForward();
}
lastEnv = env.slice();
}
};
@suhajdab
Copy link
Author

Revision 2 score: 831

@suhajdab
Copy link
Author

rev 3: 843

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment