I have passed Distraction Maneuver after cheating and looking up a solution. It turns out the only portion I had wrong was the reassignment of maxDistance the first function.
However, I have no clue why the reassignment actually works to return the furthestUnit. There is likely a simple explanation but I haven’t been able to wrap my head around it. Some understanding of this would be appreciated.
In my code below, why is reassigning maxDistance = distanceto needed? Where is maxDistance even used?
- If I reassign maxDistance = furthestUnit, my character attacks the nearest units and I fail.
- If I reassign maxDistance = 1 (totally ignoring the instructions), my character attacks the furthest units and I pass the level.
- If I reassign maxDistance = 100, my character attacks the nearest units and I fail.
This is pretty confusing. See paste:
# Protect the peasants! # This function find the furthest one from units. def findFurthest(units): furthestUnit = None maxDistance = 0 unitIndex = 0 while unitIndex < len(units): currentUnit = units[unitIndex] # Find the distance to currentUnit: distanceto = hero.distanceTo(currentUnit) # If that distance greater than maxDistance: if distanceto > maxDistance: # Reassign furthestUnit and maxDistance: furthestUnit = currentUnit maxDistance = distanceto unitIndex += 1 return furthestUnit # It's like findNearestEnemy but vice versus. def findFurthestEnemy(): enemies = hero.findEnemies() furthestEnemy = findFurthest(enemies) # Return furthestEnemy: return furthestEnemy # The function makes the hero attack without distractions. def attackWhileAlive(target): # Attack target while it's health > 0: while target.health > 0: hero.attack(target) pass while True: # To protect peasants, hunt for furthest ogres. furthestOgre = findFurthestEnemy() if furthestOgre: attackWhileAlive(furthestOgre)