Last active
August 29, 2015 14:25
-
-
Save suhajdab/86ccb5d8150ab8273d98 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
rev 3: 843