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

[SOLVED] Mad-Maxer gets greedy

I don’t understand why my hero pick up the coin with the lowest value/distance rating. Any help?

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

Try changing the middle line to:

maxRating = value/distance
1 Like

It works perfectly. Thanks!!!

1 Like

I’m having the same trouble on this level. Based upon what I’ve read here, the code should be working.

# Collect more coins than your doppelganger.
# You only have a few seconds to collect coins. Choose your path wisely!
loop:
    bestCoin = None
    maxRating = 0
    coinIndex = 0
    coins = self.findItems()
    # Try calculating "value / distance" to decide which coins to get.
    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 self.isPathClear(self.pos, bestCoin.pos):
                self.moveXY(bestCoin.pos.x, bestCoin.pos.y)

I’ve tried changing the position of coin.value and self.distanceTo(coin) but it doesn’t change the outcome. My guy appears to be heading for the lowest value coin. Sometimes he will head for the silver coins but I have yet to see him head for the gold first. Not sure what to do at this point.

Any help is greatly appreciated.

If you are moving to bestCoin while you are still in the while-loop, you will be moving to the least value coin that still has a greater ratio than 0, that is, the worst coins possible. Move the part where you move to the coin outside the while-loop. That should fix your problem.

Thank you! That worked perfectly.

hi im having difficulties with this stage, i used some help from the discussion above and it still doesnt work, when theres no coins the hero goes into the wall saying “i cant get there” and is taking too long to move to new coins that appear thus the dopplgenger bypasses me in gold count…

    if (bestCoin) {
        hero.moveXY(bestCoin.pos.x, bestCoin.pos.y);
    }

and another question is - in the hints it says to use the hero.move() and not hero.moveXY(x,y), then why doesnt it work like hero.move(bestCoin)?
image
image
sry for jumping an old post, seems better then opening new threads i think

Your hero should move to a position, so try bestCoin.pos instead of bestCoin.
Does it work now?

no, that was a general q about the hero.move()
look in my code thats exactly as it is now and doesnt work :c

Can you show me your code that you have now?

Here try to also check if the path from hero.pos to bestCoin.pos is clear, then move to the bestCoin’s position.
Do you need any more assistance at this level?

    if (bestCoin) {
        hero.moveXY(bestCoin.pos.x, bestCoin.pos.y);
    }

not working

MOD edit: The bulk of the posted code has been removed, to avoid it being used as a solution.

Have you tried to do this?


Do you need any more assistance at this level?

i dont quite understand what you mean by checking if the path from hero.pos to bestCoin.pos is clear

There is a known ‘glitch’ where the hero will see items/enemies that are not on the map, if the equipped glasses are ‘infinity’ types. In this case, it appears that your guy is finding coins on the other side of the dividing wall and tries to go pick them up…as soon as the other guy collects that particular coin, your guy is freed and will continue with the nearer coins on his side.

There are two ways (that I came up with) to counter this. The simplest is to equip a non-infinity pair of glasses…I tried using the Fine Telephoto glasses and it worked fine.

The other, cheaper, method is to set a distance qualifier in your if statement:

// you currently have:
if (value / distance > maxRating)

// try instead:
if (value / distance > maxRating && coin.pos.x < 38)
2 Likes

I tried changing the glasses and it worked! (if anyone reading this in the future: it didnt work first time, had to re-enter the stage then it worked) tyvm ^^

p.s I cant seem to edit my post with the code for some reason (to remove full splution)

2 Likes

Well done! (I’ll take care of the edit for you)

1 Like

I think there is a better “patch” to advanced glaces:

if (value / distance > maxRating && coin.pos.x < 38)

you can halve the number of computations by:

    while (coinIndex < coins.length) {
      var coin = coins[coinIndex];
      if ( coin.pos.x < 38){   
      // original code here
      }  
      coinIndex++;
    }

you can also collect much more coins replacing moveXY with move()
There is another problem - gliding. you can get rid of it by:

    if (bestCoin) hero.moveXY(bestCoin.pos.x, bestCoin.pos.y);
    else hero.moveXY(hero.pos.x, hero.pos.y);

@warlok can you format your code as it is explined here so we will be able to help you?

Hi,

Thank you @AnSeDra, and sorry for that.

Returnning to my code, i maked a new variable to compare the coin vale, and the code work kind of, however the hero dosn’t pick up the closest coin.


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