Skip to content

Instantly share code, notes, and snippets.

@kkuchta
Created June 4, 2012 00:08
Show Gist options
  • Save kkuchta/2865532 to your computer and use it in GitHub Desktop.
Save kkuchta/2865532 to your computer and use it in GitHub Desktop.
/**
* Get the nearest fruit of each type.
*
* @return [ [fruit1X,fruit1Y], [fruit2X],[fruit2Y], ... ]
*/
getNearestFruits: function(botX,botY){
var closestFruits = [];
// Init the array
for( var type = 1; type <= get_number_of_item_types(); type++ ){
closestFruits[type] = {
location:[-1,-1],
distance:Number.MAX_VALUE
}
}
var board = get_board();
// Check each fruit on the board in turn.
for( var y = 0; y < HEIGHT; y++ ){
for( var x = 0; x < WIDTH; x++ ){
// If there's a fruit here
var fruit = board[x][y];
if( fruit > 0 ){
var distance = util.getEuclideanDistance( [x,y],[botX,botY] );
//console.log('fruit=' + fruit );
if( distance < closestFruits[fruit].distance ){
closestFruits[fruit].location = [x,y];
closestFruits[fruit].distance = distance;
}
}
}
}
console.log(closestFruits);
return closestFruits;
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment