Can someone explain the code of Brittle Morale and Diamond Dozen to me?
Brittle Morale:
Some descriptive text
# Ogres are strong, but cowardly.
# Find and kill leader and they will retreat.
# You have only have one shot, but a deadly shot it will be.
# This function should return the enemy with the most health.
def findStrongestEnemy(enemies):
strongest = None
strongestHealth = 0
enemyIndex = 0
# Iterate over all the ogres to find the one with the most health.
while enemyIndex < len(enemies):
if enemies[enemyIndex].health > strongestHealth:
strongest = enemies[enemyIndex]
strongestHealth = strongest.health
enemyIndex += 1
return strongest
enemies = hero.findEnemies()
if enemies:
hero.say(findStrongestEnemy(enemies))
Diamond Dozen:
Some descriptive text
# 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!
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
# Make a function named findOptimalCoin which returns the coin with the best value.
def findOptimalCoin(coins):
optimal = -1000
thione = None
for coin in coins:
if coin.value / self.distanceTo(coin) > optimal:
optimal = coin.value / self.distanceTo(coin)
thione = coin
return thione
# Coins rapidly appear and disappear, so pick the best coin.
# Optimize your path by going for the coin with the largest value over distance.
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 = findOptimalCoin(coins)
if coin:
hero.moveXY(coin.pos.x, coin.pos.y)
The code works, I just want to understand why and how…