[SOLVED] Mad-Maxer gets greedy

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

Hello all,

I’ve read through the thread, and I think my code is right, but I keep picking up less coins - any idea what’s wrong?

    if coinIndex < len(coins):
        coin = coins[coinIndex]
        distance = hero.distanceTo(coin)
        value = coin.value
        if value/distance > maxRating:
            maxRating = value/distance
            bestCoin = coin
        coinIndex += 1
    
    if bestCoin:
        hero.moveXY(bestCoin.pos.x, bestCoin.pos.y)

Hey can someone post a solution code that works?
This level only seems to work for when the character is either too slow or it doesn’t move at all
A complete code would be nice, doesn’t seem like the codes are working at all

what should I do with this code?

while True:
minGoldDist = 9001
bestCoin = None

coinIndex = 0
coins = hero.findItems()

Try calculating “value / distance” to decide which coins to get

while coinIndex < len(coins):
    coin = coins[coinIndex]
    bestDist = hero.distanceTo(coin) / coin.value
    if minGoldDist > bestDist:
        bestCoin = coin
        minGoldDist = bestDist
    pass
    coinIndex += 1
    
pass
if bestCoin:
    hero.moveXY(bestCoin.pos.x, bestCoin.pos.y)

says it cant read property, move

(SOLVED)GuysOh now I get why it didn’t work, if your glasses see through walls you get stuck and the player stops collecting gold, check that one if you are having problems with this level, this should help many of the problems some people are having

1 Like