Created
August 10, 2015 13:29
-
-
Save nathanpeck/6888ed11b1e13f030818 to your computer and use it in GitHub Desktop.
Very simple, dumb AI for code challenge
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'); | |
/** | |
* http://honeypot.softwareskills.se/#/contest/5587cf988a4c5edb08ffa049 | |
* | |
* Executes a single step of the tank's programming. The tank can only move, | |
* turn, or fire its cannon once per turn. Between each update, the tank's | |
* engine remains running and consumes 1 fuel. This function will be called | |
* repeatedly until there are no more targets left on the grid, or the tank runs | |
* out of fuel. | |
*/ | |
var currentPlan; | |
var visited = []; | |
var knownInfo = {}; | |
var orientations = ['north', 'east', 'south', 'west']; | |
var currentOrientation = 2; | |
var currentPosition = { | |
x: 0, | |
y: 0 | |
}; | |
function buildMap(distances) { | |
// Based on current info and knowledge about the distance to things build | |
// a map. | |
} | |
function planMovementsBasedOnLidar() { | |
// We have no objective right now so | |
// build a plan based off of Lidar data | |
var distances = { | |
front: api.lidarFront(), | |
back: api.lidarBack(), | |
left: api.lidarLeft(), | |
right: api.lidarRight() | |
}; | |
var movements; | |
var thisPlan = []; | |
// Turn in the longest direction | |
if (distances.front > 1) { | |
movements = distances.front; | |
} | |
else if (distances.right > 1) { | |
thisPlan.push('turnRight'); | |
move = distances.right; | |
} | |
else if (distances.left > 1) { | |
thisPlan.push('turnLeft'); | |
move = distances.left; | |
} | |
else { | |
// We want to head back behind us, so turn twice | |
thisPlan.push('turnRight'); | |
thisPlan.push('turnRight'); | |
move = distances.back; | |
} | |
for (var i = 0; i < movements - 1; i++) { | |
thisPlan.push('moveForward'); | |
//thisPlan.push('seek'); | |
} | |
return thisPlan; | |
} | |
exports.update = function() { | |
if (!currentPlan || currentPlan.length === 0) { | |
currentPlan = planMovementsBasedOnLidar(); | |
} | |
else { | |
// Check for immeadiate targets that we should shoot as this | |
// always takes precedence over any movement plan. | |
if (api.identifyTarget()) { | |
// Cancel all plans, fire cannon then turn and recalc plans | |
api.fireCannon(); | |
currentPlan = ['turnRight']; | |
} | |
else { | |
// Just execute on the movement plan | |
var action = currentPlan.shift(); | |
console.log('Currently at (' + | |
currentPosition.x + ',' + | |
currentPosition.y + ') facing ' + | |
orientations[currentOrientation]); | |
if (action == 'turnRight') { | |
currentOrientation++; | |
if (currentOrientation > 3) { | |
currentOrientation = 0; | |
} | |
} | |
else if (action == 'turnRight') { | |
currentOrientation--; | |
if (currentOrientation < 0) { | |
currentOrientation = 3; | |
} | |
} | |
else if (action == 'moveForward') { | |
if (orientations[currentOrientation] == 'north') { | |
currentPosition.y++; | |
} | |
else if (orientations[currentOrientation] == 'south') { | |
currentPosition.y--; | |
} | |
else if (orientations[currentOrientation] == 'west') { | |
currentPosition.x--; | |
} | |
else if (orientations[currentOrientation] == 'east') { | |
currentPosition.x++; | |
} | |
visited.push(currentPosition.x+'_'+currentPosition.y); | |
} | |
console.log('Executing ' + action); | |
api[action](); | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment