Sarven Treasure JavaScript(Need help!)

I can´t pass the SarvenTreasure. I need help because I pick up only 134 coins.
This is my code:

// Collect 150 gold while evading ogres with teleporters.
// If you win, it gets harder (and more rewarding).
// If you lose, you must wait a day before you can resubmit.
// Remember, each submission gets a new random seed.
// Returns Best Coin to Move Toward

while(true) {
var minDistance = 20;
var coinIndex = 0;
var coins = hero.findItems();
var ogres = hero.findEnemies();
var ogre = hero.findNearest(ogres);
var closestCoin = hero.findNearest(coins);

while (coinIndex < coins.length) {
   var  target = coins[coinIndex];
    coinIndex += 1;
    var distance = hero.distanceTo(target);
if (distance < minDistance && target.value >= 2){
  minDistance = distance;
 closestCoin = target;
}

}
// If no ogre and a coin nearby, get it.
if (minDistance <= 20 && closestCoin !=ogre) {
hero.moveXY(closestCoin.pos.x, closestCoin.pos.y);
}
//If an ogre and a nearby coin, get the coin unless the ogre is close. If the ogre is close, teleport.
else if (minDistance <= 20 && closestCoin && ogre){
ogreDistance = hero.distanceTo(ogre);

    if (ogreDistance >= 10 && closestCoin) {
        hero.moveXY(closestCoin.pos.x, closestCoin.pos.y);
    }  
}
    
else if (closestCoin) {
        if (hero.pos.x > 43 && hero.pos.y > 38){
            hero.moveXY(75, 50); 
        }
        if (hero.pos.x > 43 && hero.pos.y <= 38) {
            hero.moveXY(75, 20);
        }
        if (hero.pos.x <= 43 && hero.pos.y > 38) {
            hero.moveXY(5, 50);
        }
        if (hero.pos.x <= 43 && hero.pos.y <= 38) {
            hero.moveXY(5, 20); 
        }
     
}

}

1 Like

It works for me sometimes and other times it doesn’t. Since the ogre’s and coins are all randomly generated, you can not predetermine what will happen. I think you need to adjust your code a bit because your hero keeps on running into the ogres trying to grab a coin. Also, check your armor. Make sure you have strong enough gear before entering the arena!

1 Like

You need 500 health!

1 Like

Well, maybe for the first stage, but it gets harder as you go. That’s the thing with replayable levels :wink:

1 Like

The problem I’m currently having is that the teleporters seem to be bugged.
I’ve tested with both Chromium and Firefox.

Any variation of the following code sees my hero moving in an endless loop through whatever teleporter was selected by the code (using flags has the same result as well)

// Move player to nearest teleporter
function teleport() {
    if (hero.pos.x > 43 && hero.pos.y > 38){
        hero.moveXY(75, 50); 
    }
    if (hero.pos.x > 43 && hero.pos.y <= 38) {
        hero.moveXY(75, 20);
    }
    if (hero.pos.x <= 43 && hero.pos.y > 38) {
        hero.moveXY(5, 50);
    }
    if (hero.pos.x <= 43 && hero.pos.y <= 38) {
        hero.moveXY(5, 20); 
    }
}

Is this happening to anyone else?

I’m using python but I was not able to beat this level until I switched from the moveXY(x, y) to the move(targetPos) method. Review your methods to see how - I’m not much help with JS. The difference between the two is huge. moveXY just didn’t work. Once I made the change I was able to beat it 6 times before it just got too hard.