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

Doom glade please help

hello!
this is my code


while (true) {
    var flag = hero.findFlag();
    var enemy = hero.findNearestEnemy();
    if (flag) {
        // hero.say("Maybe I should do something with that flag?");
        var greenFlag = hero.findFlag("green");
        if (greenFlag) {
            hero.pickUpFlag(greenFlag);
        }
    } else {
        // hero.say("I'm bored.");
        if (enemy) {
            var distance = hero.distanceTo(enemy);
            if (distance < 10) {
                if (hero.isReady("cleave")) {
                    hero.cleave(enemy);
                } else {
                  hero.attack(enemy);
                  if (hero.findFriends().length > 5) {
                      hero.shield();
    
                  }
            }
        }
    }
}
}
but |I can't pass this level, I can't kill all ogres, they are too srong, please help me

can anyone help me, I post this some time ago and anyone did’t response me :frowning:

First, you are both finding “flag” and then finding “greenFlag”…this is redundant; instead, findFlag, use ‘if flag, then pick it up, if no flag, then cleave if able, else attack’. There is no need for your final if statement.

I re-worked your code, removing all comments, etc and ended up with only 11 lines (not counting the closing brackets). It was able to pass the level. I dropped a flag once, near the end, when the hero was getting low on health…I moved him behind the remaining friends; he did rejoin the fight, but there were fewer enemies left, so he prevailed.