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

[SOLVED] Mad Maxer Gets Greedy - Hero moves to wrong coins and random positions

Hello! I am playing Mad Maxer Gets Greedy (Sarven Desert), and something is wrong but I can’t quite place it. My hero moves to a random coin’s position about half the time, and then to a completely unrelated position the other half. Thank you in advance! Here’s my code:

while(true) {
    var bestCoin = null;
    var maxRating = 0;
    var coinIndex = 0;
    var coins = hero.findItems();
    // Try calculating "value / distance" to decide which coins to get.
    while (coinIndex < coins.length) {
        var coin = coins[coinIndex];
        if (coin.pos.x > 40) {
            coinIndex++;
            continue;
        }
        var distance = hero.distanceTo(coin);
        var rating = coin.value / distance;
        if (rating > maxRating) {
            maxRating = rating;
            bestCoin = coin;
        }
        if (bestCoin) {
            hero.moveXY(bestCoin.pos.x, bestCoin.pos.y);
        }
        coinIndex++;
    }
}

Hmm…

  1. Please show me your gear, and try not to use the Twilight Glasses. Even though I have them, I use the enchanted lenses so that I wouldn’t need the if (coin.pos.x > 40) { coinIndex++; continue;} line of code.
  2. The if (bestCoin) { hero.moveXY(bestCoin.pos.x, bestCoin.pos.y); } should be placed outside of the while (coinIndex < coins.length) loop. If you don’t, your code might not have accounted the best coin in the field as bestCoin, but you’re already moving to a less valuable one.
  3. Look at the tips and docs. Use hero.move(bestCoin.pos) to move faster as it will constantly update as the positions of the hero (and maybe the coin, but not in this level) changes, but hero.moveXY(bestCoin.pos.x, bestCoin.pos.y); doesn’t.
1 Like

Also, welcome to the community!

Thanks for replying!
I do have the Twighlight Glasses, but I don’t think I was using them when I first tried this level. Here’s a picture of my gear (I’m using Anya):
image

Ah, that worked! Thank you so much!! I didn’t know that about the hero.move versus hero.moveXY difference. That made it really clear! Have a good day :smile:

1 Like

@Bubble_Animations, For later coding…I like to think of moveXY as a fire and forget method. Once called, no other code will run until the hero reaches X, Y. On the other hand, move performs as a step by step…typically, move is placed in a loop, so it ‘steps’ each time the loop iterates.

2 Likes

Thanks! I like that term - “fire and forget method”. I’ll keep it in mind! :smile: