[SOLVED] Mad-Maxer gets greedy

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

You’re missing a line here. You have to update maxRating’s value, otherwise it will always be 0 and all the coins will be classed as the bestCoin.
Danny

1 Like

Hi @Deadpool198,

Thank you for your answer, with some changes it works better, but i still loses a few coins

while coinIndex < len(coins):
        coin = coins[coinIndex]
        distance = hero.distanceTo(coin)
        value = coin.value
        if value/distance > maxRating:
            maxRating = value/distance
            bestCoin = coin
            
        coinIndex += 1

Sorry, but no one of your ways helps me… use tgth with u Pet"chest"
and just
while True:
hero.moveXY(27, 50)
hero.moveXY(26, 19)

Tried several times…

Welcome the CoCo Discourse forums @Egor_Belov ! :partying_face:
This is a place to get help within levels, dicuss ideas, give suggestions for the game or just chat with other awesome coders!
Check out the FAQ and the rules if you haven’t already and have fun! :grinning_face_with_smiling_eyes:

Welcome to the discourse Egor! :partying_face: For the next time, please format your code correctly using this button:

As for the code you should be writing, it should have:

  1. variables of the best coin and a maximum rating (of value/distance)
  2. a while loop to go over the coins you can see
  3. inside the while-loop, if its rating (value/distance) is greater than the maximum rating, then update the maximum rating and the best coin
  4. once you find the best coin, move to its position

A small hint: to avoid the error of the hero trying to move to the other side, make sure the x coordinate of the coin is less than where the pillars are