Turns out arrays are a lot more complicated than I have been giving them credit for.
I’m currently stuck on Diamond Dozen. Was working on “The Trials” which is still incomplete, but I’ve gotten progress there, and figured I’d find what I was looking for (how to not target the enemy across the map since there is a wall in my way) and tried quite a few things to no avail. Getting off topic so I’ll digress.
Ended up struggling through quite a few of levels that I still have to go back and study on due to a lack of confidence in understanding them, but I can’t for the life of me figure out this one now.
# Claim the coins while defeating the marauding ogres. 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 # Loop over the items array. # Find the item with the highest valueOverDistance() while itemsIndex < len(items): item = items[itemsIndex] if item.value > bestValue: bestItem = item bestValue = valueOverDistance(item) itemsIndex += 1 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)
He is supposed to go after the coin with the best distance and value from what I understand, but I can’t seem to figure out what I am doing wrong. I can get him to go after coins, but it seems that he just goes for whatever is closest if he goes at all. Seriously lost