I don’t understand why my warrior doesn’t go after the closest high valued coin. I’ve tried many similar topics before starting my own can someone help?
// Claim the coins while defeating the marauding ogres.
function findMostHealth(enemies) {
var target = null;
var targetHealth = 0;
var enemyIndex = 0;
while(enemyIndex < enemies.length) {
var enemy = enemies[enemyIndex];
if(enemy.health > targetHealth) {
target = enemy;
targetHealth = enemy.health;
}
enemyIndex += 1;
}
return target;
}
// Return the item with the highest valueOverDistance(item)
function valueOverDistance(item) {
return hero.distanceTo(item)/item.value;
}
function findBestItem(items) {
var bestItem = null;
var bestValue = 0;
var itemsIndex = 0;
// Loop over the items array.
// Find the item with the highest valueOverDistance()
while (itemsIndex < items.length) {
let coin = items[itemsIndex];
if (valueOverDistance(item) > bestValue) {
bestItem = coin;
bestValue = bestItem.value;
}
itemsIndex += 1;
}
return bestItem;
}
while(true) {
var enemies = hero.findEnemies();
var enemy = findMostHealth(enemies);
var coins = hero.findItems();
if(enemy && enemy.health > 15) {
while(enemy.health > 0) {
hero.attack(enemy);
}
} else {
var coin = null;
coin = findBestItem(coins);
if(coin) {
var ready = hero.isReady("jump");
ready ? hero.jumpTo(coin) : hero.moveXY(coin.pos.x, coin.pos.y);
}
}
}