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

Backwoods Treasure JavaScript. Help needed

Hello, guys! Could you, please, help me fix the code?

The problem is like this:

  • if my hero stays in one zone with coins, the time runs off and I’m not able to collect 100 coins in time;
  • if I use loop to move into another zone, my hero only kills enemies without picking up coins.
function pickUpGoldOrFight(x, y) {
    hero.moveXY(x, y);
    var enemy = hero.findNearestEnemy();
    if (enemy) {
        var ready = hero.isReady("cleave");
        if (ready) {
            hero.cleave(enemy);
        } else {
            hero.attack(enemy);
        }
    } else {
        if (!enemy) {
            var gold = hero.findNearestItem();
            if (gold) {
                var item = hero.findNearestItem();
                var itemX = item.pos.x;
                var itemY = item.pos.y;
                hero.moveXY(itemX, itemY);
            }
        }
    }
}
while (true) {
    pickUpGoldOrFight(58,14);
    pickUpGoldOrFight(58,49);
}

Howdy and welcome to the forum!

Have you thought about using flags?

Hello, and thank you for the idea!!

} else {
                if (!gold && !enemy) {
                    var flag = hero.findFlag();
                    if (flag) {
                        hero.pickUpFlag(flag);
                    }
                }

I tried adding this with (if (!gold && !enemy)) and without this line… and my char doesn’t react to flags.
I got a weird feeling that using flags will make me rewrite the whole code…

Not necessarily. I would suggest leaving your function code as it is (it appears good) and add the flag code to the while true loop. Something as simple as (not tested, just an idea):

while (true) {
    pickUpGoldOrFight(58,14);
    var flag = hero.findFlag("green") // I had just the basic flag when I first did this level
    if (flag) {
        hero.pickUpFlag(flag);
        pickUpGoldOrFight(58,49);
    }
}

So, the premise is that you only place a flag when the first section is pretty much cleared out. Place the flag in the center section, kind of near to the section you want to clear out next…the hero.findItems code will take over from there.

Also, I didn’t place my flag until the ogres stopped generating…they appear slower and slower, to an almost stop and that’s when I flag.

Thank you very much for the idea! I will be able to check it in 21 hours. I will write you whether it worked or not tomorrow. :slight_smile: