It’s hard to tell what your exact problem is due to the fact you didn’t format your code correctly
(select it and use this )
I do see a problem your findOptmialCoin function. You use the same variable name twice, so when it will always return the last coin in the array. Use a separately named variable like bestCoin to avoid this:
while coinIndex < len(coins):
coin = coins[coinIndex]
if coin.value/hero.distanceTo(coin)>V/D:
bestCoin = coin
#put the rest of the function here
return bestCoin
There are some problems with this level, but I passed it.
First - you need the fast sword. The best swords are to slow, so coins are disappear before hero can move. So use some of the weak swords.
Second - you need not only move. Hero to slow. Jump on the coin. https://github.com/vadim-job-hg/CodeCombat/blob/master/Desert/DiamondDozen.py
I do have time issues on Diamond Dozen. I’m not fast enough in about a third of all coin appearances. Even moving the the closest coin would not help me.
The problem might be, that I don’t have a clue how to jump or use a faster sword.
// Claim the coins while defeating the marauding ogres.
// If you defeat the ogre with the most health, the rest of the ogres will run!
// Coins vanish quickly after appearing, so be sure to find the best value!
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;
}
// Make a function named findOptimalCoin which returns the coin with the best value.
// Coins rapidly appear and disappear, so pick the best coin.
// Optimize your path by going for the coin with the largest value over distance.
function findOptimalCoin(coins){
var coinIndex = 0;
var optimal = 0;
var best;
while(coinIndex < coins.length) {
var distance = hero.distanceTo(coins[coinIndex]);
var value = coins[coinIndex].value;
if((value / distance) > optimal){
optimal = value / distance;
best = coins[coinIndex];
}
coinIndex++;
}
return best;
}
while(true) {
var enemies = hero.findEnemies();
var enemy = findMostHealth(enemies);
if(enemy && enemy.health > 15) {
while(enemy.health > 0) {
hero.attack(enemy);
}
} else {
var coins = hero.findItems();
var coin = null;
coin = findOptimalCoin(coins); // ∆ Uncomment this once you've written the function.
if(coin) {
var ready = hero.isReady("jump");
ready? hero.jumpTo(coin) : hero.moveXY(coin.pos.x, coin.pos.y);
}
}
}
I noticed that the methods that worked didn’t really make sense to me and I had to spend a lot of time just trying to figure out what the methods were doing. For future reference, you should never name your variables things like a, b, and c. Additionally, I did notice that if you have an ability like jump in your equipment then the algorithm will count that. So, even if it isn’t equipped and just in your equipment it will still be part of the equation. Therefore you need to add the conditional to utilize jump if it is ready.
I am having trouble with this level as well. I used the suggestions here already (it seems to come now with some guidance on choosing the enemy with most health to attack and finding the optimal coin to go to), but the coins are disappearing before I can get to them most of the time. If I don’t use the powerful Runesword (even with the long cooldown) it is even worse.
I tried using boots with jump or dash abilities, but these abilities seem broken and do not reach the coin either.
I tried using a different hero to see if it would make a speed difference, but that didn’t help.
You can only jump if you have “Boots of Jumping” or “Boots of Leaping”.
Second, you can find how fast a sword is comparing the DPS in the item description to damage per hit. For example, if a sword does 10 damage per hit, and 50 per second, that means you can attack 5 times with the sword.
So I’m still lost on this level. I tried using hero.jumpTo(coin) but that has a weird lag time after the jump so that’s a no-go. Here’s my code, formatted. Can someone maybe tell me what I’m not getting?
def findMostHealth(enemies):
target = None
targetHealth = 0
enemyIndex = 0
while enemyIndex < len(enemies):
enemy = enemies[enemyIndex]
if enemy.health > targetHealth:
target = enemy
targetHealth = enemy.health
enemyIndex += 1
return target
def valueOverDistance(item):
return item.value / hero.distanceTo(item)
# Return the item with the highest valueOverDistance(item)
def findBestItem(items):
bestItem = None
bestValue = 0
itemsIndex = 0
while itemsIndex < len(items):
item = items[itemsIndex]
if valueOverDistance(item) > bestValue:
bestItem = item
itemsIndex += 1
# Loop over the items array.
# Find the item with the highest valueOverDistance()
return bestItem
while True:
enemies = hero.findEnemies()
enemy = findMostHealth(enemies)
if enemy and enemy.health > 15:
while enemy.health > 0:
hero.attack(enemy)
else:
coins = hero.findItems()
coin = None
coin = findBestItem(coins)
if coin:
hero.moveXY(coin.pos.x, coin.pos.y)