About of "mad-maxer-gets-greedy"


#1

Hi I need your helps.
I think gold coins are required to clear on the priority.
How can I change for the better?
hint shows “value”. I don’t know how to use it.

// Collect more coins than your doppelganger.
// You only have a few seconds to collect coins. Choose your path wisely!
loop {
    var bestCoin = this.findItems("GOLD COIN");
    var maxRating = 0;
    var coinIndex = 0;
    var coins = this.findNearest(this.findItems());
    // Try calculating "value / distance" to decide which coins to get.
    if(coins){
        var distance = this.distanceTo(coins);
        if(distance < 20) {     
          pos = coins.pos;
          coinX = coins.pos.x;
          coinY = coins.pos.y;
          this.moveXY(coinX, coinY);
          pos = coins.pos;
        }else if(bestCoin){
          pos = coins.pos;
          coinX = coins.pos.x;
          coinY = coins.pos.y;
          this.moveXY(coinX, coinY);
          pos = coins.pos;
        }
    }
}

#3

You want to rate each coin by coin.value / this.distanceTo(coin). The one with the highest rating should be the one you pick up each time. Comparing each rating to the maxRating and setting it to bestCoin if its rating is the new maxRating using Math.max is the technique we’re going for here, following the previous two Mad Maxer levels you did practicing that.


#4

I’m having the same issue. I’m missing something small, and I know it. I just can’t think of it. Here is my code:

loop {
    var bestCoin = null;
    var maxRating = 0;
    var coinIndex = 0;
    var coins = this.findItems();
   
    while (coinIndex < coins.length) {
       var coin = coins[coinIndex];
       var distance = this.distanceTo(coin);
       var value = coin.value; 
       if (value / distance > maxRating) {
           maxRating = value / distance;
           bestCoin = coin; 
        }
    coinIndex += 1;
    }  
    if (bestCoin) {
        this.moveXY(bestCoin.pos.x, bestCoin.pos.y);
    }
}

What could I be missing?


#5

Hello, Justin, and welcome. Please read the FAQ prior to your next post. You must learn how to format your code properly. I’ve done it for you this time, but please do so yourself in the future.

Your problem is that you only increment coinIndex outside the while-loop. This is quite irregular. Perhaps you meant to put it inside?