Summit's Gate help in JavaScript


#1

Hey guys, need some help on Summit’s gate coding in JavaScript. The following code doesn’t seem to do anything and my soldiers just scatter without attacking anything. Hero doesn’t do anything and won’t pick up flags. I’m not sure where the problem is so I’m posting all my code / functions below.

//Choose Hero strategy, "flag", "attack, or "shield"
this.chooseHeroStrategy = function () {
    var flags = this.findFlags();
    var enemies = this.findEnemies();
    if (flags) {
        return "flag";    
    }
    else if (enemies) {
        return "attack";    
    }
    else {
        return "shield";    
    }
};

//Execute a chosen Hero Strategy
this.executeHeroStrategy = function () {
    var strategy = this.chooseHeroStrategy();
    if (strategy == "flag") {
        var flag = this.findFlag();
        this.pickUpFlag(flag);
    }
    else if (strategy == "attack") {
        var htarget = this.chooseHeroEnemy();
        if(htarget) {
        this.attack(htarget);
        }
    }
    else if (strategy == "shield") {
        this.shield();    
    }        
};

//Prioritize enemies by either catapult, warlock, or nearest
this.chooseHeroEnemy = function () {
    var enemies = this.findEnemies();
    var nearenemy = this.findNearest(enemies);
    var catapults = this.findByType("catapult", enemies);
    var warlocks = this.findByType("warlocks", enemies);
    
    if (catapults) {
        var ctarget = this.findNearest(catapults);
        if (ctarget) {
            return ctarget;
        }
    }
    else if (warlocks) {
        var wtarget = (this.findNearest(warlocks));
        if (wtarget) {
            return wtarget;    
        }
    }
    else if (nearenemy) {
        return nearenemy; 
    }
};

//Tell Allies to either "retreat", "attack", or "defend"
this.chooseAllyStrategy = function (ally) {
    var enemies = this.findEnemies();
        if (this.now() < 15) {
        return "retreat";    
    }
    else if (enemies) {
        var nearenemy = ally.findNearest(enemies);
        if (ally.distanceTo(nearenemy < 40)) {
        return "attack"; }    
    }
    else {
        return "defend";    
    }    
};

//Call Ally Strategy function and Execute it
this.executeAllyStrategy = function (allies) {
    for (a=0; a < allies.length; a++) {
        var ally = allies[a];
        var strategy = this.chooseAllyStrategy(ally);
        if (strategy == "retreat") {
            this.allyRetreat(ally);    
        }
        if (strategy == "attack") {
            this.allyAttack(ally);    
        }
        if (strategy == "defend") {
            this.command(ally, "defend", this.pos);    
        }
    }
};

this.allyRetreat = function (ally) {
    var retreatpoint = {x: 0, y: 37};
    this.command(ally, "move", retreatpoint);    
};

this.allyAttack = function (ally) {
    var enemies = this.findEnemies();
    var nearenemy = ally.findNearest(enemies);
    if (ally.type != "paladin") {
    if (nearenemy) {
        this.command(ally, "attack", nearenemy);    
    }
    }
    if (ally.type == "paladin") {
        this.healcommand(ally);        
    }
};
        
this.lowesthealthfriend = function () {
    var lowestfriend = null;
    var lowfriends = this.findFriends();
    lowfriends.push(this);
    var healthrating = 9999;
    if (this.health < 2000) {
        lowestfriend = this;        
    }
    else {
    for (i = 0; i < lowfriends.length; i++) {
        var lowfriend = lowfriends[i];
        if (lowfriend.health < healthrating && lowfriend.health < lowfriend.maxHealth) {
            lowestfriend = lowfriend;
            healthrating = lowfriend.health;
        }
    }
    }
    if (lowestfriend) {
        return lowestfriend;    
    }
};

this.healcommand = function(paladin) {
    var healtarget = this.lowesthealthfriend();
    if (healtarget) {
        this.command(paladin, "cast", "heal", healtarget);    
    }
};

loop {
    var allies = this.findFriends();
    this.executeHeroStrategy();
    this.executeAllyStrategy(allies);
}

#2

The findFlags and findEnemies methods always return arrays. Arrays are always truthy values in JavaScript. So, in your chooseHeroStrategy function, you should check whether the flags and enemies arrays contain anything. For example, you can check if there are any flags in the flags array using:

if (flags.length) {
// OR the equivalent form:
if (flags.length > 0) {

You have to do the same for the enemies array as well.


In your chooseAllyStrategy, you have a typo:

if (ally.distanceTo(nearenemy < 40)) {

Should be:

if (ally.distanceTo(nearenemy) < 40) {

#3

Hmm, I made those changes in my chooseHeroStrategy function and it didn’t seem to help anything. Also I don’t think that’s a typo in my chooseAllyStrategy function because I need that extra paren to close both the if statement and the distanceTo function right?

Any other ideas what else might be wrong?


#4

Never mind. I crushed it. Sometimes I think the code gets stuck in a loop even when you hit submit, because I boiled it down to almost nothing and it kept replaying the same failure. Then I hit reload code button, copied and pasted most of it, and it worked.

Anyway, nobody will read this, but point being I crushed the level and feel pretty damn good about it.


#5

The typo that I meant was that the closing parenthesis was in the wrong place: inside the if () you had ally.distanceTo(nearenemy < 40) instead of ally.distanceTo(nearenemy) < 40.

In any case, congratulations! :smile: