Help! mad maxer gets greedy

At 20 seconds the hero rests against the wall.
Level https://codecombat.com/play/level/mad-maxer-gets-greedy?
Снимок

while True:
    bestCoin = None
    maxRating = 0
    coinIndex = 0
    coins = self.findItems()
    while coinIndex < len(coins):
        coin = coins[coinIndex]
        coinIndex += 1
        if coin.value / self.distanceTo(coin) > maxRating:
            maxRating = coin.value / self.distanceTo(coin)
            bestCoin = coin
        
    if bestCoin:
        self.moveXY(bestCoin.pos.x, bestCoin.pos.y)

the only thing I did differently than your code was to define value and distance and use them in the first if statement. I don’t believe that makes any difference though. I believe the issue is that you incremented before the value / distance computation. I always increment last, after the computations.

distance = hero.distanceTo(coin)
value = coin.value

Also, when running the code before submitting, it would never collect enough to beat the doppelganger. But when submitted, it wins every time.

If I increment after computing, my code does not even start. Do not zayu why. All options pereprobyval, the hero rests against the wall at 20 seconds. Give your source, please.

I just ran your code exactly as you have it written and it worked fine. Now, I think you have a gear problem. What glasses are you using? I’m using fine wooden glasses with range of 10 meters. If you’re using long range glasses it would explain why your character is trying to get to the other side and thus the “I can’t get there” message. Try using shorter range glasses.

Thank you.) I used the “Sees through walls” glasses. The code works. but for some reason, at the end of the double collects one coin more. (Shoes “Leather Boots”)

The problem is solved. Everything works, thank you!

I had the same problem with glasses and solve it with adding condition x < 40 :nerd_face:

if maxRating > 0 and coins[coinIndex].pos.x < 40:
    ...
2 Likes