Distraction Maneuver (Sarven Desert) explanation

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:

while True:
    # To protect peasants, hunt for furthest ogres.
    furthestOgre = findFurthestEnemy()
    if furthestOgre:
1 Like

You loop over all enemies i. e. unitIndex will go from 0 to the number of the last enemy
For each enemy, you check the distance.
If the distance it is small, the enemy would be close, if it is large, the enemy is far away.
But which enemie is the farthest?
Well, what you do is:
Stick with one, check the others. if you find one, which is better (farther away) stick with him and drop the previous, you stuck with. You match each enemy against the best one you have. If its better, thats your new best!
In the end, you will keep the one, that has beaten all the others.
That’s the farthest!
Ho do you “stick” to an enemy? Your store the enemy itself in furthestUnit. Plus, you store the distance of that (so far best choice) enemy in maxDistance.
And then you compare the distance to all following enemies to your maxDistance
(distanceto >maxDistance) to see, if you should stick with the new one.

But how to start?
You assume, you have an enemy with distance 0 in the beginning. This will result in the first enemy in the list to get the one you stick with first.

When you chose a number like 1 for maxDistance, eventually you will find an enemy who is farther away. From then on, your loop works as intended, i. e. that enemy would be the first you stick with.

With a number like 100, no enemy will be that good, so you get none that is better and your function fails.



Conceptually still having some issues wrapping my head around this and I have decided I am not too bright.

However, I will attempt to write out in plain english what I believe the code in the loop is doing and why I did not understand it.

  • If the distance to the current unit being investigated in the array is greater than 0,
  • Then assign the furthest unit (starts out as None) to the current unit,
  • And assign the max distance to the current unit as the distance to the current unit,
  • Then look at the next unit in the array
  • and rerun steps 1, 2, and 3.
  • if the distance to the next unit analyzed in rerunning step 1 is greater than the previous assignment of max distance,
  • assign furthestUnit to this new currentUnit
  • assign the maxDistance to the distance between the hero and this currentUnit
  • and re-reun steps 1, 2, and 3.
  • And return furthestUnit (the distance to the currentUnit)

Then in the second function

  • find all enemies
  • find the furthest out of all enemies
  • and return the value of furthestEnemy, which can be assigned to a variable at will

The third function is self explanatory

Then in the last function

  • Assign the value of the first function to furthestOgre
  • if there is a furthestOgre
  • Kill it while it’s still alive (it would be awkward to kill it while dead)

In my head part of the reason I didn’t get this is because I made a basic conceptual mistake assuming that the definition of the function is actually running the function. Obviously we learned a while ago in previous lessons what loops were but I am on and off with codecombat and microfocused on the definition, assuming the definition of findFurthest was itself in a loop. This is obviously untrue; the function is only run when called in the loop at the very end, and its results assigned to a variable.

In addition I think the naming of the variable as maxDistance threw me for a loop (heh) as I took the meaning of it literally. Obviously the name can be completely arbitrary but I thought there was some reason the teachers made the lesson this way. I probably would have been less confused if it was named something like “greatestDistance” or “highestDistance”

Anyway. moving right along…