Play Home Contribute Github Join Us! Discourse Staff Members Github Contribution Guides Team

Level: Mad Maxer Sells Out - Problem

I followed all the directions, but my hero is not collecting the nearest coin. He’s taking extra time to go to a coin farther away, and then he can’t get all the gold coins before they disappear.

I think it is because the variable minGoldDist starts out so high, and so the first coin is kind of random.

Here is a copy of my code. I have a similar problem in the next level, Mad Maxer Gets Greedy. Help would be greatly appreciated!!

# Coins here disappear after a few seconds!
# Get all the gold coins before they vanish.

loop:
    closestGold = None
    minGoldDist = 9000
    coinIndex = 0
    coins = self.findItems()
    # Find the closest coin that is gold.
    # Remember that gold coins have a value of 3.
    while coinIndex < len(coins):
        coin = coins[coinIndex]
        distance = self.distanceTo(coin)
        if distance < minGoldDist:
            if coin.value == 3:
                closestGold = coin
        coinIndex += 1
        
    if closestGold:
        self.moveXY(closestGold.pos.x, closestGold.pos.y)
2 Likes

You never sent minGoldDist to the dist of the closetGold so it will always be the last gold coin checked…

1 Like

Thanks! I think that did the trick. I just added this line of code:

minGoldDist = self.distanceTo(closestGold)

I guess I just didn’t think about the fact that minGoldDist was never getting reset!

2 Likes

Actually, this is the “distance” I had this in mind…

        if distance < minGoldDist:
            if coin.value == 3:
                closestGold = coin

                                 minGoldDist = distance

since you already know “distance” to the current coin, no need to calculate it again. :smile:

1 Like

Sorry, but where exactly in the code should I put the “minGoldDist = distance”?

It goes right after the “if distance < minGoldDist” but before the “if coin.value == 3”, right?

2 Likes

I’m also stumped here. Hero collects some coins, but some disappear before he gets to them, level goals remain incomplete. He doesn’t go to the closest one first, but just some random one.

while True:
    closestGold = None
    minGoldDist = 9001
    coinIndex = 0
    coins = hero.findItems()
    # Finde die nächste Goldmünze.
    # Beachte das Goldmünzen einen Wert (value) von 3 haben
    while coinIndex < len(coins):
        coin = coins[coinIndex]
        if coin:
            distance = hero.distanceTo(coin)
            if distance < minGoldDist and coin.value == 3:
                closestGold = coin
                minGoldDistance = distance
                pass
            coinIndex += 1
            pass
    if closestGold:
        #Begib dich zur nächsten Goldmünzen und bekomme sie!
        hero.moveXY(closestGold.pos.x, closestGold.pos.y)
        pass

I wonder why the initial value settings:

    closestGold = None
    minGoldDist = 9001
    coinIndex = 0

are under the while True loop - because that means the values get reset at each iteration - and each gold coin that happens to turn out first in the array gets set as variable closestGold. But that’s the pre-written code, so it must be correct…?

1 Like

This line, minGoldDistance = distance is wrong.

Fix that one line and it works fine.

The pre-written code has to be in the while True loop because the code is iterating and incrementing through an array. If only one coin were to appear then yes, it would be set as closestGold, but you’ll notice that three appear at a time.

1 Like

Got it, thanks! 20 characters

im getting infinite loop ,could i get some help please

Please post your code with proper formatting so we can analyze it and help you identify the cause of the infinite loop.

You should increase the coinIndex outside of that if-block, otherwise it’ll never “increase” the index if it doesn’t fulfill the conditions and thus never reach coins.length, resulting in an infinite loop.
Also, you probably want to check if the value is 3 in that if block, not if the coin is 3. :sweat_smile:

1 Like

Is the main topic here considered global varibles ? so i can study the structure some more,this is my tipping point honestly.i cannot get any further past this. yet that is :slight_smile: