Created
July 21, 2015 13:30
-
-
Save suhajdab/1682e6e1e4f3b4080bf6 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 = ['r','','l','f','f','f','f','','','','f','f','f','f','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] < 2) { | |
// block ahead | |
if (env[1] > 1) decision = 'r'; | |
else if (env[3] > 1) decision = 'l'; | |
else decision = 'b'; | |
} | |
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; | |
case 'b': | |
api.moveBackward(); | |
route.unshift('b'); | |
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 1: 1251